如何使用 javascript 设置 asp.net 的标签文本并在服务器端获取设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23185439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set asp.net 's label 's text using javascript and get setted value on server side
提问by yangl
I have two questions about javascript and asp.net , the result i want is : make a label same change when textbox keypress, here are what i do :
我有两个关于 javascript 和 asp.net 的问题,我想要的结果是:在文本框按键时进行相同的标签更改,这是我所做的:
in asp.net page:
在asp.net页面中:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="okpress();"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="test1" />
</div>
</form>
and this is my script:
这是我的脚本:
<script type="text/javascript">
function okpress() {
//alert('Your keypress on TextBox1.');
var v1 = document.getElementById("Label1");
v1.innerHTML = document.getElementById("TextBox1").value;
}
</script>
when i test the result , i find label1 's text do change when textbox1 keypress , but it is strange that label1 's text always lack one char with textbox1 , that is to say , if i type "abcd" in textbox1, label1 only display "ab".
当我测试结果时,我发现 label1 的文本在 textbox1 按键时确实发生了变化,但奇怪的是 label1 的文本总是缺少 textbox1 的一个字符,也就是说,如果我在 textbox1 中键入“abcd”,则仅 label1显示“ab”。
another question is about the button , when i write this code in .cs file:
另一个问题是关于按钮的,当我在 .cs 文件中编写这段代码时:
protected void test1(object sender, EventArgs e)
{
string s1 = Label1.Text;
return;
}
and add a breakpoint at "return" , i find no matter whatever i type in textbox1 , the variable s1 always is "Label1"
并在“return”处添加一个断点,我发现无论我在 textbox1 中输入什么,变量 s1 始终是“Label1”
i think this two question maybe easy to experience one , but i just can not solve it, thanks for any helps.
我认为这两个问题可能很容易解决,但我无法解决它,感谢您的帮助。
回答by Jameem
You can change your Keypress
event to Keyup
event and try with the same Script..
您可以将Keypress
事件更改为Keyup
事件并尝试使用相同的脚本..
<asp:TextBox ID="TextBox1" runat="server" onKeyup="okpress();"></asp:TextBox>
And for the second question,If we set the innerHtml
of a label at client side it is difficult to access it on server side ..
对于第二个问题,如果我们innerHtml
在客户端设置标签的 ,则很难在服务器端访问它..
In the second case you can use this approach.
在第二种情况下,您可以使用这种方法。
Declare a Hidden input Control
..
声明一个Hidden input Control
..
<input type="Hidden" id="Hidden1" value="" clientidnode="Static" runat="server">
After that you can set the innerHtml
of the label to this control.
之后,您可以将innerHtml
标签的 设置为此控件。
document.getElementid('Hidden1').value=v1.innerHTML;
After that you can access the Hidden control in your server side as shown below..
之后,您可以访问服务器端的隐藏控件,如下所示..
protected void test1(object sender, EventArgs e)
{
string s1 = Hidden1.value;
return;
}