Passing Value To a User Control From a Web Page

This article will give you abrief idea how to load a user control dynamically and pass values to it. Thiscan be also taken as a best example for the use of properties.<o:p></o:p>

<o:p> </o:p>

Step1: Create a web page- default.aspx and place a place holder in it.<o:p></o:p>

Step2: Create a usercontrol- my.ascx and place a label in it.<o:p></o:p>

Step3:Inmy.ascx.cs write the following code<o:p></o:p>

<o:p> </o:p>

public string prop //property<o:p></o:p>

{<o:p></o:p>

set { Label1.Text = value; } //write intoproperty<o:p></o:p>

}<o:p></o:p>

Step4: Pace the following tagin default.aspx<o:p></o:p>

<%@ Register Src="my.ascx" TagName="my" TagPrefix="uc1" %><o:p></o:p>

 <o:p></o:p>

Step5: Place this code in default.aspx.cs<o:p></o:p>

Protected void Page_Load(object sender, EventArgse) <o:p></o:p>

{<o:p></o:p>

if (!Page.IsPostBack) <o:p></o:p>

{<o:p></o:p>

my uc = (my)Page.LoadControl("my.ascx" ); //loading the user control dynamically<o:p></o:p>

uc.prop = "anoop"; //assign the label text a value<o:p></o:p>

PlaceHolder1.Controls.Add(uc);<o:p></o:p>

}<o:p></o:p>

Analyze the above code. Hopeit will be of great help. For any queries please contact me at Anoop_ukrishnan@yahoo.co.in<o:p></o:p>

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!