简体   繁体   English

如何在asp.net C#中将文本框文本更改为另一个文本框?

[英]How to Change textbox text into another textbox in asp.net C#?

I am having two text boxes on my web page as txtGivenName and txtDisplayName. 我的网页上有两个文本框,分别是txtGivenName和txtDisplayName。 Now I want to display txtGivenName text on txtDisplayName while entering text on txtGivenName by using keypress event or keydown event. 现在,我想通过使用keypress事件或keydown事件在txtGivenName上输入文本时,在txtDisplayName上显示txtGivenName文本。 I have tried some code for achieve this but not able to get the textbox1 value on textbox2. 我已经尝试了一些代码来实现此目的,但无法获取textbox2上的textbox1值。

My Code is: 我的代码是:

<asp:TextBox ID="txtGivenName" runat="server" onkeypress = "copyText()"  ></asp:TextBox>

Script code is: 脚本代码为:

function copyText() {

var givenName = document.getElementById("txtGivenName");
var displayName = document.getElementById("txtDisplayName");
displayName =  givenName;

} 

but nothing happened while entering text on txtGivenName. 但是在txtGivenName上输入文本时没有任何反应。 Can anyone suggest me to achieve this or tell me any other way to achieve this? 谁能建议我实现这一目标,或告诉我其他实现此目标的方法? (Note: I dont want to do this by OnTextChanged event, Bcoz it will populate the first text box value on second one after text enter got over only. While entering the text on first textbox we need to change the text on second textbox simultanously) (注意:我不想通过OnTextChanged事件来执行此操作,Bcoz仅在输入文本结束后才填充第二个文本框的第一个文本框值。在第一个文本框上输入文本时,我们需要同时更改第二个文本框上的文本)

Thanks. 谢谢。

I got the exact answer for this feature by using below mentioned script code. 通过使用下面提到的脚本代码,我获得了此功能的确切答案。 Solution: 解:

function OneTextToOther() {

        var first = document.getElementById('<%= txtGivenName.ClientID %>').value;

        document.getElementById('<%= txtDisplayName.ClientID %>').value = first;

    }  

Take a look at the page source in the browser -- "txtGivenName" and "txtDisplayName" are not going to be the IDs of the text boxes because ASP.NET prepends the IDs based on the control hierarchy so they are globally unique. 看一下浏览器中的页面源-“ txtGivenName”和“ txtDisplayName”将不再是文本框的ID,因为ASP.NET根据控件层次结构添加ID,因此它们在全局范围内是唯一的。

You have two options -- use "<%=txtGivenName.ClientID%>" to get the true name of the text box in javascript, or set ClientIdMode="static" on the text boxes so the IDs are left alone. 您有两个选择-使用"<%=txtGivenName.ClientID%>"在javascript中获取文本框的真实名称,或在文本框上设置ClientIdMode="static"以便将ID保留下来。

function copyText() {
    var givenName = document.getElementById("<%=txtGivenName.ClientID%>");
    var displayName = document.getElementById("<%=txtDisplayName.ClientID%>");
    displayName.value =  givenName.value;
} 

You can try keyup event but I know only Jquery method to do it. 您可以尝试键入事件,但我知道只有Jquery方法可以执行此操作。

<input class="one" type="text"/>
<input class="two" type="text"/>

$(".one").on("keyup", function(){
$(".two").val($(".one").val()); 
});

here is the demo: http://jsfiddle.net/pUpZA/ 这是演示: http : //jsfiddle.net/pUpZA/

PS: the asp:textbox translates to input when you compile so its basically the same thing. PS:asp:textbox在编译时会转换为输入,因此基本相同。 use "CssClass=one" for your asp:textboxes 为您的asp:textboxes使用“ CssClass = one”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM