【问题标题】:Two Button Columns on a GridView ControlGridView 控件上的两个按钮列
【发布时间】:2010-06-16 19:30:44
【问题描述】:

我有一个网格视图,有两个不同的按钮列。我想根据用户按下的按钮执行不同的操作。如何在 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);

【问题讨论】:

    标签: asp.net gridview controls


    【解决方案1】:

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

    这是MSDN exampleGridView_RowCommand 事件中阅读CommandNameMSDN example,其中特别提到了您的多按钮情况:

      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 + ".";
    
        }
    
      }
    

    【讨论】:

    • 谢谢,经过更多搜索后,我也找到了答案。鲍勃
    【解决方案2】:
    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;
      }
    

    这是一个网格视图中的多选按钮示例

    Multiple Select Button In One Gridview

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多