RowCommand没有被动态创建的GridView触发

时间:2012-01-30 11:33:15

标签: c# asp.net

我正在使用ButtonField和几个BoundField动态创建gridview。 ButtonField按钮类型是LinkBut​​ton。如果我运行它,buttonclick会触发回发,但不会触发rowCommand。即使我使用AutogenerateSelectButton也不会触发它。这个事件是动态的。代码如下:

    protected void B_Search_Click(object sender, EventArgs e) //Search buttonclick that creates and displays the gridview
    {
        gd = getGridView(); //defines the gridview with columns and buttonfield

        gd.DataSource = executeAdvanceSearch(); //retrieves data from DB as Dataset
        gd.DataBind();

        gd.RowCommand += new GridViewCommandEventHandler(gdView_RowCommand); //Rowcommand event binding
        PlaceHolder1.Controls.Add(gd);
    }

    protected void gdView_RowCommand(object sender, GridViewCommandEventArgs e) //not getting triggered on postback
    {
        int index = Convert.ToInt32(e.CommandArgument);

        GridView gdView = (GridView)sender;

        if (e.CommandName == "IDClick")
        {
           //Do something
        }
    }


    private GridView getGridView()
    {
        GridView gdView = new GridView();
        gdView.AutoGenerateColumns = false;

        gdView.AutoGenerateSelectButton = true;

        string name;

        string[] field = ZGP.BLL.Search.getResultFormat(Convert.ToInt32(DDL_ResultView.SelectedValue), out name); //Ignore.  This jst gets columnNames

        if (field.Count() != 0)
        {
            gdView.Columns.Add(getSelectButton()); //Adds linkbutton

            foreach (string cName in field) //ignore. This adds columns.
                if (!String.IsNullOrEmpty(cName))
                {
                    gdView.Columns.Add(GV_DataColumn.getGridViewColumn((DataColumnName)Enum.Parse(typeof(DataColumnName), cName))); //Ignore. adds columns 
                }
        }

        return gdView;
    }

    private ButtonField getSelectButton()
    {
        ButtonField _bf = new ButtonField();
        _bf.ButtonType = ButtonType.Link;
        _bf.HeaderText = "ID";
        _bf.DataTextField = "ID";
        _bf.CommandName = "IDClick";
        return _bf;
    }

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

if (e.CommandName=="CommandName")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridView1.Rows[index];
        string boundFieldText= row.Cells[0].Text;
    }

答案 1 :(得分:0)

您需要在每个回发上创建网格视图。