【发布时间】: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