【ASP.NET】DataList控件之ItemTemplate模式(自定义模板)

一、DataList呈现效果图如下:

二、前端控件代码:

    <asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand" runat="server">
        <ItemTemplate>
            <table border="0" runat="server" style="width: 100%; height: 100%; margin-top: 0px; padding-top: 0px; font-size: 9pt;" align="center">
                <tr>
                    <td align="left" style="border-bottom: 1px dashed #000000; width: 150px; height: 21px;">&nbsp;·&nbsp;『
                                                <%# DataBinder.Eval(Container.DataItem,"type") %>
                                            』
                    </td>
                    <td style="border-bottom: 1px dashed #000000;" align="left">
                        <asp:LinkButton ID="IbtnTitle" runat="server" CommandName="select">
                                                <%# DataBinder.Eval(Container.DataItem,"title") %>
                        </asp:LinkButton>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>

其中,<ItemTemplate></ItemTemplate>里的内容就是自定义模板的内容。

最关键的一步是绑定数据库表的数据字段,即  <%# DataBinder.Eval(Container.DataItem,"type") %> 和    <%# DataBinder.Eval(Container.DataItem,"title") %>

总结:   <%# DataBinder.Eval(Container.DataItem,"DataList的DataSource的某个字段名") %>,一般就是数据库表的字段名

三、后台相关代码:

    /// <summary>
    /// 连接数据库
    /// </summary>
    /// <returns>返回SqlConnection对象</returns>
    public SqlConnection GetConnection()
    {
        //获取数据库连接的字符串并赋值给myStr
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }   

    /// <summary>
    /// 说明:GetDataList 数据集,返回数据源的数据集        
    /// </summary>
    /// <param name="sQueryString">SQL字符串</param>
    /// <param name="TableName">数据表名称</param>
    /// <returns>数据集DataSet</returns>
    public DataSet GetDataSet(string sQueryString, string TableName)
    {
        SqlConnection con = GetConnection();
        con.Open();
        //定义并初始化数据适配器
        SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, con);
        //定义一个数据集,用来赋值给应用程序的一个数据集
        DataSet dataset = new DataSet();
        //将数据适配器中的数据填充到数据集dataset中
        dbAdapter.Fill(dataset, TableName);
        con.Close();
        return dataset;
    }
   protected void Page_Load(object sender, EventArgs e)
   {
      DataSet ds = GetDataSet("select * from tb_News", "tb_News");
        this.DataList1.DataSource = ds;
        this.DataList1.DataKeyField = "id";
        this.DataList1.DataBind();
   }

tb_News表中就有ID,Tyle, Title字段

而其中,超链接LinkButton的点击事件是DataList的OnItemCommand事件,会将点击的列表子项信息传递给该事件对应的方法!

注意:1、LinkButton的CommandName必须设置为"select"才会触发该事件。

           2、this.DataList1.DataKeyField = "id"  必须写上,不然下面获取不了主键ID

web.config中在<configuration></configuration>中,添加:

 <appSettings>    
      <add key="ConnectionString" value="user id=sa; password=123456; database=db_news; server=(local)" />  
  </appSettings>

注意:这是连接SQL SERVER的方法,不是MYSQL(别搞错了!) 

四、点击LinkButton(超链接)触发的事件方法如下:

        /// <summary>
        /// 点击超链接文字后事件,跳转showNews显示新闻详细信息
        /// </summary>
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
            Response.Write("<script language=javascript>window.open(showNews.aspx?id=" + id +
                ",width=520,height=260</script>");
        }

通过DataList1.DataKeys[e.Item.ItemIndex].ToString()来获取表的ID字段,而e就是所点击的那行信息体。

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值