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

时间:2013-12-16 15:27:30

标签: c# gridview custom-controls

请帮助我,我需要点击按钮触发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;

    }
}

}

1 个答案:

答案 0 :(得分:0)

只是一个简单的想法,但请更换线:

    b.Click += new EventHandler(b_click);

由:

    b.Click += b_click;

添加然后在b_click函数上添加Breakdown以确保触发了Event。 如果它适合你,请告诉我。