ASP. NET implements a function in the background code, and prompts the user whether to continue the operation according to the selection.

Source: Internet
Author: User

In this case, when processing an http post command in the background, the time sequence is handed over to the browser, then Javascript is run, and then the server is returned, and subsequent operations are continued. If you draw a sequence chart, you can find that there are two typesProgramIt is more complex than most programs that know only one round-trip communication (althoughCodeNot much ). The ipostbackeventhandler interface of. NET is used here.

Give a column as follows (marked in red as the key code ):

 

Protected void btnaddgift_click (Object sender, eventargs E)
{
String inventoryidstr = string. empty;
String inventoryqtystr = string. empty;
If (giftrepeater. Items. Count> 0)
{
Foreach (repeateritem item in giftrepeater. Items)
{
String inventoryidstr1 = string. empty;
String inventoryqtystr1 = string. empty;
Repeater giftsizerepeater = item. findcontrol ("selectsizerepeater") as repeater;
If (giftsizerepeater. Items. Count> 0)
{
Foreach (repeateritem Item1 in giftsizerepeater. Items)
{
Int inventoryqty = 0;
Int inventoryqty1 = 0;
Hiddenfield productquentityhiddenfield = item1.findcontrol ("productquentityhiddenfield") as hiddenfield;
Textbox inventorytextbox = item1.findcontrol ("inventorytextbox") as textbox;
Hiddenfield qtyhiddenfield = item1.findcontrol ("qtyhiddenfield") as hiddenfield;
Int. tryparse (qtyhiddenfield. Value, out inventoryqty1 );
If (! String. isnullorempty (inventorytextbox. Text ))
{
If (! Int. tryparse (inventorytextbox. Text, out inventoryqty ))
{
Messagelabel. Text = "Make sure the number of gifts you entered is in the correct format! ";
Return;
}
If (inventoryqty> inventoryqty1)
{
Messagelabel. Text = "the number of gifts you entered is greater than the current inventory. Please reselect or reduce the number of gifts you entered! ";
Return;
}
If (inventoryqty> 0)
{
Inventoryidstr1 = inventoryidstr1 + productquentityhiddenfield. Value + ",";
Inventoryqtystr1 = inventoryqtystr1 + inventoryqty + ",";
}
}
}
}
Inventoryidstr = inventoryidstr + inventoryidstr1;
Inventoryqtystr = inventoryqtystr + inventoryqtystr1;
}
}
If (! String. isnullorempty (inventoryidstr ))
{
Inventoryidstr = inventoryidstr. trimend (',');
}
If (! String. isnullorempty (inventoryqtystr ))
{
Inventoryqtystr = inventoryqtystr. trimend (',');
}
Int giftcount = giftcount * times;
Int selectcount = 0;
If (! String. isnullorempty (inventoryqtystr ))
{
String [] arrs = inventoryqtystr. Split (New String [] {","}, stringsplitoptions. removeemptyentries );
For (INT I = 0; I <arrs. length; I ++)
{
Selectcount = selectcount + Int. parse (arrs [I]. tostring ());
}
}
If (giftcount> selectcount)
{
This. Page. clientscript. registerstartupscript (this. GetType (), "Next Step ",
"If (confirm ('the number of gifts you have selected is smaller than the number of gifts given. Are you sure you only want to choose these gifts? ') "+ This. Page. clientscript. getpostbackeventreference (this," secondnext "), true );
Return;
}
If (giftcount <selectcount)
{
Messagelabel. Text = "the number of gifts you have selected cannot exceed the number of gifts you have chosen! ";
Return;
}
String result = shoppingcardmanager. addshoppingcardforpromotiongift (inventoryidstr, inventoryqtystr, userid, 2 );
If (result = "1 ")
{
Scriptmanager. registerclientscriptblock (this, this. GetType (), "script", "closewithrefreshparent ();", true );
}
Else
{
Messagelabel. Text = "operation failed. Please try again! ";
Return;
}

}

 

// This is the ipostbackeventhandler interface.Raisepostbackevent method.

My personal understanding is: this interface is a design specifically designed for the. NET (small) architecture to meet the frontend and backend changes. When. net after these objects are converted, when the client clicks a POST request, if the ipostbackeventhandler interface specifically marks getpostbackeventreference in the execution event code of the post request ,. net will return the current post time series to the client browser at the same time, but at the same time, the client will automatically execute a POST request, then the request will go to the raisepostbackevent Method for execution, and then return it to the client browser. The above only represents my personal understanding of this (small) architecture design.

Public void raisepostbackevent (string eventargument)
{
If (eventargument = "secondnext ")
{
String inventoryidstr = string. empty;
String inventoryqtystr = string. empty;
If (giftrepeater. Items. Count> 0)
{
Foreach (repeateritem item in giftrepeater. Items)
{
String inventoryidstr1 = string. empty;
String inventoryqtystr1 = string. empty;
Repeater giftsizerepeater = item. findcontrol ("selectsizerepeater") as repeater;
If (giftsizerepeater. Items. Count> 0)
{
Foreach (repeateritem Item1 in giftsizerepeater. Items)
{
Int inventoryqty = 0;
Hiddenfield productquentityhiddenfield = item1.findcontrol ("productquentityhiddenfield") as hiddenfield;
Textbox inventorytextbox = item1.findcontrol ("inventorytextbox") as textbox;
If (! String. isnullorempty (inventorytextbox. Text ))
{
If (! Int. tryparse (inventorytextbox. Text, out inventoryqty ))
{
Messagelabel. Text = "Make sure the number of gifts you entered is in the correct format! ";
Return;
}
If (inventoryqty> 0)
{
Inventoryidstr1 = inventoryidstr1 + productquentityhiddenfield. Value + ",";
Inventoryqtystr1 = inventoryqtystr1 + inventoryqty + ",";
}
}
}
}
Inventoryidstr = inventoryidstr + inventoryidstr1;
Inventoryqtystr = inventoryqtystr + inventoryqtystr1;
}
}
If (! String. isnullorempty (inventoryidstr ))
{
Inventoryidstr = inventoryidstr. trimend (',');
}
If (! String. isnullorempty (inventoryqtystr ))
{
Inventoryqtystr = inventoryqtystr. trimend (',');
Int giftcount = giftcount * times;
Int selectcount = 0;
If (! String. isnullorempty (inventoryqtystr ))
{
String [] arrs = inventoryqtystr. Split (New String [] {","}, stringsplitoptions. removeemptyentries );
For (INT I = 0; I <arrs. length; I ++)
{
Selectcount = selectcount + Int. parse (arrs [I]. tostring ());
}
}
}
String result = shoppingcardmanager. addshoppingcardforpromotiongift (inventoryidstr, inventoryqtystr, userid, 2 );
If (result = "1 ")
{
Scriptmanager. registerclientscriptblock (this, this. GetType (), "script", "closewithrefreshparent ();", true );
}
Else
{
Messagelabel. Text = "operation failed. Please try again! ";
Return;
}
}
}

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.