为什么我不能让这个连接工作呢?

Why can't I get this concatenation to work

本文关键字:连接 工作 不能 为什么      更新时间:2023-09-26

我曾使用过Windows窗体应用程序,但对web开发非常陌生。我有一个问题,弄清楚如何连接2个文本框填写第三个框的文本。我在txtFirstName中输入名字,在txtLastName1中输入姓氏。我想有txtFullName与姓和名的组合填充。

代码放置在ASP内容占位符中。

这是我目前测试没有得到任何结果的ASP代码。

    <asp:TextBox ID="txtFirstName" runat="server" Width="175px" TabIndex="1"></asp:TextBox>
    <asp:TextBox ID="txtLname1" runat="server" Width="175px" TabIndex="3"></asp:TextBox>
    <asp:TextBox ID="txtFullName" runat="server" Width="268px" Text='<%#Eval("cpMainContent_txtFname") + " "+  Eval("cpMainContent_txtLname1") %>'></asp:TextBox>

然而,当我在呈现网页时在名字和姓氏文本框中输入数据时,什么也没有发生。我知道我遗漏了一些导致串联发生的东西。如有任何帮助,不胜感激。

我也尝试过这个java脚本

<script language="javascript">
function AppendValues(form)
{
   var TextBox1 = form.txtFirstName.value;
   var TextBox2 = form.txtLname1.value;
   form.txtFullName.value = Textbox1 + TextBox2;
   }
   </script>
  <INPUT type = text name="textbox1" onChange="AppendValues(this.form);">
  <INPUT type = text name="textbox2" onChange="AppendValues(this.form);">
  <INPUT type = text name="textbox3" READONLY>

Visual studios 2013抱怨在java脚本的第一行使用language=,导致代码无法编译。

假设您有ClientIDMode="Static"

改变
   var TextBox1 = form.txtFirstName.value;
   var TextBox2 = form.txtLname1.value;
   form.txtFullName.value = Textbox1 + TextBox2;

   var TextBox1 =document.getElementById('txtFirstName').value;
   var TextBox2 = document.getElementById('txtLname1').value;
   document.getElementById('txtFullName').value = Textbox1 + TextBox2;

否则,做

   var TextBox1 =document.getElementById('<%= txtFirstName.ClientID %>').value;
   var TextBox2 = document.getElementById('<%= txtLname1.ClientID %>').value;
   document.getElementById('<%= txtFullName.ClientID %>').value = Textbox1 + TextBox2;