Solved user control

Posted on 2005-04-14
Last Modified: 2010-04-07

Suppose my 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).

Question by:kamleshmistry
    LVL 28

    Assisted Solution


    LVL 18

    Accepted Solution

    Check 3 methods that you can use to pass values between ASPX and ASCX pages:

    [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


    3) In ASPX code behind file

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now