Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of data binding event raised by ASP.NET Composite Control

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample analysis of data binding events caused by ASP.NET composite controls", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the "sample analysis of data binding events caused by ASP.NET composite controls".

Generate data-bound ASP.NET composite controls

Most complex server controls are data-bound (or perhaps templated) and consist of various child controls. These controls keep a list of items that make up, usually rows or cells of a table. The list is saved in view state after a postback and is generated from bound data or rebuilt from view state. The control also saves the number of its components in view state so that the table structure can be correctly rebuilt when other controls on the page cause a postback. I will use the DataGrid control as an example.

The DataGrid consists of a list of rows, each representing a record in the bound data source. Each grid row is represented by a DataGridRow object (a class derived from TableRow). When each grid row is created and added to the final grid table, corresponding events such as ItemCreated and ItemDataBound are raised to the page. When you create a DataGrid through data binding, the number of rows is determined by the number of bound items and the page size. What happens if a page with DataGrid is posted back?

In this case, if the postback is caused by the DataGrid itself (for example, the user clicks to sort or mark the page), the new page renders the DataGrid again through data binding. This is obvious because DataGrid needs to refresh the data for display. If it is a master postback, the situation is different because another control on the page (such as a button) is clicked. In this case, the DataGrid is not bound to the data and must be rebuilt from the view state. (if view state is disabled, it is a different case, where the grid can only be displayed through data binding. )

The data source is not saved in view state. As a composite control, DataGrid contains child controls, each of which saves its own state to and recovers from view state. DataGrid only needs to track the number of times it must repeat execution before all rows and contained controls are restored from view state. This number is the same as the number of bound items displayed and must be stored in view state as part of the control state. In ASP.NET 1.x, you must learn and implement this pattern yourself. In ASP.NET 2.0, deriving your composite control from the new class CompositeDataBoundControl is fine.

Let's try a grid-like control that displays extensible data-bound news title rows. In the process, we will again use the Headline control discussed in the previous article.

Public class HeadlineListEx: CompositeDataBoundControl {:}

The HeadlineListEx control contains an Items collection property that collects all bound data items. The collection is a public collection and can be populated programmatically when running with most list controls. Support for typical data binding is achieved through a pair of properties (DataTextField and DataTitleField). These two properties indicate the fields in the data source that will be used to populate the news title and text. The Items collection is saved to view state.

To convert a HeadlineListEx control into a true ASP.NET composite control, you first need to derive it from CompositeDataBoundControl, and then replace CreateChildControls. Interestingly, you will notice that CreateChildControls is an overloaded method.

Override int CreateChildControls () override int CreateChildControls (IEnumerable data, bool dataBinding)

The first overloaded method replaces the method defined in the Control class. The second overloaded method is an abstract method that each composite control must replace. In fact, the development of composite controls is simplified to two main tasks:

Replace CreateChildControls.

Implement the Rows collection property to track all components of the control.

The Rows property is different from Items in that it is not saved in view state, has the same lifetime as the request, and references the helper object rather than the bound data item.

Public virtual HeadlineRowCollection Rows {get {if (_ rows = = null) _ rows = new HeadlineRowCollection (); return _ rows;}}

The Rows collection is populated when the control is generated. Let's take a look at the replacement of CreateChildControls. This method takes two parameters: a bound item and a Boolean tag that indicates whether the control is created through data binding or view state. Please note that the programmer comments in the sample program file are in English, which are translated into Chinese for easy reference in this article. )

Override int CreateChildControls (IEnumerable dataSource, bool dataBinding) {if (dataBinding) {string textField = DataTextField; string titleField = DataTitleField; if (dataSource! = null) {foreach (object o in dataSource) {HeadlineItem elem = new HeadlineItem (); elem.Text = DataBinder.GetPropertyValue (o, textField, null); elem.Title = DataBinder.GetPropertyValue (o, titleField, null); Items.Add (elem) } / / start to generate the control hierarchy Table t = new Table (); Controls.Add (t); Rows.Clear (); int itemCount = 0; foreach (HeadlineItem item in Items) {HeadlineRowType type = HeadlineRowType.Simple; HeadlineRow row = CreateHeadlineRow (t, type, item, itemCount, dataBinding); _ rows.Add (row); itemCount++;} return itemCount;}

In the case of data binding, the Items collection is populated first. Iterate through the binding collection, extract the data, and then populate the new instance of the HeadlineItem class. Next, iterate through the Items collection, which may contain additional items added programmatically, and create rows in the control.

HeadlineRow CreateHeadlineRow (Table t, HeadlineRowType rowType, HeadlineItem dataItem, int index, bool dataBinding) {/ / create new rows for the outermost table HeadlineRow row = new HeadlineRow (rowType); / / create cells for child controls TableCell cell = new TableCell (); row.Cells.Add (cell); Headline item = new Headline (); cell.Controls.Add (item) / / raise the HeadlineRowCreated event / / add this row to the created HTML table t.Rows.Add (row); / / process the data object binding if (dataBinding) {row.DataItem = dataItem; Headline ctl = (Headline) cell.Controls [0]; ctl.Text = dataItem.Text; ctl.Title = dataItem.Title; / / raise the HeadlineRowDataBound event} return row;}

The CreateHeadlineRow method creates and returns an instance of the HeadlineRow class (derived from TableRow). In this case, the row contains a cell populated by the Headline control. In other cases, you can change this section of the code to add multiple cells as needed and populate the content accordingly.

It is important to divide the tasks that need to be done into two different steps: creation and data binding. First, create the layout of the row, lead to the release creation event (if any), and finally add it to the parent table. Next, if you want to bind the control to data, set child control properties that are sensitive to the bound data. When the operation is complete, a row data binding event, if any, is raised.

Note that this pattern more accurately describes the internal architecture of the ASP.NET composite control.

You can use the following code to raise an event.

HeadlineRowEventArgs e = new HeadlineRowEventArgs (); e.DataItem = dataItem; e.RowIndex = index; e.RowType = rowType; e.Item = row; OnHeadlineRowDataBound (e)

Note that the DataItem property is set only when you want to raise a data binding event. The event data structure is arbitrarily set to the following form. You can change it if you think it is necessary.

Public class HeadlineRowEventArgs: EventArgs {public HeadlineItem DataItem; public HeadlineRowType RowType; public int RowIndex; public HeadlineRow Item;}

To actually raise an event, it is common practice to use a protected method defined below.

Protected virtual void OnHeadlineRowDataBound (HeadlineRowEventArgs e) {if (HeadlineRowDataBound! = null) HeadlineRowDataBound (this, e);}

To declare this event, you can use the new generic event handler delegate in ASP.NET 2.0.

Public event EventHandler

< HeadlineRowEventArgs>

HeadlineRowDataBound

In the sample page, everything is business as usual. You can define a handler on a control tag and write a method to a code file. Examples are as follows.

< cc1:HeadlineListEx runat="server" ID="HeadlineListEx1" DataTextField="notes" DataTitleField="lastname" DataSourceID="MySource" OnHeadlineRowDataBound="HeadlineRowCreated" />

The code for the HeadlineRowCreated event handler is shown below.

Protected void HeadlineRowCreated (object sender, HeadlineRowEventArgs e) {if (e.DataItem.Title.Contains ("Doe")) e.Item.BackColor = Color.Red;}

Figure 7: running HeadlineListEx control

By hooking up the data binding event, all items containing Doe will be rendered with a red background.

The above is all the contents of the article "sample Analysis of data binding events raised by ASP.NET Composite controls". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report