2013-03-06 36 views
0

UI功能:我有GridView幾列。最重要的列是PcCode,它顯示每行的值爲string在asp:GridView單元中單擊鏈接按鈕不會觸發OnRowCommand事件

預計:當我點擊該PcCode列的一行中的一個單元格時,應該顯示另一個GridView。但是,當我嘗試爲單元格使用asp:LinkButton時,事情不起作用。當單擊GridView單元格中的asp:LinkButton時,RowCommand不會被觸發。我在哪裏做錯事?預期功能可以實現哪種方式?提前感謝幫助新手。

在下面的代碼中,我試圖獲得RowIndex並將其傳遞給CommandArgument並使用CommandName

行被選中後,我需要選擇行的值作爲一個字符串,並用列表項比較顯示的.aspx代碼

<asp:GridView ID="_UIProfitcenterTotalGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="22" ShowFooter="True" AutoGenerateColumns="False" 
    DataKeyNames="ProfitcenterCode" EnableViewState="False" 
    OnRowDataBound="_UIProfitcenterTotalGridView_RowDataBound" 
    OnPageIndexChanging="_UIProfitcenterTotalGridView_PageIndexChanging" 
    OnRowCommand="_UIProfitcenterTotalGridView_OnRowCommand"> 
    <Columns> 

     <asp:TemplateField HeaderText="PcCode" InsertVisible="False" 
      ShowHeader="False" SortExpression="ProfitcenterCode" FooterText="Total" FooterStyle-HorizontalAlign="Left"> 
      <ItemTemplate> 
       <asp:LinkButton ID="_UIPCCodeLinkButton" runat="server" Text='<%# Eval("ProfitcenterCode") %>' 
       CommandName="Select" 
       CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'> 
       </asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
... 

後面的代碼aspx.cs

protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Select") 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      GridViewRow row = _UIProfitcenterTotalGridView.Rows[index]; 
      string ProfitcenterCode = _UIProfitcenterTotalGridView.DataKeys[_UIProfitcenterTotalGridView.SelectedIndex].Values["ProfitcenterCode"].ToString(); 
     } 
    } 

一個新的GridView。

試過

  • 使用Link_Button_Click(對象發件人,EventArgs e)和下述但失敗了。

    保護無效_UIProfitcenterTotalGridView_RowCommand(對象發件人,GridViewCommandEventArgs E) { 如果(e.CommandName == 「選擇」) {串ProfitcenterCode =((GridViewRow)(((LinkBut​​ton的)e.CommandSource).NamingContainer)) .Cells [2]。文本; }}

回答

2

我試圖用LinkButton_Click()事件,而不是RowCommandjason建議:

protected void LinkButton_Click(Object sender, EventArgs e) 
    { 
     LinkButton button = (LinkButton)sender; 
     GridViewRow row = (GridViewRow)button.NamingContainer; 
     if (row != null) 
     { 
      string theValue = ((LinkButton)sender).CommandArgument.ToString(); 
      ... 
      ... 
      //code for the extra thing I needed to do after selecting a cell value. 
     } 
    } 

但是我仍然有問題,我想通出。問題是LinkButton沒有綁定到行,因此無法在選擇時傳遞任何值。什麼缺的是下面的代碼:

if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       LinkButton linkButton = new LinkButton(); 
       linkButton.Text = e.Row.Cells[0].Text; //value of the first column from the grid 
       linkButton.Enabled = true; 
       linkButton.Click += new EventHandler(LinkButton_Click); //triggering the LinkButton event here. 
       e.Row.Cells[0].Controls.Add(linkButton); 
      } 
1

您可以使用發件人對象(鏈接按鈕調用的事件),並在GridView獲取父行。例如:

Protected Sub linkButton_click(ByVal sender As Object, ByVal e As EventArgs) 
    'First cast the sender to a link button 
    Dim btn As LinkButton = CType(sender, LinkButton) 
    'now, if there is a command arguement, you can get that like this 
    Dim id As String = btn.CommandArgument 
    'to get the gridviewrow that the button is on: 
    Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow) 

End Sub 

這是很難跟隨正是你要找的,所以如果我錯過了什麼讓我知道,我會添加它。

protected void linkButton_click(object sender, EventArgs e) 
{ 
    //First cast the sender to a link button 
    LinkButton btn = (LinkButton)sender; 
    //now, if there is a command arguement, you can get that like this 
    string id = btn.CommandArgument; 
    //to get the gridviewrow that the button is on: 
    GridViewRow row = (GridViewRow)btn.NamingContainer; 

} 

,改變你的LinkBut​​ton到:

<asp:LinkButton OnClick="linkButton_click" ID="_UIPCCodeLinkButton" 
runat="server" Text='<%# Eval("ProfitcenterCode") %>' 

      CommandName="Select" 
      CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'> 
+0

我一直在尋找如何選擇行值形成的一個LinkBut​​ton click.I在GridView試圖linkBut​​ton_Click事件,它不承認,因爲它是唯一的客戶端檢查。我需要回發到服務器,對不起,我對VB沒有任何線索。我只需要知道我做這件事的方式是否正確。如果不是,那麼哪種方式是正確的? LinkBut​​ton_Click事件或OnRowCommand事件? – shaz 2013-03-06 19:40:21

+0

您可以輕鬆地將c#輕鬆轉換爲vb.net(或vb.net轉換爲c#)。 http://converter.telerik.com/。它看起來像你設置的方式,你將需要使用點擊事件。您需要將事件處理程序添加到鏈接按鈕。我發佈一個版本在C# – jason 2013-03-06 19:49:03

+0

這是否意味着我需要同時使用linkBut​​ton_Click()和OnRowCommand()?但是,使用_Click事件會導致編譯器錯誤消息:CS1061:'ASP.displaycountries_aspx'不包含'_UIPCCodeLinkBut​​ton_Click'的定義,並且沒有找到接受類型爲「ASP.displaycountries_aspx」的第一個參數的擴展方法「_UIPCCodeLinkBut​​ton_Click」缺少使用指令或程序集引用?) – shaz 2013-03-06 20:16:14

相關問題