Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing this keyword as a parameter

Status
Not open for further replies.

TiltingCode

Programmer
Apr 10, 2009
61
0
0
US
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top