0

my concern is about post-back. I have 3 textboxes, the last name, first name and username. I want to combine last name and first name to create a username have lastname.firstname format. My code in .aspx is as follows. The post-back is true for first name because after the user enters the first name, it will automatically generate the username.

<strong>LAST NAME: <span style="color: #FF0000">*</span> </strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <asp:TextBox ID="txtLname" runat="server"
    Width="197px" onkeypress="return Alphabet()" BorderStyle="Solid"></asp:TextBox>
<strong>FIRST NAME: <span style="color: #FF0000">*</span> </strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;    <asp:TextBox ID="txtFname" runat="server" Width="199px" 
    onkeypress="return Alphabet()" AutoPostBack="True" 
    ontextchanged="txtFname_TextChanged" BorderStyle="Solid"></asp:TextBox>
<strong>USERNAME: </strong>&nbsp;<asp:TextBox 
ID="txtUser" runat="server" 
    Width="225px" ReadOnly="True" style="margin-left: 6px" BorderStyle="Solid" onkeypress="return Alphabet()"></asp:TextBox> 

My code in .aspx.cs is as follows.

protected void txtFname_TextChanged(object sender, EventArgs e)
    {
        string mystring = txtFname.Text;
        mystring = Regex.Replace(mystring, @"\s+", "");

        txtUser.Text = txtLname.Text + "." + mystring;
    }

this is to trigger the automatic generation of username.

To sum it up, I want to know how to stop post-back from going back to the top of the page after entering the first name for automatic generation of username. thank you very much.

Edge
  • 31
  • 2
  • 11
  • I would do this in javascript. – TLJ Nov 22 '15 at 01:13
  • Can you give a code snippet? – Edge Nov 22 '15 at 01:17
  • See this. http://stackoverflow.com/questions/25811275/maintainscrollpositiononpostback-true-does-not-work-with-google-chrome As TLJ stated you should use java-script or J Query for that. It's not worth the post back. – Moe Nov 22 '15 at 01:19

1 Answers1

0

Adjust as you need:

<html>
<head></head>
<body>
    <form action="">
        Last Name :<input  id="txtLastName" name="txtLastName" type="text" oninput="updateUserName()"></input><br />
        First Name:<input id="txtFirstName" name="txtLastName" type="text" oninput="updateUserName()"></input><br />
        Username:<input id="txtUserName" name="txtLastName" type="text" readonly></input><br />
        <button type="submit">Submit</button>
    </form>
    <script>
        function updateUserName()
        {
            var last_name = document.getElementById("txtLastName").value;
            var first_name = document.getElementById("txtFirstName").value;
            if(last_name !== "" && first_name !== "")
                document.getElementById("txtUserName").value =  last_name+"."+first_name;
        }
    </script>
</body>

</html>
TLJ
  • 4,525
  • 2
  • 31
  • 46