Help beginners understand ispostback

Source: Internet
Author: User

Page. ispostback is used to check whether the current webpage is loaded for the first time. When the user browses this webpage for the first time, page. ispostback returns false instead of true when the user browses this webpage for the first time!

In the page_load event, you can use ispostback to avoid repeated actions. Mainly usedSome controls perform initialization and Data BindingSometimes, if you do not use ispostback, you may find some inexplicable errors.
Here are two examples.

Example 1 (Control Initialization): The page has a tag (the initial number is 1) and a button. How can I add 1 to the number displayed when a button is clicked?

At the front end, we define an lbl1 tag and a btn1 button.

ErrorCode:

Error Code

1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 Lbl1.text = " 1 " ;
4 }
5 Protected Void Btn1_click ( Object Sender, eventargs E)
6 {
7 Lbl1.text = (Convert. toint32 (lbl1.text) + 1 ). Tostring ();
8 }

On the above code page, the initial number of lbl1 is 1, no matter how many times you click the btn1 button, the number displayed by lbl1 is always 2

Cause of error: After you click the btn1 button every time, the file executes page_load (...) and then btn1_click (...). The initial value of lbl1 obtained each time is 1 and cannot be accumulated.

Correct code:

Correct code

1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 If ( ! Ispostback)
4 {
5 Lbl1.text = " 1 " ;
6 }
7 }
8 Protected Void Btn1_click ( Object Sender, eventargs E)
9 {
10 Lbl1.text = (Convert. toint32 (lbl1.text) + 1 ). Tostring ();
11 }

Example 2 (Data Control binding): The page has a gridview control bound to the data source in the background. How can I edit, update, delete, and perform operations on data rows correctly?

There are two ways to do this:

Method 1: The commentfiele edit, update, and delete function provided by the gridview Control

Method 2: Use templatefield to customize the update and delete Functions

Here we only implement the editing function:

Method 1: front-end Page code

1 < ASP: gridview ID = " Gridview1 " Runat = " Server " Allowpaging = " True "
2 Onrowediting = " Gridviewappsrowediting " >
3 < Columns >
4 < ASP: commandfield showcancelbutton = " False " Showeditbutton = " True " />
5 </ Columns >
6 </ ASP: gridview >

Method 1 (the commentfiele editing and deletion function provided by the gridview Control) background page code

Code

Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{

}
Gridview1bind (); // This method is not included in ispostback.

}
Private Void Gridview1bind ()
{
Datatable dt = Sqlhelper. executequery ( " Select * from [products] " , Commandtype. Text );
Gridview1.datasource = DT;
Gridview1.databind ();
}
Protected Void Gridviewappsrowediting ( Object Sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
Gridview1bind ();
}

Method 2: foreground code

< ASP: gridview ID = " Gridview1 " Runat = " Server " Allowpaging = " True " Onrowediting = " Gridviewappsrowediting " >
< Columns >
< ASP: templatefield showheader = " False " >
< Itemtemplate >
< ASP: button ID = " Button1 " Runat = " Server " Commandname = " Edit " Text = " Edit " />
</ Itemtemplate >
< Edititemtemplate >
< ASP: button ID = " Button1 " Runat = " Server " Commandname = " Update " Text = " Update " /> & Nbsp; < ASP: button
ID = " Button2 " Runat = " Server " Commandname = " Cancel " Text = " Cancel " />
</ Edititemtemplate >
< Controlstyle backcolor = " # Ffc0c0 " />
</ ASP: templatefield >
</ Columns >
</ ASP: gridview >

Method 2: Background code

Code

1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 If ( ! Ispostback)
4 {
5 Gridview1bind (); // Because we are not a built-in editing function, we must put it here
6   Surface
7 }
8 }
9 Private Void Gridview1bind ()
10 {
11 Datatable dt = Sqlhelper. executequery ( " Select * from [products] " , Commandtype. Text );
12 Gridview1.datasource = DT;
13 Gridview1.databind ();
14 }
15 Protected Void Gridviewappsrowediting ( Object Sender, gridviewediteventargs E) // Trigger edit event
16   {
17 Gridview1.editindex = E. neweditindex;
18 Gridview1bind ();
19 }

Note Method 2: If gridview1bind () is not placed in ispostback, a message is returned."The callback or callback parameter is invalid."Such an error

Method 1 can not be placed in ispostback, but method 2 must be placed in it. Otherwise, an error is reported.

 
 

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.