2010-06-16 103 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