我的TemplateDataField包含 WebDataGrid 中的 DropDownList 控件。 这个DropDownList绑定在WebDataGrid的行initialize事件中,每个post back Grid再次使用DataTable绑定,DataTable存储在一个视图状态,但是一个点复选框更改事件数据从数据库获取并绑定网格,直到一切正常。但是当我点击按钮(保存按钮从网格中获取所有数据并将其保存到数据库中)而不是在DropDownList中找不到任何项目时,那么这是什么问题?
所有控件都在“更新”面板中。
答案 0 :(得分:0)
我有两个建议,在ItemTemplate创建而不是RowInitialize服务器事件上绑定列表,如果要动态创建模板,请使用Page_Init。
代码段:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TemplateDataField FreightField = new TemplateDataField();
FreightField.ItemTemplate = new CustomTemplate();
FreightField.VisibleIndex = 2;
FreightField.Key = "Freight";
TemplateDataField ShippedDateField = new TemplateDataField();
ShippedDateField.ItemTemplate = new CustomTemplate2();
ShippedDateField.VisibleIndex = 3;
ShippedDateField.Key = "ShippedDate";
if (this.WebDataGrid1.Columns.FromKey("Freight") != null)
{
((TemplateDataField)this.WebDataGrid1.Columns["Freight"]).ItemTemplate = new CustomTemplate();
((TemplateDataField)this.WebDataGrid1.Columns["ShippedDate"]).ItemTemplate = new CustomTemplate2();
}
else
{
this.WebDataGrid1.Columns.Add(FreightField);
this.WebDataGrid1.Columns.Add(ShippedDateField);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
public class CustomTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
WebDropDown wdd = new WebDropDown();
wdd.ID = "WebDropDown1";
wdd.Width = Unit.Pixel(200);
wdd.Items.Add(new DropDownItem("1", "1"));
wdd.Items.Add(new DropDownItem("2", "2"));
wdd.Items.Add(new DropDownItem("3", "3"));
wdd.ClientEvents.SelectionChanged = "ctl00_WebDropDown1_SelectionChanged";
container.Controls.Add(wdd);
}
protected void btn_Click(object sender, EventArgs e)
{
var temp = 0;
}
}
public class CustomTemplate2 : ITemplate
{
public void InstantiateIn(Control container)
{
WebDropDown wdd = new WebDropDown();
wdd.ID = "WebDropDown2";
wdd.Width = Unit.Pixel(200);
wdd.Items.Add(new DropDownItem("4", "4"));
wdd.Items.Add(new DropDownItem("5", "5"));
wdd.Items.Add(new DropDownItem("6", "6"));
container.Controls.Add(wdd);
}
}