动态绑定模板列到GridView,页面回发后控件不消失

引用:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx

 

在某些情况下,我们不知道绑定到GridView中的数据会有多少列。例如,用户需要输入产品不同颜色和尺码的需求数量,而不同的产品会有不同的尺码。如下图所示。于是,需要为GridView动态增加模板列,为防止页面回发后控件丢失可在Page_Init中增加模板列。

 

 

代码
1 protected void Page_Init(object sender, EventArgs e)
2 {
3 if (IsPostBack)
4 {
5 BindProductNum();
6 }
7 }
8
9 private void BindProductNum()
10 {
11 ProductBLL productBLL = new ProductBLL();
12 DataTable dt = productBLL.GetProductNum(ProductID, ProviderID, UserID, OrganizeID, PriceName, 2);
13 int columns = dt.Columns.Count;
14 int numWidth = 100 / columns;
15 for (int i = 0; i < columns; i++)
16 {
17 string columnName = dt.Columns[i].Caption.ToString();
18 if (columnName != "颜色" && columnName != "单价")
19 {
20 TemplateField customField = new TemplateField();
21 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, columnName);
22 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, columnName);
23 customField.ItemStyle.Width = Unit.Percentage(numWidth);
24 gvNum.Columns.Add(customField);
25 }
26 else
27 {
28 BoundField boundField = new BoundField();
29 boundField.HeaderText = columnName;
30 boundField.DataField = columnName;
31 boundField.ItemStyle.Width = Unit.Percentage(numWidth);
32 gvNum.Columns.Add(boundField);
33 }
34 }
35 gvNum.DataSource = dt;
36 gvNum.DataBind();
37 }
38
39 // Create a template class to represent a dynamic template column.
40   public class GridViewTemplate : ITemplate
41 {
42 private DataControlRowType templateType;
43 private string columnName;
44
45 public GridViewTemplate(DataControlRowType type, string colname)
46 {
47 templateType = type;
48 columnName = colname;
49 }
50
51 public void InstantiateIn(System.Web.UI.Control container)
52 {
53 // Create the content for the different row types.
54   switch (templateType)
55 {
56 case DataControlRowType.Header:
57 // Create the controls to put in the header
58 // section and set their properties.
59 Label lc = new Label();
60 lc.Text = columnName;
61 // Add the controls to the Controls collection
62 // of the container.
63 container.Controls.Add(lc);
64 break;
65 case DataControlRowType.DataRow:
66 // Create the controls to put in a data row
67 // section and set their properties.
68 TextBox txtNum = new TextBox();
69 txtNum.Width = Unit.Percentage(100);
70 txtNum.Style.Add("text-align", "center");
71 txtNum.ID = columnName;
72 // To support data binding, register the event-handling methods
73 // to perform the data binding. Each control needs its own event
74 // handler.
75 txtNum.DataBinding += new EventHandler(this.txtNum_DataBinding);
76 txtNum.TextChanged += new EventHandler(this.txtNum_TextChanged);
77 // Add the controls to the Controls collection
78 // of the container.
79 container.Controls.Add(txtNum);
80 break;
81 // Insert cases to create the content for the other
82 // row types, if desired.
83 default:
84 // Insert code to handle unexpected values.
85 break;
86 }
87 }
88
89
90 private void txtNum_TextChanged(object sender, EventArgs e)
91 {
92 TextBox txtNum = (TextBox)sender;
93 Product p = new Product();
94 if (!ValidateNumber.IsPositiveInt(txtNum.Text.Trim()))
95 {
96 txtNum.Text = String.Empty;
97 p.ShowError(true,"请输入有效的数字!");
98 return;
99 }
100 // 判断库存
101 int rowIndex = ((GridViewRow)((TextBox)sender).NamingContainer).RowIndex;
102 string colorName = ((GridViewRow)((TextBox)sender).NamingContainer).Cells[0].Text;
103 string txtID = txtNum.ID;
104 int stockNum = p.GetProductStock(colorName, txtID);
105 int orderNum = Convert.ToInt32(txtNum.Text.Trim());
106 bool isAllowNegativeOrder = p.IsAllowNegativeOrder();
107 if (!isAllowNegativeOrder && orderNum > stockNum)
108 {
109 txtNum.Text = String.Empty;
110 p.ShowError(true, "订货数量大于库存数量!");
111 return;
112 }
113 }
114
115 private void txtNum_DataBinding(Object sender, EventArgs e)
116 {
117 // Get the TextBox control to bind the value. The TextBox control
118 // is contained in the object that raised the DataBinding
119 // event (the sender parameter).
120 TextBox txtNum = (TextBox)sender;
121 GridViewRow row = (GridViewRow)txtNum.NamingContainer;
122 // Get the field value from the GridViewRow object and
123 // assign it to the Text property of the Label control.
124 txtNum.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
125 }
126 }

 

posted on 2010-08-07 16:43  NewSunshineLife  阅读(886)  评论(0编辑  收藏  举报

导航