简体   繁体   English

如何使用javascript设置asp.net的label文本并在服务器端获取设置值

[英]How to set asp.net 's label 's text using javascript and get setted value on server side

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". 当我测试结果时,我发现在textbox1按键时label1的文本确实发生了变化,但是奇怪的是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. 我认为这两个问题可能很容易遇到,但是我无法解决,谢谢您的帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中获取ASP.NET标签的文本或值 - Get ASP.NET label's text or value in JavaScript 如何在Asp.net中使用Javascript根据DropDownlist中的数据选择设置标签文本属性 - How to set Label Text property based on data selection from DropDownlist using Javascript in Asp.net 如何使用 ASP.NET 删除浏览器的缓存服务器端? - How can I remove a browser's cache server-side, using ASP.NET? 如何在AngularJS和asp.net webApi中的编辑模式下将下拉列表填充为其值和文本 - how to set a Drop down filled to it's value and text on edit mode in AngularJS and asp.net webApi 如何在ASP.NET中设置用户控件属性的值? - How to set the value of a usercontrol's property in ASP.NET? 为什么必须使用 ASP.NET 标签 asp? - Why it's necessary the label asp using ASP.NET? 在 ASP.NET 的服务器端验证 Recaptcha 2(无 CAPTCHA reCAPTCHA) - Validating Recaptcha 2 (No CAPTCHA reCAPTCHA) in ASP.NET's server side 如何使用ASP.Net中的javascript函数设置querystring值? - How to set querystring value using javascript function in ASP.Net? 每当在GridView ASP.NET中更改文本框的值时,如何更改标签的值? - How to change the value of a label whenever a textbox's value is changed in GridView ASP.NET? 如何使用c#asp.net在LinkBut​​ton的click事件中获取gridview的DataKeyNames值 - How to get DataKeyNames value of gridview inside LinkButton's click event using c# asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM