5

ASP.Net 标签的原始值为“xyz”。

我已将 ASP.Net 标签值更改如下:

$("#<%= lblNew.ClientID %>").text("123");

它在网页上发生了变化。但是当我单击按钮并获取标签的值时,它会变回以前的值“xyz”而不是“123”。

Response.Write(lblNew.Text);

我试图设置标签的 html 而不是下面的文本:但它也不起作用。

$("#<%= lblNew.ClientID %>").html("123");

如何获取 Jquery 更改的值?谢谢。

4

4 回答 4

7

This is because label text value is loaded from view state.Your jquery change the value of label but didn't change view state where it value is being loaded on postback.... But you want the change label text..so you can get it like this.......

string lblvalue=Request[lblNew.UniqueID] as string;

Here is and example to understand how view state work with label...refrence MSDN

<asp:Label runat="server" ID="lblMessage" 
  Font-Name="Verdana" Text="Hello, World!"></asp:Label>
<br />
<asp:Button runat="server" 
  Text="Change Message" ID="btnSubmit"></asp:Button>
<br />
<asp:Button runat="server" Text="Empty Postback"></asp:Button>
And the code-behind class contains the following event handler for the Button's Click event:
private void btnSubmit_Click(object sender, EventArgs e)
{
  lblMessage.Text = "Goodbye, Everyone!";
}

illustrates the sequence of events that transpire, highlighting why the change to the Label's Text property needs to be stored in the view state. enter image description here

于 2013-06-26T10:46:03.253 回答
3

解决您面临的问题是使用隐藏字段并将其值与标签字段一起更新,当回发发生时,您可以从隐藏字段中读取更新的值

于 2013-06-26T10:51:13.700 回答
1

You can't alter ASP.Net Labels in Javascript/JQuery and expect them to persist on postback.

Only values from input controls (like textboxes, radio controls, radiobuttons, etc...) are posted to the server.

于 2013-06-26T10:46:11.740 回答
1

试试.val()

$("#<%= lblNew.ClientID %>").val("123");

文档可以在这里找到:http: //api.jquery.com/val/#val-value

编辑:

我误读了你的问题,.text应该没问题。我认为这个问题更多地与回发有关。你能确认代码$("#<%= lblNew.ClientID %>").text("123");是如何调用的吗?如果开启$(document).ready()了应该没问题。但这是在进行回发/重新加载 DOM 时会丢失的操作的结果。

于 2013-06-26T10:41:14.457 回答