首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

dwz 表头排序解决方法

2012-09-24 
dwz 表头排序dwz 如何使用 表头排序,给个详细例子 带截图的 我用的是asp.net[解决办法]asp.net 点击表头排

dwz 表头排序
dwz 如何使用 表头排序,给个详细例子 带截图的 我用的是asp.net

[解决办法]

asp.net 点击表头排序及分页代码 


 
aspx:
 
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script language="javascript" type="text/javascript">
var getPageData=function(i,pageCount)
{
$.ajax({
url:'....aspx?'+new Date()+'&page='+i+'&url='+document.getElementById('ctl00_ContentArea_hidUrl').value+'&pageCount='+pageCount+'&Type='+document.getElementById('ctl00_ContentArea_hidType').value,
type:'get',
success:function(data,textStatus)
{
// $('#ShowData').text(data);
// $('#ShowData')[0].innerHTML="";
//$('#ShowData').innerHTML=data;
document.getElementById('ShowData').innerHTML=data;
},
error:function(XMLHttpRequest,textStatus)
{
//debugger;
$('#ShowData').text(XMLHttpRequest.responseText);
},
complete:function(XMLHttpRequest,textStatus)
{

}
});
}
var sortDataGridView=function(sortExpression,sortDirection)
{
//event.returnVaule=false;
$.ajax({
url:'.....aspx?'+new Date()+'&sortEx='+sortExpression+'&url='+document.getElementById('ctl00_ContentArea_hidUrl').value+'&Type='+document.getElementById('ctl00_ContentArea_hidType').value,
type:'get',
success:function(data,textStatus)
{
document.getElementById('ShowData').innerHTML=data;
//$('#ShowData').innerHTML = data;
},
error:function(XMLHttpRequest,textStatus)
{
$('#ShowData').text(XMLHttpRequest.responseText);
},
complete:function(XMLHttpRequest,textStatus)
{

}

});
}
</script>
 
 
 
aspx.cs:
 
Page_Load:
 
 //jquary sort
if (hasKeyName("page"))
{
if (!string.IsNullOrEmpty(Request.QueryString["page"].ToString()))
{
this.GridView1.PageIndex = int.Parse(Request.QueryString["page"].ToString());
ResponseData();
}
}
else
{
if (hasKeyName("sortEx"))
{
sortEx = Request.QueryString["sortEx"].ToString();
try
{
this.GridView1.Sort(sortEx, SortDirection.Ascending);
}
catch (Exception ex)
{
string info = ex.Message;
}
}
}
 
 
 
other:
 
 private bool hasKeyName(string key)
{
string[] keys = Request.QueryString.AllKeys;
foreach (string str in keys)
{
if (String.Compare(key, str, true) == 0)
return true;
}
return false;
}
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
string firstHeader = string.Empty;

for (int i = 0; i < e.Row.Cells.Count; i++)
{

LinkButton lt = (LinkButton)e.Row.Cells[i].Controls[0];
lt.Attributes["href"] = "#";


lt.OnClientClick = string.Format(" return sortDataGridView('{0}','{1}')", lt.CommandArgument, "ASC");
if (i == 0)
{
firstHeader = lt.CommandArgument.Trim();
}
}
Page.RegisterStartupScript("123456", "<script>javascript:sortDataGridView('" + firstHeader + "','ASC');</script>");
}
if (e.Row.RowType == DataControlRowType.Pager)
{
e.Row.Visible = false;
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
this.GridView1.EditIndex = -1;
e.SortExpression = sortEx;
if (string.Compare(sortDirection, "ASC", true) == 0)
{
sortDirection = "DESC";
}
else
{
sortDirection = "ASC";
}
ResponseData();
}
 
private void ResponseData()
{
DataTable dt = (DataTable)Session["App_RealData" + hidTimeStamp.Value];
DataView dw = dt.DefaultView;
GridView1.PageSize = pageSize;
this.GridView1.DataSource = dw;
if (!string.IsNullOrEmpty(sortEx))
{
dw.Sort = sortEx + " " + sortDirection;
}
System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter sWriter = new System.IO.StringWriter(info);
System.Web.UI.HtmlTextWriter html = new HtmlTextWriter(sWriter);
GridView1.DataBind();
if (GridView1 != null)
{
GridView1.RenderControl(html);
}
Response.Write(html.InnerWriter);
Response.Write(GetNav());
Response.Flush();
Response.End();
}
 
 
 
static int leftPage = 0;
public string GetNav()
{
if (hasKeyName("pageCount"))
{
leftPage = Convert.ToInt32(Request.QueryString["pageCount"]);
}
int pageCount = 10;
//总分页数
int a = GridView1.PageCount / pageCount;
int b = GridView1.PageCount % pageCount == 0 ? 0 : 1;
int allIndexCount = a + b;
string NavStr = @"<table><tr>";
if (GridView1.PageCount < pageCount)
{
pageCount = GridView1.PageCount;
}
if (leftPage > 0)
{
NavStr += @"<td><a href='#' onclick='getPageData(" + ((leftPage-1)*pageCount + 1).ToString() + ","+(leftPage-1).ToString()+")'>..." + @"&nbsp;&nbsp;" + @"</a></td>";
}
int length = pageCount * (leftPage+1);
if((leftPage+1) == allIndexCount)
{
length = GridView1.PageCount % pageCount + pageCount * leftPage-1;
}
for (int i = pageCount * leftPage; i < length; i++)
{
NavStr = NavStr + @"<td><a href='#' onclick='getPageData(" + (i + 1).ToString() + "," + leftPage.ToString() + ")'>" + (i + 1).ToString() + @"&nbsp;&nbsp;" + @"</a></td>";
}
if (GridView1.PageCount > pageCount && (leftPage+1) < allIndexCount)
{
NavStr += "<td><a href='#' onclick='getPageData(" + (pageCount * leftPage + 1).ToString() + "," + (leftPage+1) .ToString()+ ")'>..." + @"&nbsp;&nbsp;" + @"</a></td>";
}
NavStr = NavStr + @"</tr></table>";


return NavStr;
}

热点排行