Dynamic ItemTemplate implementation (translation)

xiaoxiao2021-03-06  57

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 {///

/// Summary description for GenericItem /// public class GenericItem: ITemplate {private string column; // private bool validate; public GenericItem (string column. ) {This.column = column;} public void instantiatein (control container) {literal L = new literal (); l.databinding = new eventhandler (this.binddata); Container.Controls.Add (L);} public void Binddata (Object Sender, Eventargs E) {Litral L = (Literal) Sender; DataGridItem Container = (DataGridItem) L.NamingContainer; L.Text = ((DataRowView) Container.DataItem) [Column] .tostring ();}}} As you can see, the GenericItem class implements the interface of Itemplate. Because we are implementing the interface, you must include instant IATEIN Method. This method is to define all the control objects belonging to all sub-controls and templates. In this method, we created a new Literal control to save the cell value of DataGrid. Next, we added a DataBinding event handler This event handler is actually when DataGrid binding data, putting the unit value into the text property of the Literal control. Finally, add this Litral Control to the control of the container collection. Very simple? Dynamic EditItemTemplate Dynamics EditItemTemplate Class ValidateEditItem is very similar to GenericItem, but there is a difference between 3 places. The first difference is that we add the TextBox control instead of the Literal control. In the editing mode, users can do any modifications. Second Different places, you will find that we will explicitly name the control. This will enable us to get any data changes in the update event. The last difference, you will see a RequiredFieldValidator control associated with TextBox. This is optional. However, this doesn't know that some things can be done. The following is the ValidateEditItem.Data; use system.web.ui; useing system.web.ui.webcontrols; using system. Web;

namespace TripleASP.ItemTemplates {///

/// Summary description for ValidateEditItem /// public class ValidateEditItem: ITemplate {private string column; public ValidateEditItem (string column) {this.column = column;}. Public void instainer {textbox tb = new textbox (); tb.databinding = new eventhandler (this.binddata); Container.Controls.Add (TB); tb.id = column;

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 ());

转载请注明原文地址:https://www.9cbs.com/read-112899.html

New Post(0)