为 asp.net 的 DataList/Repeater 实现寻呼机
Posted
技术标签:
【中文标题】为 asp.net 的 DataList/Repeater 实现寻呼机【英文标题】:Implementing a pager for asp.net's DataList/Repeater 【发布时间】:2010-05-30 21:46:37 【问题描述】:我有一个显示结果的 DataList 和一个应该是寻呼机的中继器。 我决定通过使用 QueryString 参数重定向来使用页码,因此它也可以加入书签...我的问题实际上主要在 UI 中。
如果我将 DIV 与他们的 onclick 一起调用一个执行 window.location = url + pagenumber 的 JS 函数,那么我必须以某种方式通过 JS 处理 QueryString 操作,这有点混乱。
如果我在 ItemTemplate 中使用 LinkButton 对象,则重定向仅在整个 div 具有 cursor: pointer 时单击链接按钮时发生,您知道我的意思。
最好的方法是什么?提前致谢。
注意:我真的很想花时间和精力来实现我自己的服务器端分页。 我认为这比阅读第 3 方手册更有趣。
【问题讨论】:
【参考方案1】:标记:
<%@ 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 查询字符串解析器 :)
【讨论】:
以上是关于为 asp.net 的 DataList/Repeater 实现寻呼机的主要内容,如果未能解决你的问题,请参考以下文章