C# 自定义控件中具有动态模板的Gridview未触发事件

C# 自定义控件中具有动态模板的Gridview未触发事件,c#,gridview,custom-controls,C#,Gridview,Custom Controls,请帮帮我,我需要点击按钮启动“点击”程序,但它不起作用。 这是我的自定义控件,其中包含一个gridview和一个使用ITemplate类动态添加的列 namespace NamespaceServerControlSearch { [ToolboxData("<{0}:ServerControlSearch runat=server></{0}:ServerControlSearch>")] public class ServerControlSearch

请帮帮我,我需要点击按钮启动“点击”程序,但它不起作用。 这是我的自定义控件,其中包含一个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;

    }
}
命名空间NamespaceServerControlSearch
{
[ToolboxData(“”)
公共类ServerControlSearch:CompositeControl
{
私有全局::System.Web.UI.WebControls.GridView网格;
受保护的覆盖无效CreateChildControls()
{
控件。清除();
grid=新的GridView();
grid.AutoGenerateColumns=true;
grid.ID=“grid”;
网格宽度=单位像素(400);
网格高度=单位像素(100);
grid.BorderStyle=BorderStyle.Solid;
DataColumn col=新的DataColumn(“xxx”);
TemplateField bfield=新TemplateField();
bfield.HeaderTemplate=新的GridViewTemplateSelect(ListItemType.Header,col.ColumnName);
bfield.ItemTemplate=新建GridViewTemplateSelect(ListItemType.Item,col.ColumnName);
grid.Columns.Add(bfield);
数据集ds=新数据集();
使用(SqlConnection conn=newsqlconnection(“数据源=(本地);初始目录=casproduccion;用户=sa;密码=Jotsa123”))
{
SqlDataAdapter da=新的SqlDataAdapter(“从CAS_用户cu中选择codigo,nombre,其中codigo类似于'F37008%”,conn);
da.填充(ds);
}
grid.DataSource=ds.Tables[0];
grid.DataBind();
}
受保护的覆盖无效渲染(HtmlTextWriter o)
{
o、 写(“”);
网格渲染控制(o);
o、 写(“”);
}
受保护的重写void RecreateChildControls()
{
ensureChildControl();
}
}
公共类GridViewTemplateSelect:控件,ITemplate
{
私有ListItemType _templateType;
私有字符串_columnName;
公共GridViewTemplateSelect(ListItemType类型,字符串colname)
{
_模板类型=类型;
_columnName=colname;
}
void ITemplate.instanceEIN(System.Web.UI.Control容器)
{
开关(_templateType)
{
案例ListItemType.Header:
标签lbl=新标签();
lbl.Text=_columnName;
container.Controls.Add(lbl);
打破
案例列表项目类型。项目:
按钮b=新按钮();
b、 Text=“测试”;
//b、 数据绑定+=新事件处理程序(单击);
b、 单击+=新建事件处理程序(单击);
容器。控件。添加(b);
打破
}
}
public void b_单击(对象发送者,事件参数e)
{
按钮bb=(按钮)发送器;
bb.Text=bb.Text+“A”;//更改名称以测试它是否正在接收事件
GridViewRow容器=(GridViewRow)bb.NamingContainer;
字符串cid=容器。单元格[1]。文本;
}
}

}

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

    b.Click += new EventHandler(b_click);
作者:

在b_click函数上添加然后添加细分,以确保事件已触发。 请让我知道它是否适合你

    b.Click += b_click;