Ajax Update Panel按钮单击重置所有值 [英] Ajax Update Panel Button click resets All Values

查看:47
本文介绍了Ajax Update Panel按钮单击重置所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个测试asp.net页面来检查ajax更新面板内按钮的行为方式。

我需要一次更新一个标签,以便我禁用两个标签的viewstate。

但是当我点击任何按钮时,其他标签也会重置为默认值Label..

这是怎么回事?我希望Ajax只更新所需的控件?不是吗?



这是代码



 <   asp:Panel     CssClass   =  Panel1    ID   =  Panel1 < span class =code-attribute>   runat   =  server >  
< asp:ScriptManager ID = ScriptManager1 runat = server > < / asp:ScriptManager >
< asp:UpdatePanel ID = UpdatePanel1 runat = 服务器 >
< ContentTemplate >
< asp:标签 ID = Label1 runat = server 文字 = 标签 ViewStateMode = 已停用 > < / asp:Label >
< br / >
< br / >
< asp:按钮 ID = Button1 runat = server OnClick = Button1_Click 文本 = 首先更新 < span class =code-attribute> / >
< br / >
< br / >
< asp:Label ID = Label2 runat = 服务器 文本 = 标签 ViewStateMode = 已禁用 > < / asp:Label >
< br / >
< br / >
< asp:Button ID = Button2 runat = 服务器 OnClick = Button2_Click 文字 = 更新第二个 / >
< br / >
< br / >

< / ContentTemplate >
< / asp: UpdatePanel >

< / asp:Panel >







 受保护  void  Button1_Click(  object  sender,EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}

受保护 void Button2_Click( object sender,EventArgs e)
{
Label2.Text = DateTime.Now.ToString();
}

解决方案





请求时来到ASP.Net它将创建该类型的对象,并且视图状态将帮助框架设置控件的先前值,现在在您的情况下,当按钮单击它时,您已禁用每个标签的视图状态请求服务器而没有两者的值标签然后服务器将相应的值(根据您的代码)设置为相应的标签。



您已将标签默认值设置为标签,因此其他标签设置为那个。



现在如何解决它你可以使用两个不同的更新面板





您可以使用JQuery实现相同的链接 [ ^ ]



阅读下面提到的链接,你会找到你的答案:



ASP.NET的生命周期 [ ^ ]

Ajax更新面板 [ ^ ]


您必须使用两个更新面板,在每个面板中放置一个标签和一个按钮
bcz 你把所有放在一个更新面板中,所以 as 每个属性更新面板,它将刷新其中的所有控件。

所以尝试我提到的内容和我知道


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

<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>




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();
      }

解决方案

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[^]


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


这篇关于Ajax Update Panel按钮单击重置所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆