Thoughts on a problem

Source: Internet
Author: User

 

 

A --- Could you tell me why the text attribute of the textbox control cannot be modified during page loading (page_load.

Private void page_load (Object sender, system. eventargs E)
{
This.txt name. Text = "1234"; // The Initial Value 1234 is assigned to the text attribute of txtname of the Textbox Control.
~~~~~~~~~~~~~~~~~~~~~~~~~
}

The text value of txtname is modified after the page is generated. However, in subsequent calls, the text value of txtname is 1234.

 

 

For example, you can modify the text value of txtname to 8888 on the page and then trigger the button event,
Call
Name = this.txt name. Text. Trim ();
But the value of name is still 1234.

 

 

B --- A: page_load will be called at any time, so it must be 1234.

C --- A: Do not write in page_load.
If this is the first change, use if (! Ispostback)
{
............
}

D --- answer: If (! Ispostback)
{
This.txt name. Text = "1234 ";
}
Positive Solution

 

E ---- answer :! Page. ispostback
Otherwise, your page_load event is automatically executed every time you refresh or request the page. The page_load is different from the form_load in VB. The load in VB is only once, whereas ASP. . Net.

 

 

The clearest explanation is:

 

 

Ispostback = false when a page is displayed for the first time
Ispostback = true when you click the button or other items to submit on this page.
In general, the content in this function refers to what needs to be done when the page is opened for the first time.
Initialize something.

Suppose you define textbox as null in page_load (), and write a segment of textbox value to the data inventory in the button_click event. after you enter a value in the textbox and click the button, you can save the value entered in the textbox to the database. in fact, when you click a button, the system will automatically re-Execute page_load (), that is, clear the textbox of the handle, and then execute the click event, the value you save to the database is always empty. therefore, add If (! Ispostback ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.