Muhammad Iqbal Muhammad Iqbal - 4 months ago 32
ASP.NET (C#) Question

Get Set values of TextBox Control from server side

Let say I have this code on Page load of ASP.NET Webform

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "123";
}


and this is my control in aspx file

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


If I change the textbox data from 123 to 12548 or any thing in the front end and then I click the button
Now in this is my code behind button click event

protected void Button1_Click(object sender, EventArgs e)
{
string s = TextBox1.Text;
}


Now in
TextBox1.Text
I should be getting 12548 or updated value instead I am getting 123 which I have already set in page load.

Now I want to get the updated value, how may I do it the right way.

Answer

Wrap it in a NOT is Postback

if(!IsPostBack)
{
  TextBox1.Text = "123";
}

or remove it completely:

protected void Page_Load(object sender, EventArgs e)
{
  //not here
}

<asp:TextBox ID="TextBox1" Text="123" runat="server"></asp:TextBox>
Comments