using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// A template for GridView columns which contain a label and a textbox /// public class GridViewTemplate : System.Web.UI.Page, ITemplate { private ListItemType templateType; private string columnName; /// /// Initializes a new GridViewTemplate for the specified column name /// /// The field type. /// The name of the column. public GridViewTemplate(ListItemType type, string colname) { templateType = type; columnName = colname; } /// /// Adds the appropriate controls to a container /// /// The object to contain the instances of controls from the inline template. public void InstantiateIn(System.Web.UI.Control container) { Label lbl; TextBox txt; switch(templateType) { case ListItemType.Header: lbl = new Label(); try { lbl.Text = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(columnName).EnglishName + " (" + columnName + ")"; } catch { lbl.Text = columnName; } lbl.EnableViewState = false; container.Controls.Add(lbl); break; case ListItemType.Item: lbl = new Label(); container.Controls.Add(lbl); lbl.EnableViewState = false; lbl.DataBinding += new EventHandler(lbl_DataBinding); txt = new TextBox(); txt.Visible = false; txt.TextMode = TextBoxMode.MultiLine; txt.EnableViewState = false; container.Controls.Add(txt); txt.DataBinding += new EventHandler(txt_DataBinding); break; case ListItemType.Footer: txt = new TextBox(); txt.TextMode = TextBoxMode.MultiLine; txt.ID = container.UniqueID + "$" + columnName; txt.EnableViewState = false; container.Controls.Add(txt); break; } } /// /// Databinds the textbox and assigns it an ID based on the column name. /// /// The source of the event. /// The instance containing the event data. private void txt_DataBinding(object sender, EventArgs e) { TextBox txt = sender as TextBox; GridViewRow row = txt.NamingContainer as GridViewRow; txt.ID = row.UniqueID+"$"+columnName; object dataValue = DataBinder.Eval(row.DataItem, columnName); if(dataValue != DBNull.Value) { txt.Text = dataValue.ToString(); } } /// /// Databinds the label. /// /// The source of the event. /// The instance containing the event data. private void lbl_DataBinding(object sender, EventArgs e) { Label lbl = sender as Label; GridViewRow row = lbl.NamingContainer as GridViewRow; object dataValue = DataBinder.Eval(row.DataItem, columnName); if(dataValue != DBNull.Value) { lbl.Text = dataValue.ToString(); } } }