关闭

DataList的数据绑定

标签: hyperlinkserveraspbbsstringhtml
6162人阅读 评论(1) 收藏 举报

 第一次接触DataList的时候,以为就像GridView一样绑,其实不然.DataList需要对每一列都进行绑定.如                         <%# Eval("FTWTime") %>

   可以先通过vs.net 2005 里 DataList 自带的"选择数据源"绑定方法进行绑定,然后再删除DataList的DataSourceID里面的值,然后在自己写绑定方法.

还有DataList的翻页...................(例:)

HTML源码:
        <asp:Label ID="lblallpage" runat="server" ForeColor="Red" Text="Label"></asp:Label>
        <asp:Label
            ID="lblpageMessage" runat="server" ForeColor="Red" Text="Label"></asp:Label>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" Width="768px" BackColor="WhiteSmoke" GridLines="Horizontal">
        <ItemTemplate>
            主题:
            <asp:Label ID="FTitleLabel" runat="server" Text='<%# Eval("FTitle") %>'></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                提问时间:<asp:Label ID="FTWTimeLabel" runat="server" Text='<%# Eval("FTWTime") %>'></asp:Label>
                <br />
            问题:
                <asp:Label ID="FContentLabel" runat="server" Text='<%# Eval("FContent") %>' ForeColor="#0000C0"></asp:Label>
                <br />
            答复:
                <asp:Label ID="FanswerLabel" runat="server" Text='<%# Eval("Fanswer") %>' ForeColor="#0000C0"></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                Font-Underline="False" ForeColor="Blue" />
        </asp:DataList><asp:HyperLink ID="prvpage" runat="server">[上一页]</asp:HyperLink>
        <asp:HyperLink ID="nextpage" runat="server">[下一页]</asp:HyperLink>&nbsp;<br />

后台代码:

public void bindBase_BBS()
    {
        string strBBS = "select fid,FContent,FTitle,FBaseId,FTWTime,FState,Fanswer,FHDTime from Base_BBS where Fanswer != '' and (FState =1 or FBaseId=" + QY_ID + ") order by FTWTime desc";    
        ds = DBO.Select(CnnString, strBBS, "Base_BBS");
        if (ds.Tables["Base_BBS"].Rows.Count > 0)
        {
            //this.DataList1.DataSource = ds;
            //this.DataList1.DataBind();
            this.lblallpage.Text = "共有" + Convert.ToString(ds.Tables[0].Rows.Count) + "条数据";
            PagedDataSource objpage = new PagedDataSource();
            objpage.DataSource = ds.Tables[0].DefaultView;
            //允许分页
            objpage.AllowPaging = true;
            //每页显示的行数
            objpage.PageSize = 3;
            //定义页码
            int CurPage;
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            objpage.CurrentPageIndex = CurPage - 1;
            this.lblpageMessage.Text = "[当前第" + Convert.ToString(CurPage) + "页]";
            if (!objpage.IsFirstPage)
            {
                this.prvpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
            }
            if (!objpage.IsLastPage)
            {
                this.nextpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            }
            this.DataList1.DataSource = objpage;
            this.DataList1.DataBind();
        }
        else
        {
            Response.Write("<script>alert('没有留言信息!')</script>");
                   }
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111159次
    • 积分:1703
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:7篇
    • 译文:0篇
    • 评论:34条
    最新评论