When you use usercontrols in your form its very easy to get that user control value in your form.
I am taking one example you are creating one Usercontrol that have two textbox with User Id and Password.
And this usercontrols you can use in your form either Load that control run time or Drag that control in your form design time.
1) Create one usercontrol name: Test.ascx
That have one panel on design time name: Panel1
Call this method (AddControls) in Page_Load() event in your
UserContorl Page (Test.ascx)
public void AddControls()
{
Label lblUserName;
Label lblPassword;
TextBox txtUserName;
TextBox txtPassword;
lblUserName = new Label();
lblUserName.Text ="User Name : ";
txtUserName = new TextBox();
txtUserName.ID="txtUserId1";
lblPassword = new Label();
lblPassword.Text="Password : ";
txtPassword = new TextBox();
txtPassword.ID="txtPassword1";
txtPassword.TextMode =
System.Web.UI.WebControls.
TextBoxMode.Password;
Panel1.Controls.Add(lblUserName);
Panel1.Controls.Add(txtUserName);
Panel1.Controls.Add(lblPassword);
Panel1.Controls.Add(txtPassword);
}
2) Create one form name: TestUserControls.aspx now
There are two ways to use this usercontrol in your form(TestUserControls.aspx)
a) Drag this usercontrol in your form
<%@ Register TagPrefix="uc1" TagName="Test" Src="Test.ascx" %>
OR
b) Load this user control in Page_Load() event in your form
(TestUserControls.aspx) where you are using this control.
Load user control at Run time
private void Page_Load(object sender, System.EventArgs e)
{
private UserControl LevelControl;
LevelControl = (UserControl) LoadControl("Test.ascx");
LevelControl.ID =" MyUserControl";
LevelControl.EnableViewState = true;
Type LevelControlType = LevelControl.GetType();
Panel1.Controls.Add(LevelControl);
}
3) In your Form (TestUserControls.aspx) put one button name Button1
And print that user control textboxes value in one label control
This is the way to get UserContol value in your page.
private void Button1_Click(object sender, System.EventArgs e)
{
string userId;
string password;
userId = ((TextBox)Page.FindControl("MyUserControl")
.FindControl("txtUserId1")). Text.ToString();
password = ((TextBox)Page.FindControl("MyUserControl")
.FindControl("txtPassword1")).Text.ToString();
Label1.Text= "Welcome : " + userId + " Your Password id : " + password;
}
The Output will be :
"Welcome : ritesh your Password : kesharwani"
Ritesh Kumar Kesharwani
A D I T I , B A N G A L O R E
Software Professional
Cell : 91-9845657636
Page: http://www.riteshk.blogspot.com/