2010-06-16 82 views
1

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

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

我认为这将有所帮助,如果你给按钮不同的CommandName属性。

这里是在GridView_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 + "."; 

    } 

    } 
+0

谢谢,我做了更多的搜索后发现了答案。 Bob – 2010-06-16 20:54:21

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; 
    } 

这里是一个用于多选择按钮的样本的GridView

Multiple Select Button In One Gridview