简体   繁体   English

自定义控件中没有动态模板的Gridview不触发事件

[英]Gridview with dynamic template in custom control not firing event

Please help me, I need a click on the button fire the b_click procedure and it's not working. 请帮助我,我需要在b_click程序上单击按钮,但它不起作用。 Here is my custom control with a gridview and a column added dynamically using a ITemplate class. 这是我的自定义控件,带有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;

    }
}

} }

Just a simple idea but please replace the Line: 只是一个简单的想法,但请替换Line:

    b.Click += new EventHandler(b_click);

by: 通过:

    b.Click += b_click;

add then add Breakdown on the b_click function to be sure that the Event was fired. 添加,然后在b_click函数上添加Breakdown,以确保触发了该事件。 please let me know if it worked for you. 请让我知道它是否对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM