为asp.net的DataList / Repeater实现一个寻呼机

时间:2010-05-30 21:46:37

标签: asp.net javascript datalist pager

我有一个显示结果的DataList,以及一个应该是寻呼机的Repeater。 我决定使用QueryString参数重定向来使用页码,所以它也可以收藏...我的问题主要在UI中。

如果我把DIV用他们的onclick调用一个执行window.location = url + pagenumber的JS函数,那么我不得不通过JS来处理QueryString操作,这有点乱。

如果我在ItemTemplate中使用LinkBut​​ton对象,那么重定向只在单击链接按钮时发生,而整个div有光标:指针,你知道我的意思。

最好的方法是什么?提前谢谢。

注意:我真的想花时间和精力实现自己的服务器端分页。 我认为它比阅读第三方手册更有趣。

1 个答案:

答案 0 :(得分:0)

标记:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SoruListe.ascx.cs" Inherits="SoruListe" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register src="EtiketControl.ascx" tagname="EtiketControl" tagprefix="uc1" %>
<script type="text/javascript">
    $(document).ready(function() {
    if ($('.pageritem')) {
            $('#pageritem-' + get_pagenumber()).addClass("pagerselected");
        }
    });

    function soruyuac(id) {
        var url = "SoruDetay.aspx?sid=" + id;
        window.location = url;
    }

    function get_QueryString(fieldname) {
        var qstr = window.location.search.substring(1);
        var pairs = qstr.split('&');
        for (i = 0; i < pairs.length; i++) {
            var keyval = pairs[i].split('=');
            if (keyval[0] && keyval[0] == fieldname) {
                return keyval[1];
            }
        }
    }

    function set_QueryString(fieldname, value) {
        var rawurl = window.location.href;
        if (rawurl.indexOf('?') > -1) { rawurl = window.location.href.split('?')[0]; }
        var qstr = window.location.search.substring(1);
        var pairs = qstr.split('&');
        var foundit = false;

        for (i = 0; i < pairs.length; i++) {
            if (i == 0 && rawurl.indexOf('?') == -1) { rawurl += '?' };  ///Buraya kadar saglam görünüyo

            var curpair = pairs[i].split('=');
            if (curpair[0] == fieldname) { /// paramname'i al ama deger için yeni geleni koy
                foundit = true;
                rawurl += curpair[0] + '=' + value;
                if (i != pairs.length - 1) { rawurl += '&' }
            }
            else { ///aynen geri doldur
                rawurl += curpair[0] + '=' + curpair[1];
                if (i != pairs.length - 1) { rawurl += '&' }
            }
        }
        if (!foundit) { rawurl += '&' + fieldname + '=' + value; }
        ///
        return rawurl;
    }

    function changepage(pagenum) {
        window.location = set_QueryString('pg', pagenum);
    }

    function get_pagenumber() {
        var pgn = get_QueryString('pg');
        if (pgn == null) return 1;
        return pgn;
    }

    function skipfrom(from) {
        window.location = set_QueryString('skip', from);
    }
</script>

<div id="dvPager">
    <asp:Repeater ID="pagerSorular" runat="server">
    <ItemTemplate>
        <div class="pageritem pagertext" id='<%# "pageritem-" + Container.DataItem %>'
            onclick='<%# (Container.DataItem != "...")?("changepage(" + Container.DataItem + ");"): ("skipfrom(" + (this.PageNumber + 4) + ");") %>' >
            <%# Container.DataItem %>
        </div>
    </ItemTemplate>
    </asp:Repeater>
</div>

<asp:DataList ID="gridSorular" runat="server" Width="100%" OnItemDataBound="gridSorular_ItemDataBound">
    <ItemTemplate>
        <div class="soruwrapper">
            <asp:HiddenField ID="hfSoruID" runat="server" Value='<%# Eval("ID") %>' />
            <div class="viewsbox boxtext">
                <b class="boxtext"><%# Eval("VIEWS") %></b>
                <br />Okuyan
                <hr />
                <div class='<%# "answersbox boxtext " + ((int.Parse(Eval("ANSWERS").ToString()) > 0) ? "isanswered" : "notanswered")  %>'>
                    <b class="numtext"><%# Eval("ANSWERS")%></b>
                    <br />Cevap
                </div>
            </div>

            <div class="item" onclick='<%# "soruyuac(" + Eval("ID") + ");" %>'>
                <div class="soruheader" title='<%# Server.HtmlEncode(Eval("BODY").ToString())%>' >
                    <%# Eval("TITLE") %>
                </div>
                <div class="etiketwrapper">
                    <uc1:EtiketControl ID="EtiketControl1" runat="server" />
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:DataList>

服务器端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLayer;

public partial class SoruListe : System.Web.UI.UserControl
{
    public int PageNumber
    {
        get
        {
           string strPgNum = Request.QueryString["pg"] as String;
           if (!String.IsNullOrEmpty(strPgNum))
           {
               int pgnum;
               if (int.TryParse(strPgNum, out pgnum))
               {
                   if (pgnum <= 0)
                   {
                       return pgnum;
                   }
                   else
                   {
                       return 1;
                   }
               }
               else
               {
                   return 1;
               }
           }
           else
           {
               return 1;
           }
        }
    }
    public int PageSize { get { return 1; } }
    public string PageName {
        get
        {
            string url = Request.Url.ToString();
            string[] parts = url.Split(new char[]{'/'});
            return parts[parts.Length - 1];
        }
    }

    public void Page_Load(object sender, EventArgs e)
    {

    }

    public void SonSorular()
    {
        gridSorular.DataSource = BLL.SonSorular(300,300);
        gridSorular.DataBind();
        ///
    }

    protected void CreatePager()
    {
        int pagenumber = this.PageNumber;
        int start = 1;
        if (PageNumber > 3)
        {
            start = pagenumber - 3;
        }
        int finish = pagenumber + 3;
        int sorucount;
        using (DataAccessDataContext db = new DataAccessDataContext())
        {
            sorucount = db.Sorus.Count();
        }

        List<string> pageritemlist = new List<string>();

        int c = start;
        for (int i = 0; i < finish; i++)
        {
            pageritemlist.Add((c++).ToString());
        }
        pageritemlist.Add("...");
        pageritemlist.Add(sorucount.ToString());

        pagerSorular.DataSource = pageritemlist.ToArray();
        pagerSorular.DataBind();
    }

    public void ListAll()
    {
        CreatePager();
        int pagesize = this.PageSize;
        int skip = this.PageNumber * pagesize;
        ListSorular(skip, pagesize);
    }
    public void Popular()
    {

    }
    public void Active()
    {

    }
    public void Unanswered()
    {

    }
    public void ListSorular(int skip, int take)
    {
        using (DataAccessDataContext db = new DataAccessDataContext())
        {
            List<SoruGridView> sorular = (from s in db.Sorus
                                          select new SoruGridView() {
                                          ID = s.ID,  TITLE = s.TITLE, BODY = s.BODY, TARIH = s.DATECREATED, VIEWS = s.VIEWS, ANSWERS = s.Cevaps.Count
                                          }).Skip(skip).Take(take).ToList();
            gridSorular.DataSource = sorular;
            gridSorular.DataBind();
        }
    }

    protected void gridSorular_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
        {
            HiddenField hfsoruid = e.Item.FindControl("hfSoruID") as HiddenField;
            if (hfsoruid != null)
            {
                int sid;
                if (int.TryParse(hfsoruid.Value, out sid))
                {
                    EtiketControl tagsctrl = (EtiketControl)e.Item.FindControl("EtiketControl1");
                    tagsctrl.GetTags(sid);
                }
            }
        }
    }
}

还有我编写的JavaScript查询字符串解析器:)