读书人

当前页的有关问题!希望高手们指点迷津

发布时间: 2012-01-19 20:57:58 作者: rapoo

当前页的问题!希望高手们指点迷津,谢谢了!(在线等)
cs:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
public int rscot;
String s;
//int num;
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
protected void BindData()
{
string txt = TextBox1.Text;
string connectionString = ConfigurationManager.ConnectionStrings[ "ConnectionString "].ConnectionString; //得到配置节
SqlConnection DBConnection = new SqlConnection(connectionString); //创建连接
string sqlStr = "select * from scrbtep where scid like ' " + txt + "% ' ";
SqlDataAdapter daGetData = new SqlDataAdapter(sqlStr, DBConnection);
DataSet dsData = new DataSet();
daGetData.Fill(dsData);
GridView1.DataSource = dsData;
rscot = dsData.Tables[0].Rows.Count;
GridView1.DataKeyNames = new string[] { "id " }; //主键
GridView1.DataBind();
daGetData.Dispose();
DBConnection.Close();
DBConnection.Dispose();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void Button1_Click(object sender, System.EventArgs e)
{

BindData();


}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int num = GridView1.PageIndex + 1;
Label2.Text = "第 " + (num).ToString() + "页/共 " + GridView1.PageCount + "页 ";

}
protected void gridClick_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add( "onmouseover ", "c=this.style.backgroundColor;this.style.backgroundColor= '#ECF0F4 ';this.style.cursor= 'hand ' ");
e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor=c ");
}
}


}


aspx:

<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 设备参数查询 </title>
<link rel= "stylesheet " href= "inc/CSS.CSS " type= "text/css ">
</head>
<body>
<form id= "form1 " runat= "server " action= "Default.aspx " >
<div style= "table-layout: auto " >
  <div style= "text-align: center ">
<table border= "0 " cellpadding= "0 " cellspacing= "0 " style= "width: 95% ">
<caption>
补充数据表
<asp:Label ID= "Label1 " runat= "server " Text= "设备名 "> </asp:Label>
: <asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>


  <asp:Button ID= "Button1 " runat= "server " Text= "查找 " OnClick= "Button1_Click " /> </caption>
<tr>
<td style= "height: 15px ">
</td>
<td style= "text-align: left; height: 15px; ">
<asp:Label ID= "Label2 " runat= "server " Text= " " > </asp:Label> / 总记录数: <%//=rscot%> </td>
<td style= "height: 15px ">
</td>
</tr>
<tr>
<td style= "height: 469px ">
</td>
<td style= "height: 469px ">
<asp:GridView ID= "GridView1 " runat= "server " Height= "450px " Width= "100% " AllowPaging= "True " OnPageIndexChanging= "GridView1_PageIndexChanging " AutoGenerateColumns= "False " EnableSortingAndPagingCallbacks= "True " OnRowCreated= "gridClick_RowCreated " OnSelectedIndexChanged= "GridView1_SelectedIndexChanged " >
<Columns>
<asp:BoundField DataField= "scid " HeaderText= "设备 " />
<asp:BoundField DataField= "zhi " HeaderText= "数值 " />
<asp:BoundField DataField= "zdtime " HeaderText= "日期 ">


<ItemStyle HorizontalAlign= "Center " VerticalAlign= "Middle " />
</asp:BoundField>
<asp:BoundField DataField= "newzhi " HeaderText= "新值 " />
<asp:BoundField DataField= "id " HeaderText= "序号 " />
</Columns>
<PagerSettings Position= "TopAndBottom " FirstPageText= ">> " />
<AlternatingRowStyle BorderStyle= "None " />
</asp:GridView>
</td>
<td style= "height: 469px " >
</td>
</tr>
<tr>
<td style= "height: 19px " >
</td>
<td style= "height: 19px " >
</td>
<td style= "height: 19px ">
</td>
</tr>
</table>
</div>

</div>


</form>
</body>
</html>


现在的问题是,我点击分页后无法获得当前页,也就是Label2.Text内容是空的,请各位高手指点!

[解决办法]
建议:
1。protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
里边改为
if(!this.IsPostback)
{
BindData();
}

2。如果是重新加载数据,那么grid要重新选择为选择第一页。
3。不明白为啥不把显示为第几页放在比如selectchange事件中,而是放在databind事件中


[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindData();
}
[解决办法]
调试下。
我觉得
int num = GridView1.PageIndex + 1;
Label2.Text = "第 " + (num).ToString() + "页/共 " + GridView1.PageCount + "页 ";
这两句应该在PageIndexChanging里边。

读书人网 >C#

热点推荐