Updating Label inside UpdatePanel

2019-09-01 02:58发布

问题:

i have a label and button inside update panel, when i try to get the value from the label on the button click, i get the value from label, but when i try to set the value to the label, it does not happens, i checked for JavaScript error, but there wasn't any, does anyone have any guess what could be the reason. i am using dotnetnuke and here is my code

<asp:UpdatePanel ID="updSection6" runat="server"><ContentTemplate>
<asp:Label ID="lbl" runat="server" />
<asp:ImageButton ImageUrl="/images/edit.gif" ID="btnEditSectionStory6" runat="server" OnClick="Clicked" />
</ContentTemplate></asp:UpdatePanel>

and here's the code

protected void Clicked(object sender, EventArgs e)
{
   lbl.Text="Welcome";
}

回答1:

You need to add the following code

<Triggers>
    <asp:PostBackTrigger ControlID="btnEditSectionStory6" />
</Triggers>

Just before your closing </asp:UpdatePanel>

So your code should look like:

<asp:UpdatePanel ID="updSection6" runat="server">
    <ContentTemplate>
        <asp:Label ID="lbl" runat="server" />
        <asp:ImageButton ImageUrl="/images/edit.gif" ID="btnEditSectionStory6" runat="server" OnClick="Clicked" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnEditSectionStory6" />
    </Triggers>
</asp:UpdatePanel>

ASP PostBackTrigger

Specifies a control and event that will cause a full page update (a full page refresh). This tag can be used to force a full refresh when a control would otherwise trigger partial rendering.

You can read more about UpdatePanel's and Triggers here.

C# (using the ImageClickEventArgs)

    protected void Clicked(object sender, ImageClickEventArgs e)
    {
        lbl.Text = "Welcome";
    }