UserControl: don't get usercontrol data from .ascx

I am new to usercontrol

and wanted to explore his application. I have implemented my user control in web form

.

I want to get data to my web page from a custom control, but I don't understand, I am having to face some error NULLRefrence

regarding the controls of the custom control. Here is my code for .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; }
    }

      

Are there any problems with my properties get

and set

please help me to know about the error.

+3


source to share


1 answer


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

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

      

And than render the control, you need to write this



<MyCtrl:MyControl runat="server" />

      

This will render the control wherever you write it.

+1


source







All Articles