Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a test asp.net page to check how a button click inside ajax update panel behaves.
I need to update one label at a time, so that I disabled viewstate of both Labels.
But When I click any button, The Other label also reset to the default value "Label"..
How this comes ? I hope the Ajax updates only the needed controls? Isn't it ?

Here is the code

XML
<asp:Panel CssClass="Panel1" ID="Panel1" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"  >
            <ContentTemplate>
    <asp:Label ID="Label1" runat="server" Text="Label" ViewStateMode="Disabled"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Update First" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" Text="Label" ViewStateMode="Disabled"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Update Second" />
    <br />
    <br />
</ContentTemplate>
            </asp:UpdatePanel>

</asp:Panel>



C#
protected void Button1_Click(object sender, EventArgs e)
      {
          Label1.Text = DateTime.Now.ToString();
      }

      protected void Button2_Click(object sender, EventArgs e)
      {
          Label2.Text = DateTime.Now.ToString();
      }
Posted

Hi,

When a request come to ASP.Net it will create an object of that type and view state will help framework to set previous values of controls,Now in your case you have disabled view state for each label when button clicked it request to server without the value of both label and then server set respective values (as per your code) to the respective Label.

You have set your label default value to "Label" so other label set to that.

Now how to solve it you can use two different update panel
Or

You can use JQuery to achieve the same Link[^]

Read below mention links you will find your answer:

Life Cycle Of ASP.NET[^]
Ajax Update Panel[^]
 
Share this answer
 
C#
you have to use two update panel, placed one label and one button in each panel.
bcz as u put all together in one updatepanel ,so as per property of update panel it will refresh all the control present inside it.

so try what i have mentioned and let me know
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900