Repeater显示数据--[Asp.Net]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HyperLink.aspx.cs" Inherits="WebApplication1.HyperLink" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <asp:hyperlink ID="Hyperlink2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"name") %>' NavigateUrl ='<%# "HyperLink.aspx?id=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem,"id").ToString()) %>'>HyperLink</asp:hyperlink> </ItemTemplate> </asp:Repeater> </form> </body> </html>
using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class HyperLink : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //本地内存中创建一个表 DataTable dt = new DataTable(); //声明一个新的行对象 DataRow dr; dt.Columns.Add(new DataColumn("id", typeof(Int32)));//创建一个名为ID dt.Columns.Add(new DataColumn("name", typeof(String)));//创建类型为字符串类型 dt.Columns.Add(new DataColumn("dat", typeof(DateTime)));//创建日期类型 for (int i = 0; i < 5; i++) { dr = dt.NewRow();//新建一行数据 dr[0] = i;//第一列数据为i dr[1] = "第" + i + "项";//第二列数据为第i项 dr[2] = DateTime.Now;//时间是现在 //将上面填充好的行添加到表里面 dt.Rows.Add(dr); } //指定Repeater控件的数据源为dt表 Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { //改变控件的链接指向 HyperLink1.NavigateUrl = "http://www.baidu.com"; //加粗字体 HyperLink1.Font.Bold = true; //链接文字颜色 HyperLink1.ForeColor = Color.Azure; //背景色为蓝色 HyperLink1.BackColor = Color.Blue; //改变链接的文字 HyperLink1.Text = "我现在是指向洪川医药的了"; } public object DataItem { get; set; } } }
显示效果:
创建一个临时的数据库。有意识,最终还是我战胜了你!嘎嘎