我的数据库表中有7条记录,名为EntryTable。我将页面限制设置为每页5条记录。因此第1页将有5条记录,第2页将有2条记录。但是在我单击“全部搜索”并单击第2页后,整个网格视图将消失。
搜索全部后
点击第2页后
public partial class Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
bindResultGridView();
}
string memName = (String)Session["UserName"];
if (Session["Username"] != null && Session["Username"] != String.Empty)
{
//txtUserName.Text = "Welcome, " + memName + "!";
}
}
protected void SearchBlog(object sender, EventArgs e)
{
String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(ConStr);
try
{
string invalid = txtSearch.Text;
String SQL = null;
if (invalid == "all")
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}
else
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
}
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();
cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchBlogType"].Value = txtSearch.Text + "%";
cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchName"].Value = txtSearch.Text + "%";
cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text + "%";
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
grdResult.DataSource = dt;
grdResult.DataBind();
lblError.Text = "";
if (dt.Rows.Count > 0)
{
lblError.Text = null;
}
else
{
lblError.Text = "Record not found";
}
reader.Close();
}
catch (Exception)
{
lblError.Text = "Error!";
//lblOrderError.Text = ex.Message;
}
finally
{
con.Close();
}
}
private void bindResultGridView()
{
String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(ConStr);
try
{
string invalid = txtSearch.Text;
String SQL = null;
if (invalid == "all")
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}
else
{
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
}
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();
cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchBlogType"].Value = txtSearch.Text;
cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchName"].Value = txtSearch.Text;
cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text;
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
grdResult.DataSource = dt;
grdResult.DataBind();
lblError.Text = "";
reader.Close();
}
catch (SqlException ex)
{
lblError.Text = "Error:" + ex.Message.ToString();
}
finally
{
con.Close();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
}
protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (string.Compare(e.CommandName, "Page", true) != 0)
{
Response.Redirect("~/ResultDetails.aspx?cat=" + e.CommandArgument);
}
}
protected void grdResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPageIndex = e.NewPageIndex;
grdResult.PageIndex = newPageIndex;
bindResultGridView();
}
}
EDITED
点击第2页后
protected void Page_Load(object sender, EventArgs e)
{
bindResultGridView();
string memName = (String)Session["UserName"];
if (Session["Username"] != null && Session["Username"] != String.Empty)
{
//txtUserName.Text = "Welcome, " + memName + "!";
}
}
答案 0 :(得分:1)
它应该工作。也许grdResult_PageIndexChanging
事件不会触发
所以在
上创建一个断点 int newPageIndex = e.NewPageIndex;
或在这一行
grdResult.PageIndex = newPageIndex;
如果此事件未运行,可能您的结果网格尚未附加此事件。请尝试重新连接它,我认为,机会会起作用。
编辑:
最后使用此
进行编辑 else if (invalid == "") {
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}