Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>




cs


C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string user;
        user = TextBox1.Text.Substring(0, 2);
        TextBox2.Text = user;
    }
}
Posted
Comments
♥…ЯҠ…♥ 24-Sep-12 5:26am    
Is it working as expected or not? what is your question?
CodeHawkz 24-Sep-12 5:53am    
Just a quick tweak on your Button1_Click code

if (!string.IsNullOrWhiteSpace(TextBox1.Text))
TextBox2.Text = TextBox1.Text.Substring(0, 2);

1 solution

while using string.substring, don't forget to check whether the string is null or empty, orelse exception will occur.
 
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