Friday, March 31, 2006
More on devlist
Thursday, March 09, 2006
Using OOP to pass variables to ASP.NET user controls
Below is a simple example of using OOP to pass variables into ASP.NET user controls then creating the controls on post back. This code is currently used in http://www.devlist.com/.
namespace DevList.Lists{using System;using System.Data;using System.Drawing;using System.Web;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Web.UI;
/// <summary>/// Summary description for ShowList./// </summary>public class ShowList : System.Web.UI.UserControl{ protected System.Web.UI.WebControls.Label lblTitle; protected System.Web.UI.WebControls.Label lblTable; protected System.Web.UI.WebControls.Label lblDescription;
private string _Title; private string _Description; protected System.Web.UI.WebControls.Label lblPrintButton; private string _List; private string _TableName;
public string Title { get { return _Title; } set { _Title = value; } }
public string Description { get { return _Description; } set { _Description = value; } }
public string List { get { return _List; } set { _List = value; } }
public string TableName { get { return _TableName; } set { _TableName = value; } }
private void Page_Load(object sender, System.EventArgs e) {
lblTitle.Text = _Title.ToString(); lblDescription.Text = _Description.ToString();
string szPrintHeader = _Title.ToString() + " - " + _Description.ToString(); Control cControlHolderPrintButton = this.FindControl("lblPrintButton"); //string szPrintButtonHTML = "<INPUT onclick=\"ShowTableInDocument('" + _TableName + "','" + szPrintHeader + "');\" type=\"image\" src=\"Images/printer.gif\" value=\"Print\"></td>"; string szPrintButtonHTML = "<INPUT onclick=\"ShowTableInDocument('" + _TableName + "','" + szPrintHeader + "');\" type=\"button\" value=\"Print\"></td>"; cControlHolderPrintButton.Controls.Add(new LiteralControl( szPrintButtonHTML ));
Control cControlHolder = this.FindControl("lblTable"); Control c2 = Page.LoadControl(_List); cControlHolder.Controls.Add(c2);
}
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load);
} #endregion}}The object creation and enbedding is pretty simple:
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;
namespace DevList.Lists{////// Summary description for WebForm1./// public class CSharp : System.Web.UI.Page{protected System.Web.UI.WebControls.Label lblControlHolder;protected System.Web.UI.HtmlControls.HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e){// Put user code to initialize the page hereBuildTables(); //Leave in}
private void BuildTables(){Control cControlHolder = Page.FindControl("lblControlHolder");
ShowList slCSharpKeyWords = (ShowList) Page.LoadControl("Lists/ShowList.ascx");slCSharpKeyWords.Title = "C# Key Words Table";slCSharpKeyWords.Description = "Table of C# keywords and Descriptions";slCSharpKeyWords.List = "Lists/CSharpKeyWords.ascx";slCSharpKeyWords.TableName = "CSharpKeyWordsTable"; //Must match table name. Will not show print screen correctly if not.cControlHolder.Controls.Add(slCSharpKeyWords);
}
#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN: This call is required by the ASP.NET Web Form Designer.//InitializeComponent();base.OnInit(e);
}
////// Required method for Designer support - do not modify/// the contents of this method with the code editor./// private void InitializeComponent(){this.Load += new System.EventHandler(this.Page_Load);}#endregion}}