我有一个datalist和一个包含url的超链接控件。用户通过管理网站创建此数据列表,并非所有超链接都包含链接。问题是如何隐藏数据库字段中没有url的链接。
datalist的代码:
<asp:DataList ID="dtlPromoEvents" runat="server" CellPadding="2" CellSpacing="2" RepeatColumns="1" RepeatDirection="Vertical" RepeatLayout="Table" >
<ItemTemplate>
<asp:Label ID="lblPromoHeading" runat="server" Text='<%# Eval("eventHeading") %>' Font-Size="12pt" ForeColor="#FFFF00" />
<br />
<br />
<asp:Label ID="promoDate" runat="server" Text='<%# Eval("eventDate", "{0:D}") %>' Font-Size="11pt" ForeColor="#33FF00" /> <span style="color: #33ff00; font-size: 12pt">@</span> <asp:Label ID="lblPromoTime" runat="server" Text='<%# Eval("startTime", "{0:t}") %>' Font-Size="11pt" ForeColor="#33FF00" />
<br />
<br />
<asp:Label ID="lblPromoDetails" runat="server" Text='<%# Eval("eventDetails") %>' Font-Size="11pt" />
<br />
<br />
<br />
<asp:HyperLink ID="eventLink" Text="Check this out!" CssClass="linkEvent" Visible="false" runat="server" Target="_blank" ToolTip="click to go check out what's happening!" NavigateUrl='<%# Eval("eventLink") %>' />
<br />
<br />
<br />
<hr style="width: 480px; height: 1px; background-color: #ff9900; border-color: #ff9900" />
<br />
</ItemTemplate>
</asp:DataList>
在后面的代码中,我使用代码设置数据源,现在我很难理解如何根据数据库字段中是否有链接来访问项目并控制“检查此输出”链接的可见性。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtlPromoEvents.DataSource = LoadEvents();
//filter through the events and turn on visibility for the events that have a live link.
dtlPromoEvents.DataBind();
}
}
public static DataSet LoadEvents()
{
DataSet eventInfo = new DataSet();
string connectionString = ConfigurationManager.AppSettings["ConnectBeerGeeksDb"];
using (SqlConnection selectConnInfo = new SqlConnection(connectionString))
{
SqlDataAdapter adapterInfo = new SqlDataAdapter("SELECT [eventDate], [startTime], [eventHeading], [eventDetails], [eventLink] FROM [promoEvent] WHERE (eventDate + 1 > GETDATE()) ORDER BY eventDate", selectConnInfo);
selectConnInfo.Open();
eventInfo.Clear();
adapterInfo.Fill(eventInfo);
selectConnInfo.Close();
}
return eventInfo;
}
任何建议都将不胜感激。
谢谢,
rjsteward
添加适用于此网址的精炼代码:[http://beergeekspub.com/events.aspx] [1]
protected void dtlPromoEvents_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Hyperlink control in the current DataListItem.
HyperLink eLink = (HyperLink)e.Item.FindControl("eventLink");
// Check if a URL exists, if not then hide the control
if (string.IsNullOrEmpty(eLink.NavigateUrl))
{
eLink.Visible =false;
}
}
}
页面加载数据绑定数据集 -
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtlPromoEvents.DataSource = LoadEvents();
dtlPromoEvents.DataBind();
}
}
答案 0 :(得分:2)
您应该使用ItemDataBound事件来捕获控件,因为它们绑定到数据。在这种情况下,您可以检查URL是否存在,如果不存在,则隐藏超链接控件。
如何:在运行时自定义DataList项目:
http://msdn.microsoft.com/en-us/library/6y92e1ze(v=VS.100).aspx#Y900
DataList.ItemDataBound事件
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound(v=VS.100).aspx
例如,修改第二个链接上的示例(就在我的头顶,而不是C#的人,所以这可能不完全):
protected void dtlPromoEvents_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Hyperlink control in the current DataListItem.
HyperLink eLink = (HyperLink)e.Item.FindControl("eventLink");
// Check if a URL exists, if not then hide the control
if (string.IsNullOrEmpty(eLink.NavigateUrl))
{
eLink.Visible =false;
}
}
}
在asp标记中执行此操作:
<asp:DataList ID="dtlPromoEvents" runat="server" CellPadding="2" CellSpacing="2" OnItemDataBound="dtlPromoEvents_ItemDataBound" RepeatColumns="1" RepeatDirection="Vertical" RepeatLayout="Table" >