page_load里面有一个默认的打开页面显示所有数据的搜索.
下面有一个约束搜索.
输入搜索条件以后..
点击下一页.
分页里面又出现所有数据.
也就是说触发NewPageIndex后读取的数据是页面的默认数据,并不是搜索的数据.
什么问题??
难道是默认的和约束的搜索必须使用2个DataGrid???
不会吧.....
<asp:DataGrid AllowPaging= "true " BorderColor= "#999999 " BorderWidth= "1 " CellPadding= "5 " CellSpacing= "0 " CssClass= "STYLE3 " ID= "dgrdMain " runat= "server " OnPageIndexChanged= "MyDataGrid_Page " PageSize= "5 " >
<PagerStyle NextPageText= "下一页 " PrevPageText= "上一页 " > </PagerStyle >
</asp:DataGrid > public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
DataBind();
}
下面有一个约束搜索.
输入搜索条件以后..
点击下一页.
分页里面又出现所有数据.
也就是说触发NewPageIndex后读取的数据是页面的默认数据,并不是搜索的数据.
什么问题??
难道是默认的和约束的搜索必须使用2个DataGrid???
不会吧.....
<asp:DataGrid AllowPaging= "true " BorderColor= "#999999 " BorderWidth= "1 " CellPadding= "5 " CellSpacing= "0 " CssClass= "STYLE3 " ID= "dgrdMain " runat= "server " OnPageIndexChanged= "MyDataGrid_Page " PageSize= "5 " >
<PagerStyle NextPageText= "下一页 " PrevPageText= "上一页 " > </PagerStyle >
</asp:DataGrid > public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
DataBind();
}
解决方案 »
- 小弟急需了
- (顶者有分)如何写一份OA报告~苦闷了!随便说点什么都好
- ASP.NET 2.0 中如何精确设置控件位置?
- ASP.net 的页面 怎设置居中显示
- doc转换pdf,cs成功,bs失败。
- 在datagrid中如何增加幾個新行(我搜索過N多次,看了兩天了)
- 编译错误!当前上下文不存在名称“UserService”!!怎么解决
- 哪位朋友有“ 征服ASP.NET Ajax典型应用”的源代码发给我一份好吗?
- 完了,完了,现在做系统没感觉了.
- 在发布asp.net时怎样将目标机器上的一个目录,设为应用程序.
- 请教一个底级问题关于ImageButton
- 如何双击GridView的一行,然后取得这行的值,并返回主页面,将值传给主页面的textbox中呢?
DataBind(); ==>
在databind中有没有考虑搜索条件?
DataBind(); ==>
在databind中有没有考虑搜索条件?怎么可能?
我原来是页面一切正常.默认的时候会显示数据库里面所有数据.
设定搜索条件以后.所有出来的数据只有一部分.
但是太多了.必须做一个分页.
做了分页后才出现的分页出错.
在数据绑定的时候写代码
//判断是否有搜索条件 购建sql语句
要么写两个不同的方法。要么写一个通用方法。在绑定的时候传递条件。dgrdMain.CurrentPageIndex = e.NewPageIndex;
DataBind(搜索参数);
再重新绑定
DataBind(搜索参数)
这里应该怎么来写。
只要你帮我解决问题。每天给你刷分都可以。
没看见我的帐号名字啊?
wenwenti3(问问题3)
你参考一下吧
* 模块名称: 行政用车申请列表模块
* 创建人: 叶明
* 创建日期: 2007-03-09
* 涉及数据表: OfficeUseBusRecord,EmployeeBaseInfo
* 最后修改人:
* 最后修改日期:
* 修改记录:
***************************************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
using TedaBus.RFPrincipal;public partial class OfficeUseBus_ApplyUseBusList : SimpleCode
{
DataSet ds = new DataSet("BusUseRecord"); //存储申请记录的数据集 protected override void OnInit(EventArgs e)
{
base.OnInit(e);
} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindUseBusRecordList(); //绑定行政用车申请GridView
}
} #region 显示/隐藏筛选条件
protected void btnShowFilter_Click(object sender, EventArgs e)
{
//设置筛选Panel的可见性
pnlFilter.Visible = !pnlFilter.Visible;
if (pnlFilter.Visible == false)
{
btnShowFilter.Text = "显示筛选条件";
}
else
{
btnShowFilter.Text = "隐藏筛选条件";
}
}
#endregion #region 绑定行政用车记录GridView
/// <summary>
/// 绑定行政用车记录GridView
/// 涉及数据表:OfficeUseBusRecord
/// </summary>
private void BindUseBusRecordList()
{
string sql = "SELECT * FROM OfficeUseBusRecord WHERE valid = 1 AND DeptID = " + GetCurrentUserDeptID();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
//对于数据视图的RowFilter选项,我先将其赋值为一个总是为真的条件,然后根据用户的选择逐步增加其限制
string filterString = "1 = 1";
//处理开始查询的日期
if (ViewState["StartDate"] != null)
{
if (ViewState["StartDate"].ToString() != string.Empty)
{
filterString += " and UserBusDate > #" + ViewState["StartDate"].ToString() + "#";
}
}
//处理最晚查询的日期
if (ViewState["EndDate"] != null)
{
if (ViewState["EndDate"].ToString() != string.Empty)
{
filterString += " and UserBusDate < #" + ViewState["EndDate"].ToString() + "#";
}
} dv.RowFilter = filterString; //处理GridView的排序
if (ViewState["sort"] != null)
{
if(ViewState["sort"].ToString() != string.Empty)
{
dv.Sort = ViewState["sort"].ToString();
}
else
{
dv.Sort = "";
}
} gvUseBusRecord.DataSource = dv;
gvUseBusRecord.DataBind();
}
#endregion #region 处理行政用车申请GridView的行绑定事件
protected void gvUseBusRecord_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //只处理数据行
{
//将GridView中的日期显示为短日期
DateTime dt = DateTime.Parse(e.Row.Cells[0].Text);
e.Row.Cells[0].Text = dt.ToShortDateString();
//把申请的批准状态映射为中文
DataRowView row = (DataRowView)e.Row.DataItem;
e.Row.Cells[0].ToolTip = row["RecordID"].ToString();
if ((int)row["Status"] == 0)
{
e.Row.Cells[5].Text = "未批准";
e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
}
else if ((int)row["Status"] == 1)
{
e.Row.Cells[5].Text = "已批准";
e.Row.Cells[5].ForeColor = System.Drawing.Color.Green;
//如果申请已经批准了,就不能撤销了
LinkButton lb = (LinkButton)e.Row.Cells[7].Controls[0];
lb.Enabled = false;
}
//设置查看LinkButton的PostUrl,这样用户点击的时候会转到申请查看的页面
LinkButton lbView = (LinkButton)e.Row.Cells[8].FindControl("lbView");
lbView.PostBackUrl = "ViewBusUseApply.aspx?RecordID=" + row["RecordID"].ToString();
//为撤销按钮添加确认对话框
LinkButton lbDelete = (LinkButton)e.Row.Cells[7].Controls[0];
lbDelete.Attributes.Add("OnClick","return confirm('您是否要撤销这个申请?');");
}
}
#endregion
protected void gvUseBusRecord_Sorting(object sender, GridViewSortEventArgs e)
{
//把GridView的排序表达式存储在ViewState中,然后重新绑定GridView
ViewState["sort"] = e.SortExpression;
BindUseBusRecordList();
}
#endregion #region 处理行政用车申请GridView翻页
protected void gvUseBusRecord_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//把GridVew得到PageIndex设置为新值,然后重新绑定GridView
gvUseBusRecord.PageIndex = e.NewPageIndex;
BindUseBusRecordList();
}
#endregion #region 设置筛选条件
protected void btnSetFilter_Click(object sender, EventArgs e)
{
//用户点击设置筛选条件的按钮时,我把筛选条件存入ViewState,具体的工作,绑定GridView的时候会处理
ViewState["StartDate"] = txtStart.Text;
ViewState["EndDate"] = txtEnd.Text;
BindUseBusRecordList();
}
#endregion #region 处理行政用车申请GridView行删除事件
protected void gvUseBusRecord_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//点击删除的时候,只是将Valid字段设置为0,并没有物理删除
//如果其他表中出现过的车辆被"删除",可以避免出现错误
string sql = "UPDATE OfficeUseBusRecord SET valid = 0 WHERE RecordID = " + gvUseBusRecord.Rows[e.RowIndex].Cells[0].ToolTip ;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
if(cmd.ExecuteNonQuery() > 0)
{
BindUseBusRecordList();
RegisterStartupScript("message","<script language='JavaScript'>alert('删除成功');</script>");
}
else
{
RegisterStartupScript("message","<script language='JavaScript'>alert('删除失败');</script>");
}
conn.Close();
conn.Dispose();
}
#endregion #region 获取当前用户所在的部门编号
/// <summary>
/// 利用当前用户的登录名获取当前用户所在的部门编号
/// 涉及数据表:EmployeeBaseInfo
/// </summary>
/// <returns>当前用户所在的部门编号</returns>
private string GetCurrentUserDeptID()
{
string result = "";
//获取当前登录的用户的员工编号
//P.S. 如果无法得到当前用户所在的部门,请在这里Response一下,很可能就是下面的变量没有得到
string userName = ((SitePrincipal)Context.User).UserCardNum.ToString(); if (userName != string.Empty)
{
string sql = "SELECT DepID FROM EmployeeBaseInfo WHERE EmployeeNo ='" + userName + "'";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
//因为只有一个字段,我用的是SqlCommand的ExecuteScalar方法
result = cmd.ExecuteScalar().ToString();
}
catch
{
result = "";
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
//Response.Write("dept:" + result + " user " + userName);
return result;
}
#endregion
}
{
DataBind(); //绑定行政用车申请GridView
}
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@Import Namespace="System"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<html>
<script runat="server" language="c#">
protected void Page_Load(object sender, EventArgs e)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("1.mdb");OleDbConnection objConnection=new OleDbConnection (strConnection);String ee5="update jinhuo,DS_ClassInf set jinhuo.classname2=DS_ClassInf.classname where jinhuo.ClassID2=DS_ClassInf.ClassID";
OleDbCommand objCommand115 = new OleDbCommand(ee5,objConnection);
objCommand115.Connection.Open();
objCommand115.ExecuteNonQuery();
objCommand115.Connection.Close();String sql = "select bianhao as 教材编号,bookname as 教材名称,shuliang as 进货数量,booktime as 进货时间,Classname as 类别2,Classname2 as 类别1 from jinhuo ORDER by [booktime] desc";
DataSet ds = new DataSet();
OleDbDataAdapter myada = new OleDbDataAdapter(sql,strConnection);
myada.Fill(ds,"jinhuo");
dgrdMain.DataSource = ds.Tables["jinhuo"].DefaultView;
dgrdMain.DataBind();
bianhao.Text="";
}public void bind(object sender,EventArgs E)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("1.mdb");OleDbConnection objConnection=new OleDbConnection (strConnection);String sql = "select bianhao as 教材编号,bookname as 教材名称,shuliang as 进货数量,booktime as 进货时间,Classname as 类别2,Classname2 as 类别1 from jinhuo where bianhao='" + bianhao.Text + "' and format([booktime],'yyyy-mm-dd HH:mm:ss')>='"+shijian1.Text+"' and format([booktime],'yyyy-mm-dd HH:mm:ss')<='"+shijian2.Text+"' ORDER by [booktime] desc";
DataSet ds = new DataSet();
OleDbDataAdapter myada = new OleDbDataAdapter(sql,strConnection);
myada.Fill(ds,"jinhuo");
dgrdMain.DataSource = ds.Tables["jinhuo"].DefaultView;
dgrdMain.CurrentPageIndex=0;
dgrdMain.DataBind();
}public void MyDataGrid_Page(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
dgrdMain.DataBind();
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE3 {font-size: 12px}
.STYLE5 {font-size: 9px}
-->
</style>
</head>
<body bgcolor="#F1F1F1">
<form name="form1" method="post" action="" runat="server">
<table width="674" border="0" align="left" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr bgcolor="#ffffff">
<td width="662" class="pad"></FONT>
<p><span class="STYLE3">从</span>
<asp:TextBox ID="shijian1" runat="server" Text='2007-01-01 24:00:00' TextMode="SingleLine" Width="150" CssClass="STYLE3"/>
<span class="STYLE3">至</span>
<asp:TextBox ID="shijian2" runat="server" Text='2010-12-30 24:00:00' Width="150" CssClass="STYLE3"/>
<p><span class="STYLE3">教材编号:</span>
<asp:TextBox ID="bianhao" runat="server"/>
<span class="STYLE3">
<asp:Button ID="Button1" Text="开始查询" OnClick="bind" runat="server" />
<asp:RangeValidator ID="bianhao1" runat="server"
ControlToValidate="bianhao" ErrorMessage="请输入正确的教材编号" Font-Bold="true" MaximumValue="10000000000000000" MinimumValue="1" Type="Double" Display="Dynamic"></asp:RangeValidator>
</span>
<td width="669" height="155"><asp:DataGrid AllowCustomPaging="false" AllowPaging="true" BorderColor="#999999" BorderWidth="1" CellPadding="5" CellSpacing="0" CssClass="STYLE3" ID="dgrdMain" PageSize="5" runat="server" OnPageIndexChanged="MyDataGrid_Page">
<PagerStyle NextPageText="下一页" PrevPageText="上一页"></PagerStyle>
</asp:DataGrid>
</td>
<td width="97" colspan="4" rowspan="2"> </td>
</tr>
<tr>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
dgrdMain.DataBind();
}
dgrdMain.DataBind();这个这样不行的,需要把前面page_load里的话重新写
建议这样
private void Bind()
{
//你在page_load里面的代码,自行选择下
}
protected void Page_Load(object sender, EventArgs e)
{
if(!PostBack)
{
Bind();
}
}System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
Bind();
}
Bind(string strSql)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("1.mdb");OleDbConnection objConnection=new OleDbConnection (strConnection);String ee5="update jinhuo,DS_ClassInf set jinhuo.classname2=DS_ClassInf.classname where jinhuo.ClassID2=DS_ClassInf.ClassID";
OleDbCommand objCommand115 = new OleDbCommand(ee5,objConnection);
objCommand115.Connection.Open();
objCommand115.ExecuteNonQuery();
objCommand115.Connection.Close();
DataSet ds = new DataSet();
OleDbDataAdapter myada = new OleDbDataAdapter(strSQL,strConnection);
myada.Fill(ds,"jinhuo");
dgrdMain.DataSource = ds.Tables["jinhuo"].DefaultView;
dgrdMain.DataBind(); }在使用的时候
Bind("select ……");