×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Passing this keyword as a parameter

Passing this keyword as a parameter

Passing this keyword as a parameter

(OP)
I am creating some ASP.Net pages using C#. I want to create a class which will be used by other cs files. What I think I want to do is pass the this keyword as a parameter. The reason for this is I want to use .Controls.Add(new LiteralControl in each class using the current cs file. I've tried using ref but I'm definitely doing something wrong.

As simple as possible, here is what I have:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1.GenerateHTML();
}
}


And here is the class:
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}

public static void GenerateHTML()
{


//this.Controls.Add(new LiteralControl("<table><tr>"));
//this.Controls.Add(new LiteralControl("<td colspan=2 align=center valign=middle >"));
//this.Controls.Add(new LiteralControl("<table><tr><td>"));
//this.Controls.Add(new LiteralControl("<input id='Text1' type='text' value='This is text' />"));
//this.Controls.Add(new LiteralControl("</td><td>"));
//this.Controls.Add(new LiteralControl("<a href=tektips.com>"));
//this.Controls.Add(new LiteralControl("Hi This is someone struggling!"));
//this.Controls.Add(new LiteralControl("</a>"));
//this.Controls.Add(new LiteralControl("</td></tr></table>"));

}
}


The commented code is what I want to execute but of course I want to pass the current cs class. Please note I am learning C# so any additional advice would be greatly appreciated.

RE: Passing this keyword as a parameter

(OP)
I believe I have figured it out:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1.GenerateHTML(this.Page);
}
}


And here's the class:
public static class Class1
{
static Class1()
{
//
// TODO: Add constructor logic here
//
}

public static void GenerateHTML(this System.Web.UI.Page Webpage)
{
Webpage.Controls.Add(new LiteralControl("<table><tr>"));
Webpage.Controls.Add(new LiteralControl("<td colspan=2 align=center valign=middle >"));
Webpage.Controls.Add(new LiteralControl("<table><tr><td>"));
Webpage.Controls.Add(new LiteralControl("<input id='Text1' type='text' value='Patrick Norman' />"));
Webpage.Controls.Add(new LiteralControl("</td><td>"));
Webpage.Controls.Add(new LiteralControl("<a href=patmcdaniel.com>"));
Webpage.Controls.Add(new LiteralControl("Hi This is Norman"));
Webpage.Controls.Add(new LiteralControl("</a>"));
Webpage.Controls.Add(new LiteralControl("</td></tr></table>"));
}
}


I'm sure there's a better way. If I figure it out I'll share the results.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close