Neha
Neha

Reputation: 49

UserControl : Not getting usercontrol's data from .ascx

I'm new to usercontrol and wanted to learn its application. I rendered my user control in a web form.

I want to get data to my web page from user control but I'm not getting it, I am facing some NULLRefrence error regarding user control controls. Here is my code for both .ascx and .aspx

 ascx.cs

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private string userName;
    private int userAge;
    private string userCountry;

    public string UserName
    {
        get { return txt_name.Text; }
        set { txt_name.Text = value; }
    }

    public int UserAge
    {
        get { return  userAge= Convert.ToInt32(txt_age.Text); }
        set {      userAge = value; }
    }

    public string UserCountry
    {
        get { return ddl_country.Text; }
        set { ddl_country.Text = value; }
    }

Is there is any problem with my get and set property, please help me to find out the error.

Upvotes: 3

Views: 135

Answers (1)

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

You can use user control by registering user control on the page where you want to use it like this

<%@ Register TagName="MyControl" TagPrefix="MyCtrl" Src="~/UserControls/MyUserControl.ascx" %>

And than to render the control you have to write this

<MyCtrl:MyControl runat="server" />

This will render the control on the place where you will write this.

Upvotes: 1

Related Questions