Get text from asp: textbox

I am writing an ASP.NET project in C #.

The UpdateUserInfo.aspx page consists of text fields and buttons. In the pageLoad () method, I set some text in the text field, and when the button rings, I get the new value of the text field and write it to the database.

The problem even is that I changed the value of the textbox textbox.Text () method returns the old value of textbox ("sometext") and writes it to the DB.

Here are the methods:

protected void Page_Load(object sender, EventArgs e) { textbox.text = "sometext"; } void Btn_Click(Object sender,EventArgs e) { String textbox_text = textbox.text();// this is still equals "somevalue", even I change the textbox value writeToDB(textbox_text); } 

So, how to make the text box appear with some value initially, but when the user changes this value, the getText method returns the new changed value and writes it to the DB?

+6
source share
4 answers
 protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { textbox.text = "sometext"; } } 

Postback sets the text property textboxs back to "somevalue" when the button is clicked, you want to set the value only once, as described above.

Feedback Explanation:

In the context of ASP web development, postback is a different name for HTTP POST. An interactive web page sends the contents of the form to the server to process some information. Subsequently, the server sends the new page back to the browser.

This is done to verify passwords for entering the system, processing an online order form, or other such tasks that the client computer cannot perform. This should not be confused with update or return button actions in the browser.

A source

Reading on View State will also be helpful in understanding how it all fits together.

+16
source

Try the following:

 If (!IsPostBack) { textbox.text = "sometext"; } 
+2
source

In fact, when the page loads, the textbox reinitialized

  protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { textbox.text = "sometext"; } } void Btn_Click(Object sender,EventArgs e) { String textbox_text = textbox.text; writeToDB(textbox_text); } 
+1
source

Please check the PostBack page on the Load Event ... page.

+1
source

Source: https://habr.com/ru/post/917624/


All Articles