Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have a textbox i want that automatically this textbox will generate autogenerated int value in it..how it can done please help me thnx in advance

I have a textbox. I want that this textbox should contain a autogenerated (random )'int' value in it (on page load). How can it be done?
Posted
Updated 19-Jul-12 20:51pm
v2
Comments
bbirajdar 20-Jul-12 2:48am    
please use proper capitalization and fulll stops in your question wherever necessary....

1 solution

Following code can help you in starting. You can use it and make the required changes. Hope it helps you.
In the ASPX file, set the textbox property to AutoPostBack to true.

ASP.NET
<![CDATA[<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="TestingATextBox._Default" Culture="Auto" %>]]> 
 
<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown"> 
</asp:content> 
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown"> 
    <table> 
        <tr> 
            <td> 
                <asp:textbox runat="server" id="TextBox1" autopostback="true"> 
                </asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td> 
                <asp:button runat="server" id="SaveBtn" text="Save" onclick="SaveBtn_Click" /> 
            </td> 
        </tr> 
    </table> 
</asp:content>


In the codebehind, on Save button click, increment the value.

C#
public partial class _Default : System.Web.UI.Page 
    { 
        static int i = 1; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            TextBox1.Text = i.ToString(); 
        } 
 
        protected void SaveBtn_Click(object sender, EventArgs e) 
        { 
            i++; 
        } 
    }
 
Share this answer
 
Comments
bbirajdar 20-Jul-12 2:53am    
Every time the button is clicked, it will insert the same number '1' into the textbox. Right ? This is equivalent to <asp:textbox runat="server" id="TextBox1" Text="1" autopostback="true">

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