根据同一行中单元格的值显示和隐藏gridview按钮 [英] Showing and hiding of gridview button based on the value of a cell in the same row

查看:124
本文介绍了根据同一行中单元格的值显示和隐藏gridview按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET中有一个gridview。在我的gridview列中,我希望每行上的按钮显示或隐藏单元格的值是相同的单元格是空还是空。例如,我想要一个按钮在每行上显示Signout_Time为null或空。我写了下面的代码。我遇到的问题是代码以相反的方式工作。使用Signout_Time在行上显示按钮,而没有Signout_Time可见性值的行上的按钮变为false。不应该这样。我也试过改变我的if条件,它仍然无法工作



我尝试过:



I have a gridview in my ASP.NET. in my gridview columns, i want the button on each row to show or hide if the value of a cell is the same cell is empty or null. for example, i want a button to show on each row that has Signout_Time as null or empty. i have written the code below. the issue i'm having is that the codes works in an opposite way. Buttons are showed on the rows with Signout_Time, while the button on rows without a value for Signout_Time visibility becomes false. it shouldnt be so. I have also tried changing my if conditions, it still didnt work

What I have tried:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" style="text-align: left; margin-left: 169px" Width="1069px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
                        
                        <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>














protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {





开关(e.Row.RowType)

{

case DataControlRowType.DataRow:

DataRowView myDataRowView =(DataRowView)e.Row.DataItem;

if(String.IsNullOrEmpty(myDataRowView [Signout_Time]。ToString()))

{

按钮状态=(按钮)e.Row.FindControl(out);

if(status!= null)

{

status.Visible = true;

}

}

休息;

}



}



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

}

推荐答案

结果与预期相反,因为if条件错误。

只需在条件中添加而不是,它应该按预期工作。

The result is opposite than the expected because the if condition is wrong.
Just add a not to the condition and it should work as expected.
if (!String.IsNullOrEmpty(myDataRowView["Signout_Time"].ToString()))
{
  //rest of code





如果不是,请告诉我。有帮助:)



Please let me know, if it doesn't helps :)


我发现了另一种更简单的方法emTemplae。这就是我做的事情



I have discovered another easier way of doing it in my ItemTemplae. this is what i did

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







这也是神奇的。两种解决方案都很完美




This also did the magic. Both solutions are perfect


这篇关于根据同一行中单元格的值显示和隐藏gridview按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆