自定义控件中具有动态模板的网格视图未激发事件

本文关键字:视图 网格 事件 动态 自定义控件 | 更新日期: 2024-10-22 16:23:32

请帮帮我,我需要点击按钮来启动b_click过程,但它不起作用。这是我的自定义控件,其中包含一个gridview和一个使用ITemplate类动态添加的列。

namespace NamespaceServerControlSearch
{
    [ToolboxData("<{0}:ServerControlSearch runat=server></{0}:ServerControlSearch>")]
    public class ServerControlSearch : CompositeControl
    {
        private global::System.Web.UI.WebControls.GridView grid;

    protected override void CreateChildControls()
    {
        Controls.Clear();
        grid = new GridView();
        grid.AutoGenerateColumns = true;
        grid.ID = "grid";
        grid.Width = Unit.Pixel(400);
        grid.Height = Unit.Pixel(100);
        grid.BorderStyle = BorderStyle.Solid;
        DataColumn col = new DataColumn("xxx");
        TemplateField bfield = new TemplateField();
        bfield.HeaderTemplate = new GridViewTemplateSelect(ListItemType.Header, col.ColumnName);
        bfield.ItemTemplate = new GridViewTemplateSelect(ListItemType.Item, col.ColumnName);
        grid.Columns.Add(bfield);
        DataSet ds = new DataSet();
        using (SqlConnection conn  = new SqlConnection("Data Source=(local); Initial Catalog=casproduccion; USER=sa; PASSWORD=Jotsa123"))
        {
            SqlDataAdapter da = new SqlDataAdapter("select codigo, nombre from CAS_USERS cu where codigo like 'F37008%'", conn);
            da.Fill(ds);
        }
        grid.DataSource = ds.Tables[0];
        grid.DataBind();
    }
    protected override void Render(HtmlTextWriter o)
    {
        o.Write("<div style='"width: 420px; height: 100px; overflow: scroll; border: 1px solid black;'">");
        grid.RenderControl(o);
        o.Write("</div>");
    }
    protected override void RecreateChildControls()
    {
        EnsureChildControls();
    }
}
public class GridViewTemplateSelect : Control, ITemplate
{
    private ListItemType _templateType;
    private string _columnName;
    public GridViewTemplateSelect(ListItemType type, string colname)
    {
        _templateType = type;
        _columnName = colname;
    }
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (_templateType)
        {
            case ListItemType.Header:
                Label lbl = new Label();
                lbl.Text = _columnName;
                container.Controls.Add(lbl);
                break;
            case ListItemType.Item:
                Button b = new Button();
                b.Text = "test";
                //b.DataBinding += new EventHandler(b_click);
                b.Click += new EventHandler(b_click);
                container.Controls.Add(b);
                break;
        }
    }
    public void b_click(object sender, EventArgs e)
    {
        Button bb = (Button)sender;
        bb.Text = bb.Text + "A";        //change name to test if it's receiving event
        GridViewRow container = (GridViewRow)bb.NamingContainer;
        string cid = container.Cells[1].Text;
    }
}

}

自定义控件中具有动态模板的网格视图未激发事件

这只是一个简单的想法,但请替换行:

    b.Click += new EventHandler(b_click);

发件人:

    b.Click += b_click;

add,然后在b_click函数上添加Breakdown,以确保事件已被激发。请告诉我这对你是否有效。