GridView控件上的两个按钮列

时间:2010-06-16 19:30:45

标签: asp.net gridview controls

我有一个网格视图将两个不同的按钮列。我想根据用户按下的按钮执行不同的操作。如何在SelectedIndexChanged事件中确定按下了哪个列。这是我用来生成列的代码。

grdAttachments.Columns.Clear();
ButtonField bfSelect = new ButtonField();
bfSelect.HeaderText = "View";
bfSelect.ButtonType = ButtonType.Link;
bfSelect.CommandName = "Select";
bfSelect.Text = "View";

ButtonField bfLink = new ButtonField();
bfLink.HeaderText = "Link/Unlink";
bfLink.ButtonType = ButtonType.Link;
bfLink.CommandName = "Select";
bfLink.Text = "Link";

grdAttachments.Columns.Add(bfSelect);
grdAttachments.Columns.Add(bfLink);

2 个答案:

答案 0 :(得分:2)

我认为如果你为按钮提供不同的CommandName属性会有所帮助。

以下是CommandName事件中的MSDN example阅读GridView_RowCommand,其中特别提及了您的多按钮情况:

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {

    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Select")
    {

      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    

      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  

      // Display the selected author.
      Message.Text = "You selected " + contact + ".";

    }

  }

答案 1 :(得分:0)

string commandName = e.CommandName.ToString().Trim();
  GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
  switch (commandName)
  {
      case "showName":                   
          LClickName.Text = "You Clicked Show Name Button : \"" + row.Cells[1].Text + "\"";                   
          break;
      case "EditName":                   
          LClickName.Text = "You Clicked Edit Name Button : \"" + row.Cells[1].Text + "\"";                  
          break;
      default: break;
  }

以下是One Gridview中多选按钮的示例

Multiple Select Button In One Gridview