更改页面大小或点击分页后,GridView丢失了搜索结果
本文关键字:GridView 搜索结果 分页 | 更新日期: 2023-09-27 18:12:04
我有一个gridview显示数据和文本框做搜索。当我搜索时,gridview可以正确显示数据,但当我点击更改页面大小或转到下一页时,gridview将重新加载到原始状态。有什么与ViewState有关的吗?感谢有人能帮上忙。下面是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
// handle event
DropDownList ddpagesize = sender as DropDownList;
GridView1.PageSize = Convert.ToInt32(ddpagesize.SelectedItem.Text);
ViewState["PageSize"] = ddpagesize.SelectedItem.Text;
GridView1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "" + txtSearchValue.Text + "'";
GridView1.DataBind();
}
protected void btnReload_Click(object sender, EventArgs e)
{
txtSearchValue.Text = "";
lblSearchError.Text = "";
GridView1.DataBind();
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Attributes["onmouseover"] =
"this.style.backgroundColor;this.style.backgroundColor='#AED4EB';";
row.Attributes["onmouseout"] =
"this.style.textDecoration='none';this.style.background='#ffffff';";
// Set the last parameter to True
// to register for event validation.
row.Attributes["onclick"] =
Page.ClientScript.GetPostBackClientHyperlink(GridView1,
"Select$" + row.DataItemIndex, true);
}
}
base.Render(writer);
}
protected void btnDate_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "";
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int rowindex = GridView1.SelectedIndex % GridView1.PageSize;
GridViewRow row = GridView1.Rows[rowindex];
Session["TSO"] = row.Cells[7].Text.Trim();
string url = "http://localhost:60918/Requests.aspx";
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type='text/javascript'>");
sb.AppendLine("window.open('" + url + "')");
sb.AppendLine("<" + "/script>");
ScriptManager.RegisterStartupScript(upGrdCustomers, upGrdCustomers.GetType(), "myjs", sb.ToString(), false);
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
}
这是我的代码为GridView:
[ToolboxData("<{0}:GridView runat=server></{0}:GridView>")]
public class GridView : System.Web.UI.WebControls.GridView, IPageableItemContainer
{
/// <summary>
/// TotalRowCountAvailable event key
/// </summary>
private static readonly object EventTotalRowCountAvailable = new object();
/// <summary>
///
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataBinding"></param>
/// <returns></returns>
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
// if the paging feature is enabled, determine
// the total number of rows in the datasource
if (this.AllowPaging)
{
// if we are databinding, use the number of rows that were created,
// otherwise cast the datasource to an Collection and use that as the count
int totalRowCount = dataBinding ? rows : ((ICollection)dataSource).Count;
// raise the row count available event
IPageableItemContainer pageableItemContainer = this as IPageableItemContainer;
this.OnTotalRowCountAvailable(
new PageEventArgs(
pageableItemContainer.StartRowIndex,
pageableItemContainer.MaximumRows,
totalRowCount
)
);
// make sure the top and bottom pager rows are not visible
if (this.TopPagerRow != null)
{
this.TopPagerRow.Visible = false;
}
}
return rows;
}
#region IPageableItemContainer Interface
/// <summary>
///
/// </summary>
/// <param name="startRowIndex"></param>
/// <param name="maximumRows"></param>
/// <param name="databind"></param>
void IPageableItemContainer.SetPageProperties(
int startRowIndex, int maximumRows, bool databind)
{
int newPageIndex = (startRowIndex / maximumRows);
this.PageSize = maximumRows;
if (this.PageIndex != newPageIndex)
{
bool isCanceled = false;
if (databind)
{
// create the event args and raise the event
GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex);
this.OnPageIndexChanging(args);
isCanceled = args.Cancel;
newPageIndex = args.NewPageIndex;
}
// if the event wasn't cancelled
// go ahead and change the paging values
if (!isCanceled)
{
this.PageIndex = newPageIndex;
if (databind)
{
this.OnPageIndexChanged(EventArgs.Empty);
}
}
if (databind)
{
this.RequiresDataBinding = true;
}
}
}
/// <summary>
///
/// </summary>
int IPageableItemContainer.StartRowIndex
{
get { return this.PageSize * this.PageIndex; }
}
/// <summary>
///
/// </summary>
int IPageableItemContainer.MaximumRows
{
get { return this.PageSize; }
}
/// <summary>
///
/// </summary>
event EventHandler<PageEventArgs> IPageableItemContainer.TotalRowCountAvailable
{
add { base.Events.AddHandler(GridView.EventTotalRowCountAvailable, value); }
remove { base.Events.RemoveHandler(GridView.EventTotalRowCountAvailable, value); }
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected virtual void OnTotalRowCountAvailable(PageEventArgs e)
{
EventHandler<PageEventArgs> handler = (EventHandler<PageEventArgs>)base.Events[GridView.EventTotalRowCountAvailable];
if (handler != null)
{
handler(this, e);
}
}
#endregion
}
}
在您的服务器端事件处理程序称为ddPageSize_SelectedIndexChanged中,您正在网格上调用Databind,而不检查是否有一个搜索词输入并在网格的数据源上设置。
protected void ddPageSize_SelectedIndexChanged(对象发送方,EventArgs e)
{…
SqlDataSource1。SelectCommand = " + txtSearchValue。
GridView1.DataBind ();}
试试这个。当您更改pagesize
时,它会触发回发并将SelectCommand
的值保留为空,从而导致gridview以其原始状态加载。设置为txtSearchValue.Text
并尝试
protected void ddPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
// handle event
DropDownList ddpagesize = sender as DropDownList;
GridView1.PageSize = Convert.ToInt32(ddpagesize.SelectedItem.Text);
ViewState["PageSize"] = ddpagesize.SelectedItem.Text;
SqlDataSource1.SelectCommand = "" + txtSearchValue.Text + "'";
GridView1.DataBind();
}