asp.net:使用javascript设置标签值,但它不';救不了
这是我的代码:asp.net:使用javascript设置标签值,但它不';救不了,asp.net,Asp.net,这是我的代码: <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <input type="button" onclick="ontextchange();" /> <asp:Button ID="Button1" runat="server"
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<input type="button" onclick="ontextchange();" />
<asp:Button ID="Button1" runat="server"
Text="Button" onclick="Button1_Click" />
</div>
问题是:我可以通过javascript更改标签值,但当我单击按钮1时,标签值成为第一个“标签”。单击asp.net按钮时如何获取新值?您可以尝试使用隐藏字段,但需要在客户端脚本和服务器端事件处理程序中保持这些字段的同步
<asp:Hidden ID="Hidden1" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
服务器端您可以读取隐藏的输入并更新标签(同样,保持同步):
您可以尝试使用隐藏字段,但需要在客户端脚本和服务器端事件处理程序中保持这些字段的同步
<asp:Hidden ID="Hidden1" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
服务器端您可以读取隐藏的输入并更新标签(同样,保持同步):
你不能。当您单击按钮时,它将表单发布到服务器,服务器将提供一个新的HTML页面。标签不会被张贴。如果要将值发布到服务器,则需要将其存储在输入字段中,但不能。当您单击按钮时,它将表单发布到服务器,服务器将提供一个新的HTML页面。标签不会被张贴。如果希望将值发布到服务器,则需要将其存储在输入字段中。
function ontextchange() {
// Set the label for the visual result
document.getElementById("Label1").innerText="New";
// Set the hidden input for the server
document.getElementById("Hidden1").value="New";
}
protected void Button1_Click(object sender, EventArgs e)
{
// Set the label text to the value from the hidden input
string value = Hidden1.Value;
Label1.Text = value;
}