当前页的问题!希望高手们指点迷津,谢谢了!(在线等)

cuoban 2007-05-14 03:50:35
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="&gt;&gt;" />
<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内容是空的,请各位高手指点!
...全文
415 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuoban 2007-05-15
  • 打赏
  • 举报
回复
up
cuoban 2007-05-14
  • 打赏
  • 举报
回复
为什么GridView1_RowDataBound事件不起作用呢?
cuoban 2007-05-14
  • 打赏
  • 举报
回复
现在Label2.Text已经显示内容了问题出在
<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" >
现改为:
<asp:GridView ID="GridView1" runat="server" Height="450px" Width="100%" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True" OnRowCreated="gridClick_RowCreated" >

可是点击分页时Label2.Text没有变化,还是启动页面后的初始值,只有点击Button1后Label2.Text才能显示当前页。郁闷中~~~~~~!
whslovexyp 2007-05-14
  • 打赏
  • 举报
回复
承认失误,我也不太记得大小写,误导LZ了
james_hunter 2007-05-14
  • 打赏
  • 举报
回复
要区分大小写的,IsPostback不对,IsPostBack才对
whslovexyp 2007-05-14
  • 打赏
  • 举报
回复
楼上给你写了,呵呵
james_hunter 2007-05-14
  • 打赏
  • 举报
回复
调试下。
我觉得
int num = GridView1.PageIndex + 1;
Label2.Text = "第" + (num).ToString() + "页/共" + GridView1.PageCount + "页";
这两句应该在PageIndexChanging里边。
amandag 2007-05-14
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindData();
}
cuoban 2007-05-14
  • 打赏
  • 举报
回复
to whslovexyp(菜鸟学习中ing)

我用过IsPostback可提示
“_Default”并不包含“IsPostback”的定义”
whslovexyp 2007-05-14
  • 打赏
  • 举报
回复
建议:
1。protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
里边改为
if(!this.IsPostback)
{
BindData();
}

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

mdl821120 2007-05-14
  • 打赏
  • 举报
回复
太长了,帮你顶

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧