You must Sign In to post a response.
  • Category: ASP.NET

    How to display the first and last letter word in textbox

    Company table as follows

    Compid Compname
    1 Mask

    I have one textbox as follows
    Company_username textbox1

    I have gridview as follows
    In gridview i show record from company table and display into gridview

    Compid Compname
    select 1 Mask

    I want when i click the select in gridview i want to show companame first letter M and last letter K to be displayed in the textbox1.

    In the textbox1 i want output as follows

    Company_username Mk(textbox1)

    from that how can i do in asp.net using c#
  • #761807
    You can try the following code

    string Company_username = "Mask";
    TextBox1.Text = Company_username.Substring(0, 1) + Company_username.Substring(Company_username.Length - 1, 1) + TextBox1.Text;

    By Nathan
    Direction is important than speed

  • #761808
    Hi,

    If you want to know the first and last character of a string then use below sample code.

    string result="hello how are you";
    string fstchar=result.substring(0);
    string lstchar=result.substring(result.Length-1);
    Textbox1.Text=fstchar+""+lstchar;


    Try like above to achieve your goal..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #761842
    Hi

    You can fixed this issue using substring means try this code

    client side

    <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="grd1_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="Companyid" HeaderText="Companyid" />
    <asp:BoundField DataField="Companyname" HeaderText="Companyname" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>


    Server Side

    protected void grd1_SelectedIndexChanged(object sender, EventArgs e)
    {
    string a =grd1.SelectedRow.Cells[2].Text.ToString();
    string ffg = a.Substring(0, 1);
    string ffg1 = a.Substring(a.Length - 1, 1);
    string totsttring = ffg + ffg1;

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761843
    Hi

    If you need without substring solved this issue means
    try this code

    Client Side

    <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="grd1_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="Companyid" HeaderText="Companyid" />
    <asp:BoundField DataField="Companyname" HeaderText="Companyname" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>


    Server Side


    string ff="";
    string gg="";
    for (int i = 0; i<=a.Length-1; i++)
    {
    if (i == 0)
    {
    ff = ff + a[i];
    }

    if (i == a.Length - 1)
    {
    gg = gg + a[i];
    }
    }

    string totstr = ff + gg;

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments