在gridview中添加按钮动态填充:事件单击不激活

时间:2012-03-20 18:45:51

标签: c# gridview button dynamically-generated

我在这个功能上工作了几次,但根本没有工作。 我通过代码填充gridview,然后添加一个按钮,在单击时打开行的详细信息。

我尝试在填充数据表时添加按钮但不起作用。 我尝试在row_databound方法上添加按钮后;它正在渲染的按钮但没有火灾事件点击。

遵循简化代码。 我希望你能帮助我解决这个问题。

提前感谢并问候。 P

protected void btnSearch_Click(object sender, EventArgs e)
{
    string sSearchQuery = "(" + TextBox1.Text + ")";
    loadDynamicGrid(sSearchQuery);
}

private void loadDynamicGrid(string sSearchQuery)
    {
        //search
       .....     
       var oHitColl = searcher.Search(oParser.Parse(sSearchQuery));

        //istance of DataTable
        gvResult.Columns.Clear();
        gvResult.DataSource = null;

        DataTable dt = new DataTable();


        DataColumn dcol = new DataColumn("Id", typeof(System.String));
        dt.Columns.Add(dcol);
        dcol = new DataColumn("Table", typeof(System.String));
        dt.Columns.Add(dcol);
        dcol = new DataColumn("Summary", typeof(System.String));
        dt.Columns.Add(dcol);
        dcol = new DataColumn("Link", typeof(System.String));
        dt.Columns.Add(dcol);
        dcol = new DataColumn("Button", typeof(Button));
        dt.Columns.Add(dcol);


        //Populate la datatable
        for (int i = 0; i < oHitColl.Length(); i++)
        {
            Document oDoc = oHitColl.Doc(i);

            DataRow drow = dt.NewRow();
            drow["Table"] = oDoc.Get("Table");
            drow["Summary"] = oDoc.Get("Summary");
            drow["Id"] = oDoc.Get("Id");
            string url = ....".aspx";
            drow["Link"] = linkText;
            //Button btn = CreateButton("dinamicBtn" + i.ToString(), "dinamicBtn" + i.ToString());
            //drow["Button"] = btn;
            dt.Rows.Add(drow);
        }

        // add columus in GridView
        foreach (DataColumn col in dt.Columns)
        {
            //Dichiarare i campi bindati e allocare la memoria che serve
            BoundField bfield = new BoundField();
            bfield.DataField = col.ColumnName;
            bfield.HeaderText = col.ColumnName;
            gvResult.Columns.Add(bfield);

        }

        gvResult.DataSource = dt;
        gvResult.DataBind();
        searcher.Close();
    }

protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btn = new Button();
            btn = CreateButton("btnOpen_"+ e.Row.RowIndex.ToString(), "Open");
            btn.DataBinding +=new EventHandler(btn_DataBinding);
            e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(btn);

        }
    }

private Button CreateButton(string id, string name)
{       
    Button b = new Button();
    b.Text = name;
    b.ID = id;
    b.Click += new EventHandler(Dynamic_Method);
    b.DataBinding += new EventHandler(btn_DataBinding);
    return b;
}

private void btn_DataBinding(object sender, EventArgs e)
    {
        object bound_value_obj = null;
        Control ctrl = (Control)sender;
        IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
        bound_value_obj = DataBinder.Eval(data_item_container.DataItem,"gvResult.Rows.Count.ToString()");
    }

protected void Dynamic_Method(object sender, EventArgs e)
{
    Response.Write("You have clicked at: "+DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
}

1 个答案:

答案 0 :(得分:0)

您动态创建的任何控件都需要在每次回发时重新添加,事件需要在每次回发时挂钩等。除此之外,您应该在OnInit方法中执行此操作。 / p>