【问题标题】:Custom Control VS Dynamic TemplateField checkbox自定义控件 VS 动态模板字段复选框
【发布时间】: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();  

}  

【问题讨论】:

    标签: c# .net asp.net


    【解决方案1】:

    您需要在 Pre_Init 中进行绑定。丢失复选框的选定值的问题也应该消失了。

    【讨论】:

    • 这是一个自定义服务器控件,我试图覆盖 this.OnInit,this.OnLoad ... 没办法。此外,在任何其他不受控制的组件(在页面中)触发的任何回发之后,所有模板字段都会消失。如果 Control.Page.On_Pre_Init 被触发,我该如何重新绑定它?
    【解决方案2】:

    只需覆盖所有 Control LifeCycle 事件(重新绑定 gridview),我认为这是一个很难的话题 很多开发者!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2014-01-04
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多