【发布时间】:2013-12-20 08:24:00
【问题描述】:
我在一个自定义服务器控件上工作,该控件包含两个带有动态模板字段复选框的网格,第一个由 SqlDataSource 绑定,第二个网格填充了第一个网格中的选定行, 一切都很清楚,第二个网格填充得当,但是当一个按钮(超出自定义控件)单击事件触发网格消失时,第二个重要的是如何保存复选框的状态,而在回发后我必须创建字段并绑定网格?
感谢您的直接回答。
对于模板字段:
class CheckBoxTemplateHandler:ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
CheckBox cb = new CheckBox();
cb.ID = "CB";
//cb.EnableViewState = true;
cb.AutoPostBack = true;
cb.DataBinding += new EventHandler(this.cb_DataBinding);
cb.CheckedChanged += new EventHandler(Dynamic_Method);
container.Controls.Add(cb);
}
protected void Dynamic_Method(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
((CheckBox)sender).Text = "Selected";
}
}
private void cb_DataBinding(Object sender, EventArgs e)
{
// Get the checkbox control to bind the value. The checkbox control
// is contained in the object that raised the DataBinding
// event (the sender parameter).
CheckBox l = (CheckBox)sender;
// Get the GridViewRow object that contains the checkbox control.
GridViewRow row = (GridViewRow)l.NamingContainer;
// Get the field value from the GridViewRow object and
// assign it to the Text property of the checkbox control.
l.Text = DataBinder.Eval(row.DataItem, "price").ToString();
}
【问题讨论】: