asp.net user control



Suppose my asp.net application is designed such that I create several user controls.

Lets say I have some user control,   PaymentMethodUserControl which I drag and drop into my startup form.

How do I reference the elements of PaymentMethodUserControl1 within the context of the startup form?

for example, I would like to do something like this within the context of the startup form (the form in which I dragged the user control):
if Me.PaymentMethodUserControl1.radioButtonCash.checked = true then....  [this logic could, for example, be in the click event of some button within the startup form]

Of course, the above does not work (not even if you change the code of PaymentMethodUserControl, such that all the radio buttons are made public instead of protected).

I get the following error:
Object reference not set to an instance of an object (PaymentMethodUserControl1).

kamleshmistryAsked:
Who is Participating?
 
tusharashahCommented:
Check 3 methods that you can use to pass values between ASPX and ASCX pages:
  http://www.c-sharpcorner.com/Code/2005/Jan/PassingValuesfrmUCtoASPX.asp

[for detail refere Link]
1) Using Properties
If the test.ascx control has two textboxes and submit button.You can access the values of the textboxes in the control from an .aspx page by declaring public property in the .ascx page.

public string FirstName
{
get{return txtFirstName.Text;}
set{txtFirstName.Text = value;}
}

2) Using Javascript

You can set the values of the controls declared in the .ascx page by

document.forms[0]['TestControl:txtFirstName'].value ="Suzzanne";

You can get the values of the controls declared in the .ascx page by

document.forms[0]['TestControl:txtFirstName'].value

3) In ASPX code behind file

TestControl objTestControl = (TestControl)Page.FindControl("TestControl");
TextBox objTextBox = objTestControl.FindControl("txtFirstName");
string strFirstName = objTextBox.Text;

-tushar
0
 
rafranciscoCommented:
Try:

((RadioButton)Me.PaymentMethodUserControl1.FindControl("radioButtonCash")).checked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.