2017-08-10 84 views
0

我有一個具有以下定義的網格。我在這裏包含了兩個按鈕(不同類型),但我真的只想要一個,但在某些情況下它必須能夠隱藏。
通過'ButtonField',我可以將其隱藏在RowDataBound事件中,但是,在回發(行單擊事件)時,顯示的所有按鈕都會顯示。
點擊這個按鈕會觸發兩個RowCommand事件,一個是'Select',另一個是'AcceptStats',如果我不想要時可以隱藏按鈕,這將是好的。c#.net與兩個(有時顯示)按鈕點擊和行單擊事件gridview

'asp:Button'一直顯示正確,但點擊事件似乎已經在行點擊事件下丟失了。
在RowCommand事件中,CommandName始終爲「Select」,它來自行單擊事件。 我曾嘗試將OnClick =「btnAcceptStats_Click」添加到asp:Button,但它不會觸發。

<asp:GridView ID="gvApsimFiles" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" 
    PageSize="10" AllowPaging="true" DataKeyNames="PullRequestId, RunDate" 
    OnPageIndexChanging="gvApsimFiles_PageIndexChanging" 
    OnRowCommand="gvApsimFiles_RowCommand" 
    OnRowDataBound="gvApsimFiles_RowDataBound" 
    OnSelectedIndexChanged="gvApsimFiles_SelectedIndexChanged" > 
    <HeaderStyle CssClass="GridViewHeaderStyle" /> 
    <Columns> 
     <asp:BoundField DataField="PullRequestId" HeaderText="Pull Request Id" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="RunDate" HtmlEncode="false" HeaderText="Run Date" ItemStyle-Width="220px" DataFormatString="{0:d MMMM, yyyy hh:mm tt}" /> 
     <asp:BoundField DataField="IsMerged" HeaderText="Is Merged" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="PercentPassed" HtmlEncode="false" HeaderText="Percent<br />Passed" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="Total" HeaderText="Total Files" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="100px" /> 
     <asp:ButtonField ButtonType="Button" ItemStyle-Font-Size="11px" Text="Accept Stats" CommandName="AcceptStats" /> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Button ID="btnAcceptStats" runat="server" Text="Accept Stats" 
        CommandName="AcceptStats" 
        CommandArgument='<%# Container.DataItemIndex %>' 
        OnClick="btnAcceptStats_Click" 
       /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

在後面的代碼:

protected void btnAcceptStats_Click(object sender, EventArgs e) 
    { 
     GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent; 
     int index = gvRow.RowIndex; 

     int pullRequestId = int.Parse(gvApsimFiles.Rows[index].Cells[0].Text); 
     //Now we can call our web api 'merge' call 
     bool mergeStatus = bool.Parse(gvApsimFiles.Rows[index].Cells[2].Text); 
     if (!mergeStatus) 
     { 
      UpdatePullRequestMergeStatus(pullRequestId, true); 
     } 

    } 

    protected void gvApsimFiles_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     gvApsimFiles.PageIndex = e.NewPageIndex; 
     BindApsimFilesGrid(); 
    } 

    protected void gvApsimFiles_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "AcceptStats") 
     { 
      var eVal = Convert.ToInt32(e.CommandArgument); 
      int index = int.Parse(eVal.ToString()); 
      int pullRequestId = int.Parse(gvApsimFiles.Rows[index].Cells[0].Text); 
      //Now we can call our web api 'merge' call 
      bool mergeStatus = bool.Parse(gvApsimFiles.Rows[index].Cells[2].Text); 
      if (!mergeStatus) 
      { 
       UpdatePullRequestMergeStatus(pullRequestId, true); 
      } 
     } 
    } 

    protected void gvApsimFiles_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //Show as green if 100% 
      if (e.Row.Cells[3].Text.Equals("100")) 
      { 
       e.Row.ForeColor = Color.Green; 
      } 
      //Activate the row click event 
      e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvApsimFiles, "Select$" + e.Row.RowIndex); 
      e.Row.Attributes["style"] = "cursor:pointer"; 


     } 
    } 

有沒有辦法,我可以有一個按鈕的方式,即只顯示必要時,不回發重新出現,並正確觸發,同時保持行單擊事件?

+0

請具體說明 – AsifAli72090

+0

嗨Asif.Ali,我不知道你的意思「請什麼請明確點;? – Lorettac242

回答

0

我找到了解決方案。它通過使用Gridview_RowDataBound,更新網格中的每個單元格,然後在Gridview_RowCommand中檢索這些值,然後從那裏開始。

加載稍慢,但起作用。在.aspx頁面上運行的代碼如下:

<asp:GridView ID="gvApsimFiles" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" 
    PageSize="10" AllowPaging="true" DataKeyNames="PullRequestId, RunDate" 
    OnPageIndexChanging="gvApsimFiles_PageIndexChanging" 
    OnRowCommand="gvApsimFiles_RowCommand" 
    OnRowDataBound="gvApsimFiles_RowDataBound" > 
    <HeaderStyle CssClass="GridViewHeaderStyle" /> 
    <RowStyle CssClass="GridViewRowStyle" /> 
    <Columns> 
     <asp:BoundField DataField="PullRequestId" HeaderText="Pull Request Id" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="RunDate" HtmlEncode="false" HeaderText="Run Date" ItemStyle-Width="220px" DataFormatString="{0:d MMMM, yyyy hh:mm tt}" /> 
     <asp:BoundField DataField="IsMerged" HeaderText="Is Merged" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="PercentPassed" HtmlEncode="false" HeaderText="Percent<br />Passed" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="100px" /> 
     <asp:BoundField DataField="Total" HeaderText="Total Files" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="100px" /> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Button ID="btnAcceptStats" runat="server" Text="Accept Stats" 
        Visible='<%# Eval("IsMerged").ToString().ToLowerInvariant().Equals("false") %>' 
       /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

背後存在的代碼:

protected void gvApsimFiles_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     // Don't interfere with other commands. 
     // We may not have any now, but this is another safe-code strategy. 
     if (e.CommandName == "CellSelect") 
     { 
      // Unpack the arguments. 
      String[] arguments = ((String)e.CommandArgument).Split(new char[] { ',' }); 

      // More safe coding: Don't assume there are at least 2 arguments. 
      // (And ignore when there are more.) 
      if (arguments.Length >= 2) 
      { 
       // And even more safe coding: Don't assume the arguments are proper int values. 
       int rowIndex = -1, cellIndex = -1; 
       bool canUpdate = false; 
       int.TryParse(arguments[0], out rowIndex); 
       int.TryParse(arguments[1], out cellIndex); 
       bool.TryParse(arguments[2], out canUpdate); 

       // Use the rowIndex to select the Row, like Select would do. 
       if (rowIndex > -1 && rowIndex < gvApsimFiles.Rows.Count) 
       { 
        gvApsimFiles.SelectedIndex = rowIndex; 
       } 

       //here we either update the Update Panel (if the user clicks only anything OTHER THAN our'Button' 
       //or we process the UpdatePullRequest as Merged 
       if (cellIndex == 5 && canUpdate == true) 
       { 
        int pullRequestId = int.Parse(gvApsimFiles.Rows[rowIndex].Cells[0].Text); 
        UpdatePullRequestMergeStatus(pullRequestId, true); 
       } 
       else 
       { 
        int pullRequestId = int.Parse(gvApsimFiles.Rows[rowIndex].Cells[0].Text); 
        BindSimFilesGrid(pullRequestId); 

       } 
      } 
     } 

    } 

    protected void gvApsimFiles_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //Show as green if 100% 
      if (e.Row.Cells[3].Text.Equals("100")) 
      { 
       e.Row.ForeColor = Color.Green; 
      } 
      e.Row.Attributes["style"] = "cursor:pointer"; 

      //Active cell click events on individual cells, instead of the row 
      foreach (TableCell cell in e.Row.Cells) 
      { 
       // Although we already know this should be the case, make safe code. Makes copying for reuse a lot easier. 
       if (cell is DataControlFieldCell) 
       { 
        int cellIndex = e.Row.Cells.GetCellIndex(cell); 
        bool canUpdate = false; 
        // if we are binding the 'Button' column, and the "IsMerged' is false, then whe can Update the Merge Status. 
        if (cellIndex == 5 && e.Row.Cells[2].Text.ToLower().Equals("false")) 
        { 
         canUpdate = true; 
        } 
        // Put the link on the cell. 
        cell.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvApsimFiles, String.Format("CellSelect${0},{1},{2}", e.Row.RowIndex, cellIndex, canUpdate)); 

        // Register for event validation: This will keep ASP from giving nasty errors from getting events from controls that shouldn't be sending any. 
        Page.ClientScript.RegisterForEventValidation(gvApsimFiles.UniqueID, String.Format("CellSelect${0},{1},{2}", e.Row.RowIndex, cellIndex, canUpdate)); 
       } 
      } 

     } 
    } 
0

如果你想隱藏按鈕,爲什麼不把他們的可見性基於內聯GridView的值。

<asp:Button ID="Button2" runat="server" Visible='<%# Convert.ToBoolean(Eval("myBool")) %>' Text="Button 1" /> 

<asp:Button ID="Button1" runat="server" Visible='<%# !Convert.ToBoolean(Eval("myBool")) %>' Text="Button 2" /> 
0

感謝您的回覆。我有ASP:按鈕用下面的代碼正確顯示:

visible='<%# Eval("IsMerged").ToString().ToLowerInvariant().Equals("false") %>' 

的問題是,當我使用這個按鈕,我不能讓按鈕點擊事件的工作,只有該行單擊事件。當我遍歷RowCommand時,CommandName只是'Select'。我無法觸發'AcceptStats'事件。