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 complete steps .using system; using system.web; using system.data; using system.web.ui; using system.web.ui .WebControls; namespace TripleASP.ItemTemplates {///
namespace TripleASP.ItemTemplates {///
RequiredFieldValidator rfv = new RequiredFieldValidator (); rfv.Text = "Please Answer"; rfv.ControlToValidate = tb.ID; rfv.Display = ValidatorDisplay.Dynamic; rfv.ID = "validate" tb.ID; container.Controls.Add (RFV);
}
public void BindData (object sender, EventArgs e) {TextBox tb = (TextBox) sender; DataGridItem container = (DataGridItem) tb.NamingContainer; tb.Text = ((DataRowView) container.DataItem) [column] .ToString ();} }} The dynamic template is now there are two classes that implement the itempalte interface. Everything is ready! What we have to do now is to join us to our DataGrid.
We put BindData and DynamicColumns two ways. BindData is mainly to create a SQL query statement, add columns to DataGrid, then bind the data sheet 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). The column variable is of course the column name we want to join. Iseditable variable is used as test, If we want this column is allowed to edit, then .protected TemplateColumn DynamicColumns (string column, bool isEditable) {TemplateColumn genericcolumn = new TemplateColumn (); genericcolumn.HeaderText = column; genericcolumn.ItemTemplate = new GenericItem (column); if (isEditable) { Genericcolumn.editItemplate = new validatee Ditithm (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 the itemTemplate to GenericColumn by adding a new GenericItem, and incorporates the name. Finally, we must check the iSeditable to see if we need to allow editing of this column. If it is true, We have to add new references to ValidateEditItem, and the column names are also passed. Our editors are very standard. You may have seen them 100 times. In our editorial event, we are simple Remove the number of the selected rows and then rebound the data.
Protected void Edit_Click (Object Sender, DataGridCommandeventAndargs E) {dataGrid1.edititeMindex = E.Item.itemindex; binddata ();} Our cancellation event is set to -1 in the currently selected line number. This is equal to telling DataGrid, not Edit mode. Then, we rebound data .Protected void Cancel_click (Object Sender, DataGridCommandeventArgs E) {dataGrid1.edititemindex = -1; binddata ();} Update event will have a little difference with you before. , it reminds you of your days in ASP .protected void Update_Click (Object sender, DataGridCommandEventArgs e) {// 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 ());