2010-07-31 69 views
0

我工作的自定义服务器控件包含两个网格与动态templatefield复选框,第一个绑定SqlDataSource,第二个网格填充选定的行在第一个网格, 都清楚,第二个网格适当填充,但是当一个按钮(超出自定义控件)单击事件触发网格消失时,第二个重要的是如何在回发后保存复选框的状态,我必须创建该字段并绑定网格?自定义控件VS动态TemplateField复选框

我很感谢你的直接答案。

为模板领域:

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(); 

} 

回答

0

只要覆盖所有的Control LifeCycle事件(rebind gridview),我认为这是一个很难的主题 很多开发人员!

0

您需要在Pre_Init绑定。关于丢失复选框选定值的问题也应该消失。

+0

这是一个自定义服务器控件,我试图覆盖this.OnInit,this.OnLoad ...没办法。此外,在任何其他控制组件(页面中)发布的回发之后,所有模板域消失。我如何重新绑定Control.Page.On_Pre_Init? – Nazm 2010-08-01 08:44:36