0

i want to add a label to update panel, on button click.. i have following code in .aspx file...

 <asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" >
                   <ContentTemplate >
                       <asp:Panel runat="server" ID="myPanel" >
                        <label id="ssd" runat="server" >abc</label>
                       </asp:Panel>
                       <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"  Text="test"/>
                   </ContentTemplate>
               </asp:UpdatePanel>

and on button click event..

protected void Button1_Click(object sender, EventArgs e)
{
    Label l1 = new Label();
    l1.ID = "label1";
    l1.Text = "this is it...";
    up.ContentTemplateContainer.Controls.Add(l1);      
}

but its not working ..:-(

1 Answer 1

1

Since the UpdatePanel's UpdateMode is set to Conditional, you need to update it manually from codebehind:

protected void Button1_Click(object sender, EventArgs e)
{
    Label l1 = new Label();
    l1.ID = "label1";
    l1.Text = "this is it...";
    up.ContentTemplateContainer.Controls.Add(l1); 

    up.Update();     
}

MSDN UpdatePanel.Update Method

If you plan to use the Update method, set the UpdateMode property to Conditional. If you want the decision to update the panel in server logic, make sure that the ChildrenAsTriggers property is false and that no explicit triggers are defined for the panel.

In a typical page development scenario, if you define triggers or if the ChildrenAsTriggers property is true for the UpdatePanel control, the Update method is automatically called during the page life cycle.

Note that you need to recreate dynamical controls even with ASP.NET Ajax. So you need to create the label manually on the next postback in page_load at the latest.

Sign up to request clarification or add additional context in comments.

1 Comment

protected void Page_Load(object sender, EventArgs e) { Label l1 = new Label(); l1.ID = "label1"; l1.Text = "this is it..."; this.form1.Controls.Add(l1); } even this is not adding label to my page :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.