简体   繁体   English

根据在另一个文本框中输入的文本在文本框中显示文本

[英]Display Text in text box according to text entered in another text box

I am writing a web application in asp.net.我正在用 asp.net 编写一个 Web 应用程序。 I have two text box one field is for txtEmployeeID and the other field is for txtEmployeeName.我有两个文本框,一个字段用于 txtEmployeeID,另一个字段用于 txtEmployeeName。 I set the EmployeeName textbox to readonly in the Page_Load event.我在 Page_Load 事件中将 EmployeeName 文本框设置为只读。 In my program, I have connected to my database and set up my stored procedure.在我的程序中,我已经连接到我的数据库并设置了我的存储过程。 What I want right now is when I typed a ID in my EmployeeID textbox, the corresponding EmployeeName textbox should displayed in the employee's name in that textbox.我现在想要的是当我在我的 EmployeeID 文本框中输入一个 ID 时,相应的 EmployeeName 文本框应该显示在该文本框中的员工姓名中。

For example.例如。 In my db, I have在我的数据库中,我有

employeeID: 000123 
employeeName: Jimmy

If I type 000123 in my txtEmployeeID, the name Jimmy will show in txtEmployeeName.如果我在我的 txtEmployeeID 中输入 000123,那么名字 Jimmy 将显示在 txtEmployeeName 中。

I was thinking to use JavaScript to accomplish this.我正在考虑使用 JavaScript 来实现这一点。 This is what I was thinking but all it does is copy the text in one text box and display the same text in another textbox.这就是我的想法,但它所做的只是复制一个文本框中的文本并在另一个文本框中显示相同的文本。

     function Text() {
        var txt1 = document.getElementById('<%= txtEmployeeID.ClientID %>').value;
        document.getElementById('<%= txtEmployeeName.ClientID %>').value = txt1;

     }

HTML Code: HTML代码:

<asp:TemplateField HeaderText="Employee ID">
       <EditItemTemplate>
              <asp:TextBox ID="txtEmployeeID" runat="server" Text='<%#Bind("Employee_ID") %>'
                Width="90px"></asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
            <asp:Label ID="lblEmployeeName" runat="server" CssClass="GridInput" Text='<%#Bind("Employee_Name") %>'
                 Width="90px"></asp:Label>
        </ItemTemplate>
</asp:TemplateField>

If you want to use the OnTextChange event, you can do so like this;如果你想使用 OnTextChange 事件,你可以这样做;

<asp:UpdatePanel ID="udpEmployee" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtEmployeeID" runat="server" AutoPostBack="true" OnTextChanged="txtEmployeeID_TextChanged"></asp:TextBox>
        <asp:TextBox ID="txtEmployeeName" runat="server" ReadOnly="true"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>

And in code behind;在后面的代码中;

protected void txtEmployeeID_TextChanged(object sender, EventArgs e)
    {
        //get employee name from DB
        string employeeName = GetEmployeeName(txtEmployeeID.Text);

        //set employee name on txtEmployeeName
        txtEmployeeName.Text = employeeName;
    }

I'm not sure how you are connecting to your DB so unable to provide an example of getting the value.我不确定您是如何连接到数据库的,因此无法提供获取价值的示例。

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

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