| |

VerySource

 Forgot password?
 Register
Search
View: 644|Reply: 9

About control event flow

[Copy link]

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-24 23:40:02
| Show all posts |Read mode
code show as below:
using System;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace shopping cart
{
/// <summary>
/// A summary description of WebCustomControl1.
/// </ summary>
public enum LoaderType {DetailLoader, CartLoader};
public delegate void CartLoaderEventHandler (object sender, CartLoaderEventArgs e);

public class CartLoaderEventArgs: EventArgs
{
private int _itemCount;
private string _productNumber;
Ranch
public CartLoaderEventArgs () {}

public CartLoaderEventArgs (string ProductNumber)
{
_itemCount = 0;
_productNumber = ProductNumber;
}

public CartLoaderEventArgs (string ProductNumber, int NumberOfItems)
{
_itemCount = NumberOfItems;
_productNumber = ProductNumber;
}

public string ProductNumber
{
get {return _productNumber;}
set {_productNumber = value;}
}

public int NumberOfItems
{
get {return _itemCount;}
set {_itemCount = value;}
}

}

public class CartLoader: System.Web.UI.WebControls.WebControl, IPostBackDataHandler, IPostBackEventHandler
{
public event CartLoaderEventHandler ItemAdded;
public event CartLoaderEventHandler ItemUpdated;
public event CartLoaderEventHandler ItemRemoved;

private LoaderType _location = LoaderType.DetailLoader;
private string _productID;
private int _itemCount;

public LoaderType Location
{
get {return _location;}
set {_location = value;}
}
public string ProductID
{
get {return _productID;}
set {_productID = value;}
}

public int ItemCount
{
get {return _itemCount;}
set {_itemCount = value;}
}

protected void OnItemAdded ()
{
if (ItemAdded! = null)
ItemAdded (this, new CartLoaderEventArgs (_productID, _itemCount));
}

protected void OnItemUpdated ()
{
if (ItemUpdated! = null)
ItemUpdated (this, new CartLoaderEventArgs (_productID, _itemCount));
}

protected void OnItemRemoved ()
{
if (ItemRemoved! = null)
ItemRemoved (this, new CartLoaderEventArgs (_productID));
}
// Thrown when loading text box data
public bool LoadPostData (string postDataKey, NameValueCollection postData)
{
int count = Int32.Parse (postData [postDataKey]);
if (_itemCount! = count)
{
_itemCount = count;
return true;
}
else
{
return false;
}
}
// If the control starts sending back. That is, a button is clicked. Call this method
public void RaisePostDataChangedEvent ()
{
OnItemUpdated ();
}

public void RaisePostBackEvent (string eventArgument)
{
if (eventArgument == "AddToCart")
{
OnItemAdded ();
_itemCount ++;
}
else if (eventArgument == "RemoveFromCart")
{
OnItemRemoved ();
_itemCount = 0;
}
}

protected override void Render (HtmlTextWriter output)
{
// If the control is in verbose mode, render a button to add it to the shopping cart
if (_location == LoaderType.DetailLoader)
{
output.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference (this, "AddToCart"));
output.AddAttribute (HtmlTextWriterAttribute.Style, "Display: inline; BORDER-RIGHT: gray thin outset; BORDER-TOP: gray thin outset; BORDER-LEFT: gray thin outset; CURSOR: hand; BORDER-BOTTOM: gray thin outset; FONT -FAMILY: 'ComicSans MS'; BACKGROUND-COLOR: lightgrey ");
output.RenderBeginTag (HtmlTextWriterTag.Div);
output.Write ("Add to cart");
output.RenderEndTag ();
}
else /// now in the shopping cart detail page, so two buttons and a text box are displayed
{
output.AddAttribute (HtmlTextWriterAttribute.Width, "5");
output.AddAttribute (HtmlTextWriterAttribute.Name, this.UniqueID);
output.AddAttribute (HtmlTextWriterAttribute.Value, _itemCount.ToString ());
output.RenderBeginTag (HtmlTextWriterTag.Input);
output.RenderEndTag ();

// present the delete button
output.AddAttribute (HtmlTextWriterAttribute.Style, "Display: inline; BORDER-RIGHT: gray thin outset; BORDER-TOP: gray thin outset; BORDER-LEFT: gray thin outset; CURSOR: hand; BORDER-BOTTOM: gray thin outset; FONT -FAMILY: 'ComicSans MS'; BACKGROUND-COLOR: lightgrey ");
output.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference (this, "RemoveFromCart"));
output.RenderBeginTag (HtmlTextWriterTag.Div);
output.Write ("Remove");
output.RenderEndTag ();

// Render update button
output.AddAttribute (HtmlTextWriterAttribute.Style, "Display: inline; BORDER-RIGHT: gray thin outset; BORDER-TOP: gray thin outset; BORDER-LEFT: gray thin outset; CURSOR: hand; BORDER-BOTTOM: gray thin outset; FONT -FAMILY: 'ComicSans MS'; BACKGROUND-COLOR: lightgrey ");
output.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference (this, ""));
output.RenderBeginTag (HtmlTextWriterTag.Div);
output.Write ("Update");
output.RenderEndTag ();
}
}

protected override object SaveViewState ()
{
return (object) _itemCount;
}

protected override void LoadViewState (object state)
{
_itemCount = (int) state;
}


}
}

When you click Add to Cart, Remove, Update will trigger
LoadPostData (string postDataKey, NameValueCollection postData) method
Will determine whether the original quantity is the same as the user just entered. Returns true if different. Do the following
public void RaisePostBackEvent (string eventArgument)
{
if (eventArgument == "AddToCart")
{
OnItemAdded ();
_itemCount ++;
}
else if (eventArgument == "RemoveFromCart")
{
OnItemRemoved ();
_itemCount = 0;
}
}

But this method
public void RaisePostDataChangedEvent ()
{
OnItemUpdated ();
}
When you click Update, the LoadPostData method is executed first. Will determine whether the original value in the text box is equal to the value entered by the user. Returns false if equal. The RaisePostBackEvent method is not executed. But execute
public void RaisePostDataChangedEvent ()
{
OnItemUpdated ();
}

this way. I don't know if my brother understood it correctly. Just a little confused. Or who patiently read this program. Tell me a complete process. THANK YOU
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-13 15:00:02
| Show all posts
You are talking about the life cycle of the control
------------------------------------------
A series of processing steps are performed during the life cycle, including:
Instantiate
A control is instantiated by a page or another control by calling its constructor. The stages listed after this step will only occur if the control is added to the control tree.
Initialize
Initialize the settings required during the life cycle of the incoming web request. At this stage, the page and all controls in the control tree call the OnInit method by default. Developers can provide initialization logic for controls by overloading the OnInit method. At this point in its lifecycle, the control can safely access its child controls placed in the Controls collection, but it cannot access parent controls in the control hierarchy or other higher-level controls (such as pages).

Begin Tracking View State
This phase occurs at the end of the initialization phase. The page automatically calls the TrackViewState method at this stage. The TrackViewState method guarantees that after this stage, changes made using the ViewState dictionary property are stored in the view state of the control. In most cases, the implementation of the TrackViewState method provided by the Control base class is sufficient. The TrackViewState method must be overridden only if the control defines complex properties.

Load View State (postback only)
This phase occurs during postback, not during the initial request. At the end of this phase, the ViewState property of the control is automatically populated. The control can override the default implementation of the LoadViewState method to restore it with a custom state.

Load Postback Data (only used for postback process, optional) (Load Postback Data (postback only, optional))
This phase only occurs in the postback when the control participates in the postback data processing by implementing the IPostBackDataHandler interface. The TextBox control is an example. At this stage, the control must update its state from the sent form data by implementing the LoadPostData method of the IPostBackDataHandler excuse.

Load
Until the beginning of this phase, all controls in the control tree have been initialized and restored to their last state in the previous cycle. The OnLoad method performs operations common to all requests, such as setting up a database query. At this point, the server controls in the tree have been created and initialized, the state has been restored, and the form controls reflect client data. If you need to implement logic that is only executed in the initial request of the page, you should check the IsPostBack property of the page when implementing this logic

Raise change events (only for the postback process, optional) (Raise Changed Events (postback only, optional))
This phase only occurs in the postback when the control participates in the postback data processing by implementing the IPostBackDataHandler interface. At this stage, the control uses a signal (such as the TextBox's TextChanged event) as a signal-its state changes due to a postback (a change event is raised in response to a state change between the current and previous postbacks). In order to participate in this phase, the control must implement the RaisePostDataChangedEvent method of the IPostBackDataHandler interface.

Raise Postback Events (Raise Postback Events (postback only, optional))
This phase occurs in the postback only when the control participates in the postback data processing by implementing the IPostBackEventHandler interface. At this stage, you can implement the logic by implementing the RaisePostBackEvent method of the IPostBackEventHandler interface in order to map client events to server events.

PreRender
At this stage, you should perform any work required before the control is generated by overloading the OnPreRender method. Performing any update before generating output saves changes made to the state of the control during the pre-build phase, and changes made during the build phase are lost.

Save View State
If the control does not maintain state, or it uses the ViewState dictionary to hold all of its state information, then there is no need to implement any additional logic during this phase. During this phase, the page framework automatically saves the ViewState dictionary. If you need custom state management, you must override the SaveViewState method to implement custom state recovery. This method is only called by controls whose EnableViewState property is true. Changes made to any control after this stage will not be saved in the view state of the control.

Generate
With this method, the control writes markup text on the output stream by overloading one of the Control's Render method or the WebControl class's rendering method.

Unload
At this stage, the page performs the cleanup by implementing the Page_Unload method. As a control developer, you should override the Dispose method to perform cleanup.

Dispose
At this stage, you should override the Dispose method to release all the resources occupied by the control.
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-13 18:30:01
| Show all posts
I debugged it. Find out whether this method returns true or false.

public bool LoadPostData (string postDataKey, NameValueCollection postData)
{
int count = Int32.Parse (postData [postDataKey]);
if (_itemCount! = count)
{
_itemCount = count;
return true;
}
else
{
return false;
}
}
Always execute
public void RaisePostBackEvent (string eventArgument)
{
if (eventArgument == "AddToCart")
{
OnItemAdded ();
_itemCount ++;
}
else if (eventArgument == "RemoveFromCart")
{
OnItemRemoved ();
_itemCount = 0;
}
}
this way. The documentation says. This method is not executed when it returns false. A little dizzy.
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-13 23:45:01
| Show all posts
Return false the following will not be executed
public void RaisePostDataChangedEvent ()
{
OnItemUpdated ();
}
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 United States

Post time: 2020-2-14 09:15:01
| Show all posts
RaisePostBackEvent is to determine whether you post back, if it is post back, it will be executed
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-14 10:45:01
| Show all posts
public void RaisePostBackEvent (string eventArgument)
{
// Generally we all do some delegation processing here
Ranch
}
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-10 10:00:01
| Show all posts
RaisePostBackEvent is to determine whether you post back, if it is post back, it will be executed
?? What the hell
public bool LoadPostData (string postDataKey, NameValueCollection postData)
Returns false. Is it executed or not? When debugging yesterday, no matter how you post back, RaisePostBackEvent will be executed
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-10 21:45:02
| Show all posts
LoadPostData

with

RaisePostBackEvent has nothing to do

Its return value can only represent the execution and non-execution of RaisePostDataChangedEvent
If LoadPostData returns TRUE RaisePostDataChangedEvent executes
If LoadPostData returns false RaisePostDataChangedEvent is not executed
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-24 21:45:01
| Show all posts
ASP.NET Server Control and Component Development-> Learn the Basics
Practice more custom controls.
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-25 01:45:01
| Show all posts
Ok. Now studying. Just touched. So quite a lot is not quite clear.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list