【问题标题】:RowCommand not getting fired on Dynamically created GridViewRowCommand 没有在动态创建的 GridView 上被触发
【发布时间】:2012-01-30 11:33:15
【问题描述】:

我正在使用一个 ButtonField 和几个 BoundFields 动态创建一个 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;
    }

感谢您的帮助。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:
    if (e.CommandName=="CommandName")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            string boundFieldText= row.Cells[0].Text;
        }
    

    【讨论】:

      【解决方案2】:

      您需要在每次回发时创建网格视图。

      【讨论】:

      • 尝试在会话中存储网格视图。并将事件绑定到每个帖子都不起作用。感谢回复
      猜你喜欢
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多