To create a user control. Right click on Project->Add New Item-> User Control. Name it, say, myUserControl
A page with .ascx extension is control. Use it to place your controls.
To add it to ur page. Go to your webpage and open it in design mode. Drag and drop the usercontrol on it. When you got to Source view you will see that a new <%Register ... %> attribute is added to your code. And a new control is added like
<uc1:myUserControl ID="myUserControl1" runat="server" />
You can access it in code using its ID like other server controls.
To send data to user control from your page
TextBox txtuc1 = (TextBox)myUserControl1.FindControl("TextBox1");
txtuc1.Text = "I am user control";
To send data from user control to page
use Response.Redirect and pass data in querystring, just like you do redirect normally.
A page with .ascx extension is control. Use it to place your controls.
To add it to ur page. Go to your webpage and open it in design mode. Drag and drop the usercontrol on it. When you got to Source view you will see that a new <%Register ... %> attribute is added to your code. And a new control is added like
<uc1:myUserControl ID="myUserControl1" runat="server" />
You can access it in code using its ID like other server controls.
To send data to user control from your page
TextBox txtuc1 = (TextBox)myUserControl1.FindControl("TextBox1");
txtuc1.Text = "I am user control";
To send data from user control to page
use Response.Redirect and pass data in querystring, just like you do redirect normally.
No comments:
Post a Comment