Reputation: 1733
I am writing a web application in asp.net. I have two text box one field is for txtEmployeeID and the other field is for txtEmployeeName. I set the EmployeeName textbox to readonly in the Page_Load event. 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.
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.
I was thinking to use JavaScript to accomplish this. 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:
<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>
Upvotes: 2
Views: 1650
Reputation: 940
If you want to use the OnTextChange event, you can do so like this;
<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.
Upvotes: 2