0

I have the following issue: I create a TextBox dynamically in my web page, its value is "initialVal" in the beginning. Now I need to make a postback (not callback) to the server, and during this operation, I need to compute/change the value of my textbox to other value.

Here's an example:

protected void Page_Load(object sender, EventArgs e)
    {
        TextBox txtBox = new TextBox();
        txtBox.ID = "newButton";
        form1.Controls.Add(txtBox);
        txtBox.Text = "initialVal";

        if (IsPostBack && Session["change"] == null)
        {
            txtBox.Text = "change";
            Session["change"] = true;
        }
    }

The problem: even if I change the value via code, the textbox will keep the text "initialVal". I feel this is something related to the view state, but I don't understand. Coudl anyone please help me here?

Thanks.

1
  • 1
    Better if you create it in Init event. Commented Feb 2, 2016 at 12:45

4 Answers 4

1

Everytime you load the page it is running this:

txtBox.Text = "initialVal";

You should wrap this in a check for postback:

if (!Page.IsPostback)
{
    txtBox.Text = "initialVal";
}

That said, onLoad is the wrong event to do the creation, for it to be valid in the early enough in the page lifecycle, use OnInit.
See this article on MSDN.


Here is the final code from @user2890888:

public partial class WebForm1 : System.Web.UI.Page 
{ 
  TextBox txtBox = null; 

  protected void Page_Init(object sender, EventArgs e) 
  { 
    txtBox = new TextBox(); 
    txtBox.ID = "newButton"; 
    form1.Controls.Add(txtBox); 
  } 

  protected void Page_Load(object sender, EventArgs e) 
  { 
     if (!IsPostBack) 
     { 
       txtBox.Text = "initialVal"; 
     } 

     if (IsPostBack && Session["change"] == null) 
     { 
       txtBox.Text = "change"; 
       Session["change"] = true; 
      } 
   } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Nikolai, I will put the solution in the Question body.
@user2890888 I'll edit the answer and add it in so it is readable, if thats ok.
1

Create your dynamic textbox creation in !IsPostBack

    protected void Page_Load(object sender, EventArgs e)
        {
if(!isPostBack){
            TextBox txtBox = new TextBox();
            txtBox.ID = "newButton";
            form1.Controls.Add(txtBox);
            txtBox.Text = "initialVal";
    }
            if (IsPostBack && Session["change"] == null)
            {
                txtBox.Text = "change";
                Session["change"] = true;
            }
        }

Thanks and let me know if your issue still pending

1 Comment

nope, first of all the txtBox will become a local variable inside the if, code will not compile. Second, even if I get out of "if" the txtBox, it will be null. The TextBox must be created all the time, no only on !IsPostBack.
0

Find the TextBox and use it

TextBox txtBox = (TextBox)FindControl("txtBox");
txtBox.Text = "change";

Comments

0

Even now you are having issue and your textBox is not getting the value you needed. Store your new value for TEXTBOX in a hiddenfield. I mean ,

 if (IsPostBack && Session["change"] == null)
{
   hiddenfield1.value = "change";    
}

and later in your page script, you can assign back the value in this hiddenfield1 to textbox.

$(document).ready(
{
$('#txtBoxID').value=$('#hiddenfield1').value;
}
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.