2010-05-30 118 views
0

我有一個顯示結果的DataList和一個應該是尋呼機的Repeater。 我決定通過重定向使用QueryString參數來使用頁碼,所以它也將是可收藏的...我的問題主要在UI中。爲asp.net的DataList/Repeater實現尋呼機

如果我把DIV與他們的onclick調用一個JS函數做window.location = url + pagenumber,那麼我不得不通過JS處理QueryString操作,這有點麻煩。

如果我在ItemTemplate中使用LinkBut​​ton對象,那麼重定向僅在單擊linkbutton時發生,而整個div有光標:pointer,你知道我的意思。

什麼是最好的方法呢?提前致謝。

注:我真的很想花時間和精力實現我自己的服務器端分頁。 我覺得它比閱讀第三方手冊更有趣。

回答

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的查詢字符串解析器:)