【问题标题】:How to hide Asp.net LinkButton from code behind without using On Row Data Bound如何在不使用 On Row Data Bound 的情况下从后面的代码中隐藏 Asp.net LinkBut​​ton
【发布时间】:2019-03-19 20:40:32
【问题描述】:

如果我的数据库行中的字段为空,我试图在GridView 中隐藏LinkButton。没有错误,但该行也没有隐藏。这种方法可行还是我必须使用OnRowDataBound 来完成此操作?

这是aspx页面:

<asp:GridView ID="tblDownload" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
    <Columns>
        <asp:BoundField DataField="Description" HeaderText="Description" />                        
        <asp:TemplateField HeaderText="Download Link" SortExpression="Download Link">
            <ItemTemplate>
                <asp:LinkButton ID="ID" runat="server" Text="Download" OnClick="DownloadFile" CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这里是c#:

    public void BindTable()
    {
        try
        {                
            string constr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))

            {
                using (SqlCommand cmd = new SqlCommand("dbo.spAllRows", con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        tblDownload.DataSource = dt;
                        tblDownload.DataBind();                           
                    }
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                        var FileName = sdr["FILE_NAME"].ToString();
                        con.Close();

                        if (FileName == "" || FileName == null)
                        {
                            LinkButton DownloadLink = tblDownload.FindControl("ID") as LinkButton;
                            DownloadLink.Visible = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

【问题讨论】:

    标签: c# asp.net gridview webforms


    【解决方案1】:

    我想你想要这样的东西。您可以使用三元运算符根据行中的值切换 Visible 属性。

    <asp:LinkButton ID="ID" Visible='<%# string.IsNullOrEmpty(Eval("YourColumn").ToString()) ? false : true %>' 
        runat="server" Text="Download"></asp:LinkButton>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多