Updating a webusercontrol on button click in asp.net c#  

by ne on 2020-12-02 under Asp.net

Update a lable control of webusercontrol through button click of a page: first of all add new webusercontrol and add this line to WebUserControl1.ascx :


<asp:Label ID="Label1" runat="server"

    onprerender="Label1_PreRender" Text="Label"></asp:Label>

and after this click on WebUserControl1.ascx.cs and add the following code:

public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        public string  Text2 { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }


        protected void Label1_PreRender(object sender, EventArgs e)
        {
            Label1.Text = Text2;
        }
    }


and now go to the WebForm1.aspx page and drag and drop the webusercontrol on this and
add a new button and double click on this paste the following code in it:

WebUserControl11.Text2 = "hello";//code what you want to show on usercontrol's label


Thanks