我的数据库表中有 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();
}
}
已编辑
点击第 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 + "!";
}
}
应该可以。也许
grdResult_PageIndexChanging
事件不会触发
因此创建一个断点
int newPageIndex = e.NewPageIndex;
或在这条线上
grdResult.PageIndex = newPageIndex;
如果此事件没有运行,可能您的结果网格尚未附加此事件。请尝试重新连接,我想,有机会。
编辑:
最后用这个编辑
else if (invalid == "") {
SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
}