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

    How to display random number in textbox

    In Gridview record as follows

    Companyid Companyname
    select 1 SK
    select 2 MR

    When i click the first row select button in gridview display the record as follows
    Companyid 1 (textbox1)
    companyname SK (textbox2)

    I want to display random number in companyname in textbox2.

    for example in textbox2 i want to display random number SK99059
    for example i mentioning random number 99059.

    how to display random number in textbox2.

    for that how can i do in using c#.
  • #761812

    You just concatenate the string with random number, as like below.

    textbox2.Text="your text" + "your random number";

    hope this will helpful to you...

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

    Blog :

  • #761815

    What exactly your requirement is?
    Post correct details regarding the question to get solution?

    You want just to display text with concatenated number or you want how to generate random number.


    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #761835

    If you need random number use the method

    Random dd = new Random();
    dd.Next(0, 2);

    you can use also guid this code

    string dd= Guid.NewGuid().ToString()

    when we selected gridview concatenate in your textbox value

    for ex:

    string dd= Guid.NewGuid().ToString();
    TextBox2.Text = dd;

    If you need some length of string means you can use the split string and assign them.

    Name : Dotnet Developer-2015
    Email Id :

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

  • #761836
    You can try this code for select the row populated to textbox with random number

    Client side code

    <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="grd1_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="White" />
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="Companyid" HeaderText="Companyid" />
    <asp:BoundField DataField="Companyname" HeaderText="Companyname" />
    <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:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    Server Side

    TextBox2.Text = dd + grd1.SelectedRow.Cells[2].Text;

    In the Selected Index Changed Event you can generated random or guid and populated them.

    Name : Dotnet Developer-2015
    Email Id :

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

  • Sign In to post your comments