我想让Datalist拥有可链接的项目,当我点击它时,我可以导航到特定的URL它转到c#do response to new URL
我的网址不是常数
如何?
答案 0 :(得分:1)
您应该编辑DataList的项目模板并在其中放置ASP HyperLink,并使用数据绑定将NavigateUrl分配给此类控件。
类似的东西:
<asp:DataList ID="listSearchResults" Border="1" BorderColor="Black"
RepeatDirect="Horizontal" RepeatColumns="5" runat="server" >
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:DataList>
另一种方法是使用ASP:LinkButton并将CommandName和CommandArgument属性分配给该控件。
答案 1 :(得分:0)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Datalist with linkable items</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:DataList ID="dlLinkable" runat="server"
onitemdatabound="dlLinkable_ItemDataBound" >
<ItemTemplate>
<table style="width: 300px;" cellpadding="0" cellspacing="1">
<tr>
<td style="width:200px">
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
</td>
<td>
<a id="linkA" runat="server">link</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
populateDataList();
}
private void populateDataList()
{
dt = new DataTable();
//Create 3 columns for this DataTable
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Url");
//Add All These Columns into DataTable table
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
// Create a Row in the DataTable table
DataRow row = dt.NewRow();
row[col1] = 1;
row[col2] = "google";
row[col3] = "http://www.google.com";
dt.Rows.Add(row);
//////////////////////
row = dt.NewRow();
row[col1] = 2;
row[col2] = "yahoo";
row[col3] = "http://www.yahoo.com";
dt.Rows.Add(row);
//////////////////////////////////
dlLinkable.DataSource = dt;
dlLinkable.DataBind();
}
protected void dlLinkable_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Label lblName = (Label)e.Item.FindControl("lblName");
if (lblName != null)
{
lblName.Text = dt.Rows[e.Item.ItemIndex]["Name"].ToString();
}
HtmlAnchor linkA = (HtmlAnchor)e.Item.FindControl("linkA");
if (linkA != null)
{
linkA.HRef = dt.Rows[e.Item.ItemIndex]["Url"].ToString();
}
}
}
}