在asp.net中使数据列表项可链接

本文关键字:列表 链接 数据 asp net | 更新日期: 2023-09-27 18:08:39

我想让数据列表有可链接的项目,我可以导航到特定的url,当我点击它它会去c#做响应新的url

我的url不是常量

如何?

在asp.net中使数据列表项可链接

你应该编辑DataList的项目模板,并在里面放一个ASP超链接,你使用数据绑定来分配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属性。

<%@ 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();
        }
    }
  }

}