Wednesday, October 12, 2005

How to call user controls value (like Textbox, button, checkbox etc.) in aspx page.

How to call user controls value (like Textbox, button, checkbox etc.) in aspx page.

When you use usercontrols in your form it’s 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();
lblPassword = new Label();
lblPassword.Text="Password : ";
txtPassword = new TextBox();
txtPassword.TextMode =


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" %>


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();

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")

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


Siva said...

Hey, This is really great . nice one.
I have a mcse training site. It pretty much covers mcse training related stuff.
Come and check it out if you get time.. Thanks.

louisfisher7048 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. My blog is just about my day to day life, as a park ranger. So please Click Here To Read My Blog

lucymartinez8897 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. So please Click Here To Read My Blog

Jagai Nitai Dasa said...

thanks! this saved me!

Giri said...

Hi, I'm doing a project wherein I have a similar kind of user control. I'm getting "Null Reference exception". I tried using your code and I'm getting same error. The problem is, as you click on the button, the control goes to page_load. Once it goes to page_load the user control is unloaded. So I'm getting the error "Null Reference Exception, Object Reference not set to an instance of the object." Please help, I'm struck at this.