English Original: http://scottwater.com/articles/dynamicItemTemplates Chinese Translation (Translation) (Translation)
Original: Implementing Dynamic ItemTemplatesby: Scott Watermaskpublish: 4/10/2002 Translation: Draason template control allows users to create a complex user interface almost any time. ASP.NET has many controls use template technology (DataGrid is one Examples). These controls are working very well, usually, the template can be saved as an ASCX file to increase the reuse. It is very possible, you don't know how your control is layout beforehand, and you need to add a dynamic added Some templates to cope with different events. Use another advantage of the template, that is, they can be added to your control. In this case, you can design a model version in advance, then add it to you by simple lines. In the control. Below this article, you will tell you how to add a dynamic itemtemplate and EditItemTemplate to DataGrid step by step. In addition, you will tell you how to get and update the user's changes to EditItemTemplate. Example will be very Simple. Then, I will quickly release a improved TableEditor version on TripleASP. This version will better explain how to use dynamic template .ItemPalte's implementation to add ItemTemplate and EditItemplate, we need to create 2 Class to implement Itemplate interface (interface). The first class is GenericItem. This class is: Take the column name of the data source, create a text control (Literal Contral), assign this text control, and finally Text control is added to the parent control (the parent control here is DataGrid). So far, it is still very smooth. Before we continue the following discussion, let's take a look at the code and completed steps .using system; using system.web; using system; .Data; using System.Web.UI; using System.Web.UI.WebControls; namespace TripleASP.ItemTemplates {///
}}} As you can see, the GenericItem class implements the interface of Itemplate. Because we are implementing the interface, you must include instantiatein. This method is to define all the controls to all sub-controls and templates. In this method, we created a new Literal control to save the DataGrid unit value. Next, we added a DataBinding event handler. This event handler actually puts the unit value when DataGrid binds data. During the text property of the Literal control. Finally, add this Literal Control to the container collection of the control. Very simple? Dynamic edititemtemplate dynamic edititemtemplate class ValidateEditIitith is very similar to GenericItem, but there are 3 places. The first different places are We add the TextBox control instead of the Literal control. In the edit mode, users can do any modifications. The second different places, you will find that we will explicitly naming the control. This will make us get us Update any data in the event. The last difference, you will see a RequiredFieldValidator control with TextBox. This is optional. However, this does let you know that some things can do so. Below is ValidateEditItem Code: using System; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; using System.Web; namespace TripleASP.ItemTemplates {///
(DataGridItem) TB.NamingContainer; TB.Text = ((DATAROWVIEW) Container.DataItem) [Column] .tostring ();}}} Dynamic template Implementation Now We already have two classes that implement the itempalte interface. Everything is ready Ok! What we have to do now is to join them in our DataGrid. We put both BindData and DynamicColumns. BindData is mainly to create SQL query statements, add columns to DataGrid (dynamic columns), then put data binding to datagrid.void BindData () {string sql = "Select * from publishers Where State Is not null"; DataGrid1.Columns.Add (DynamicColumns ( "pub_id", false)); DataGrid1.Columns.Add (DynamicColumns ( "pub_name", true)); DataGrid1.Columns.Add (DynamicColumns ( "city", true)); DataGrid1.Columns.Add (DynamicColumns ( "state", true)); DataGrid1.Columns.Add (DynamicColumns ( "country ", true); dataGrid1.datakeyfield =" pub_id "; dataGrid1.datasource = getDataTable (SQL); DataGrid1.Database ();} DynamicColumns has two parameters: column (character type) and iSeditable (Boolean). Column. Column variable Of course, we have to join the column name of TemplateColumn. The iSEDITABLE variable is used as test, if we want this column is allowed to edit .protected templateColumn DynamicColumn (String Column, Bool Iseditable) {TemplateColumn GenericColum n = new TemplateColumn (); genericcolumn.HeaderText = column; genericcolumn.ItemTemplate = new GenericItem (column); if (isEditable) {genericcolumn.EditItemTemplate = new ValidateEditItem (column);} return genericcolumn;
} As you can see, first we instantiate a TemplateColumn (GenericColumn), set the headerText property according to the name of the column we want to add (of course, you can set it to anything). Next, we add new genericItem Reference, add ItemTemplate to GenericColumn, incorporated the name. Finally, we must check iSEDITABLE to see if we need to allow you to edit this column. If you are true, we have to add a new reference to ValidateEditItem. And the column name is also passed. Our editorial and cancellation events are very standard. You have already seen them 100 times. In our editing event, we simply remove the number selected by the selected line, Then resin the data. Protected void Edit_Click (Object Sender, DataGridCommandeventAndargs e) {dataGrid1.edititemindex = E.Item.itemindex; binddata ();} Our cancellation event is to set the currently selected line number to -1. This way It is equal to telling DataGrid, it is not an edit mode. Then, we rebound data .protected void cancel_click (Object sender, DataGridCommandEventArgs e) {dataGrid1.edititemindex = -1; binddata ();} Update event will be seen before there is a little bit different. However, it will make you think you .protected void Update_Click (Object sender, DataGridCommandEventArgs e) {// in the days of ASP Gets the UniqueID that is attached to the front of each textbox // dyamically added to our DataGrid's edititemtempate string uid = E.Item.Uniqueid ":"; string pub_id = (string) DataGrid1.DataKeys [E.Item.ItemIndex]; String pub_name = (Request.form [UID "Pub_Name"]. TOSTRING ()); String City = (Request.form [UID "City"]. TOSTRING ()); string state = (Request.form [UID "State"]. TOSTRING ()); string country = (Request.form [UID "Country]. TOSTRING ()); // Simple Method to Update DB UpdateRecord (Pub_ID, Pub_Name, City, State, Country); DataGrid1.edititemindex = -1; binddata ();