Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Please help me with this, I have 3 textboxes namely txtLastname,txtFirstname and txtUsername I want my txtUsername to automatically be filled after supplying data for txtLastname and txtFirstname the format should be txtLastname_txtFirstname(The first two letters in txtFirstname), here's the code I'm using:

aspx:

XML
<asp:TextBox ID="txtUsername" runat="server" Width="250px"
            ontextchanged="txtUsername_TextChanged" AutoPostBack="true"></asp:TextBox>


cs file:

C#
protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        txtUsername.Text = txtLastName.Text + "_" + txtFirstName.Text;
    }
Posted

It should be like follows:
C#
<asp:textbox id="txtFirstname" runat="server" ></asp:textbox>

<asp:textbox id="txtLastname" runat="server" autopostback="true" ontextchanged="txtLastname_TextChanged" ></asp:textbox>

<asp:textbox id="txtUsername" runat="server" width="250px" ></asp:textbox>

C#
protected void txtLastname_TextChanged(object sender, EventArgs e)
{
     txtUsername.Text = txtLastName.Text + "_" + txtFirstName.Text;
}

Regards..:)
 
Share this answer
 
v4
Comments
[no name] 11-Sep-13 8:46am    
5'ed..
Thanks7872 11-Sep-13 8:47am    
Thanks. :)
If you want textbox for username to be changed automatically on changing the value of the txtLastname, txtFirstname than you should create ontextchanged event for txtLastname, txtFirstname instead of txtUsername.

See below code :

ASPX Code :

XML
<asp:TextBox ID="txtLastname" runat="server" Width="250px"
            ontextchanged="txtName_TextChanged" AutoPostBack="true"></asp:TextBox>

    <asp:TextBox ID="txtFirstname" runat="server" Width="250px"
            ontextchanged="txtName_TextChanged" AutoPostBack="true"></asp:TextBox>

    <asp:TextBox ID="txtUsername" runat="server" Width="250px"
            AutoPostBack="true"></asp:TextBox>



CS Code :

C#
protected void txtName_TextChanged(object sender, EventArgs e)
        {
            this.txtUsername.Text = this.txtLastname.Text + "_" + this.txtFirstname.Text;
        }
 
Share this answer
 
Try this :
put your logic on firstname and lastname textbox text changed.
 
Share this answer
 
write the same code for txtfirstName_TextChanged Event then txtUserName will Be filled auto


first u have enter fill data for last name then first name as u remove focus from first name text box then user name text box will fill auto
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900