【问题标题】:Showing and hiding of gridview button based on the value of a cell in the same row根据同一行中单元格的值显示和隐藏gridview按钮
【发布时间】:2016-10-11 12:32:36
【问题描述】:

我的 ASP.NET 中有一个网格视图。在我的gridview列中,如果单元格的值相同,我希望每行上的按钮显示或隐藏单元格为空或空。例如,我希望在 Signout_Time 为空或为空的每一行上显示一个按钮。我已经写了下面的代码。我遇到的问题是代码以相反的方式工作。按钮显示在带有“Signout_Time”的行上,而没有“Signout_Time”可见性值的行上的按钮变为假。不应该这样。我也试过改变我的if条件,还是不行

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

  switch (e.Row.RowType) {
      case DataControlRowType.DataRow:
          DataRowView myDataRowView = (DataRowView)e.Row.DataItem;

          if (String.IsNullOrEmpty(myDataRowView["Signout_Time"].ToString())) {
              Button status = (Button)e.Row.FindControl("out");

              if (status != null) {
                  status.Visible = true;
              }
          }

          break
     }
 }

                    <Columns>
                        <asp:BoundField HeaderText="S/N" DataField="SN" />
                        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                        <asp:BoundField HeaderText="Address" DataField="Address" />
                        <asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
                        <asp:BoundField HeaderText="Sex" DataField="Sex" />
                        <asp:BoundField HeaderText="Reason" DataField="Reason" />
                        <asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
                        <asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />

                        <asp:TemplateField HeaderText="Action" Visible="True">
                            <ItemTemplate>
                                <asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut"  CommandArgument='<%#Eval("SN") %>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />


        </asp:GridView>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    可以直接在GridView Template中设置按钮的Visibility:

    <asp:Button Visible='<%# string.IsNullOrEmpty(Eval("Signout_Time").ToString()) %>' runat="server" Text="Sign out" ID="out"  CommandName="SignOut" CommandArgument='<%#Eval("SN") %>'/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      相关资源
      最近更新 更多