【问题标题】:GridView button with condition带有条件的 GridView 按钮
【发布时间】:2011-08-18 16:30:31
【问题描述】:

我有一个包含 3 列 Name,Surname,Change(ButtonField column) 的 gridview,它是使用以下代码从 SqlDataReader 填充的

               SqlCommand sqlCommand = new SqlCommand("select * from Empl where userId ='" + userid + "'", sqlConnection);
            sqlConnection.Open();

            SqlDataReader reader = sqlCommand.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();

在我的数据库中有 4 列 UserId、Name、Surname、Change(bit)。如何仅在数据库中的 Change(bit) 列为 true 时显示按钮更改?

【问题讨论】:

    标签: c# asp.net gridview sqldatareader


    【解决方案1】:

    你可以这样做..

    <asp:TemplateField>
          <ItemTemplate>
             <asp:Button ID="btnChange" runat="server" Text="Change" 
                  Visible='<%# (Boolean) Eval("Change") %>' />
          </ItemTemplate>
     </asp:TemplateField>
    

    正如您在评论中提到的,您在上面的代码中遇到了错误,您尝试...

    Visible='<%# Convert.ToBoolean(Eval("Change")) == true ? true : false %>' 
    

    【讨论】:

    • hm 我试过这个但我得到一个错误code
    • 这不适用于ButtonField,您必须将其设为模板字段。
    • 你的意思是这样吗? code code我又报错了。
    • 异常详细信息:System.InvalidCastException:指定的强制转换无效。
    • 你可以用 Visible=''真的 ?真:假 %>'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多