继承ITemplate类,动态创建并绑定数据(转载)
时间:2010-09-24 来源:聽︻$▅▆雨
原文地址:http://hi.baidu.com/trip008/blog/item/be3ce3ef89a0833eadafd520.html 继承ITemplate类,动态创建textbox,checkbox,dropdownlist等,并动态绑定数据 public class GridViewItemTemplate:ITemplate { private string obj; //控件对象的字符串,以此来判断具体创建哪个控件 private DataControlRowType templateType; //当前行的模板 (Header,item) private string colName; //控件要显示的字符,或是绑定数据源的字段列名 public GridViewItemTemplate(DataControlRowType rtype, string controlStr, string colN) { templateType = rtype; colName = colN; obj = controlStr; } public GridViewItemTemplate(DataControlRowType rtype, string colN) { templateType = rtype; colName = colN; obj = "TextBox"; } public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) { case DataControlRowType.Header: Literal headlt = new Literal(); headlt.Text = colName; container.Controls.Add(headlt); break; case DataControlRowType.DataRow: switch (obj.ToLower()) { case "htmlcheckbox": HtmlInputCheckBox cbkeys = new HtmlInputCheckBox(); cbkeys.ID = "cb_keys"; cbkeys.DataBinding += new EventHandler(this.CheckBoxDataBinding); container.Controls.Add(cbkeys); break; case "dropdownlist": DropDownList drr = new DropDownList(); drr.ID = "dropdown"; drr.AppendDataBoundItems = true; drr.Items.Add(new ListItem("-----请选择------", "")); drr.Items.Add(new ListItem("AA", "a")); drr.Items.Add(new ListItem("BB", "b")); drr.Items.Add(new ListItem("CC", "c")); container.Controls.Add(drr); break; default: TextBox myTextBox = new TextBox(); myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding); container.Controls.Add(myTextBox); break; } break; default: break; } } private void TextBoxDataBinding(Object sender, EventArgs e) { TextBox myTextBox = (TextBox)sender; GridViewRow row = (GridViewRow)myTextBox.NamingContainer; myTextBox.Text = System.Web.UI.DataBinder.Eval(row.DataItem, colName).ToString(); } private void CheckBoxDataBinding(Object sender, EventArgs e) { GridViewRow row=null; switch (obj.ToLower()) { case "htmlcheckbox": HtmlInputCheckBox hcbox = (HtmlInputCheckBox)sender; row= (GridViewRow)hcbox.NamingContainer; hcbox.Value = System.Web.UI.DataBinder.Eval(row.DataItem, colName).ToString(); break; default: CheckBox cbox = (CheckBox)sender; row = (GridViewRow)cbox.NamingContainer; cbox.Text = System.Web.UI.DataBinder.Eval(row.DataItem, colName).ToString(); //cbox.Text = ((DataRowView) row.DataItem)[colName].ToString(); break; } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。 //下面只是例子,举一反三 DataRowView gv = (DataRowView)e.Row.DataItem; int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString()); DropDownList dr = (DropDownList)e.Row.FindControl("dropdown"); dr.SelectedIndex = itemSeleted; } } } void BindFields(int tbid) { TemplateField tf = new TemplateField(); tf.ItemTemplate = new GridViewItemTemplate(DataControlRowType.DataRow, "htmlcheckbox", "dataKey"); tf.HeaderTemplate = new GridViewItemTemplate(DataControlRowType.Header, "htmlcheckbox", ""); tf.HeaderStyle.CssClass = "cbox"; this.gv_Data.Columns.Add(tf); }
相关阅读 更多 +