Custom Control VS Dynamic TemplateField checkbox
I work on a custom server control that contains two grids with dynamic templatefield checkbox , the 1st one binded by SqlDataSource, the 2nd grid filled with selected rows in the 1st grid, all is clear, the 2nd grid filled appropriately but when a button(out of the custom control) click开发者_如何学运维 event fired the grid disappear , Second and what is important is how to save state of the checkbox while after postback I must create the field and bind the grid ?
I appreciate your direct answers .
for Template field :
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();
}
You need to bind in Pre_Init. The problem about loosing the selected value of the checkbox should go away, too.
Just override all Control LifeCycle events (rebind gridview),I think it's a hard topic for lot of developer !
精彩评论