Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to keep the grid view which appears as a result of searching by id in the text field when the page id posted back. To illustrate, I have the link on grid view table takes me to another page then I have on the page link go back that grid view is not found because the page in posted back.


please help

C#
//pageload
protectedvoidPage_Load(objectsender,EventArgse)
{
Cache["MId"]=TextBox7.Text;
if(TextBox7.Text!=null)
{
view_back();
rep_bind();
}
if(IsPostBack)
{
label10.Text="";
view_back();
Cache["MId"]=TextBox7.Text;
grid.Visible=true;
}
}
//searchcode
protectedvoidSearch(objectsender,EventArgse)
{
if(TextBox7.Text!=null)
{
SqlConnectioncon1=newSqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con1.Open();
stringquery="SElECtt1.*,t2.*,t3.*fromdbo.MaterialASt1INNERJOINdbo.ModuleRelationAst2ONt2.MID=t1.MIDINNERJOINdbo.UsersASt3ONt3.UniID=t2.UIdwheret3.user_type='Staff'andt1.IsDeleted=0ANDt2.MID='"+TextBox7.Text+"'";
SqlCommandcom=newSqlCommand(query,con1);
SqlDataReaderdr;
dr=com.ExecuteReader();
if(dr.HasRows)
{
dr.Read();
rep_bind();
label10.Text="";
grid.Visible=true;
}
if(!dr.HasRows)
{
label10.Text="ModuleIDNotFound!";
grid.Visible=false;
}
}
}
//methodfordisplayinggridview
privatevoidrep_bind()
{
if(TextBox7.Text!=null)
{
SqlConnectioncon1=newSqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con1.Open();
stringquery="SElECtt1.*,t2.*,t3.*fromdbo.MaterialASt1INNERJOINdbo.ModuleRelationAst2ONt2.MID=t1.MIDINNERJOINdbo.UsersASt3ONt3.UniID=t2.UIdwheret3.user_type='Staff'andt1.IsDeleted=0ANDt2.MID='"+TextBox7.Text+"'";
SqlDataAdapterda=newSqlDataAdapter(query,con1);
DataSetds=newDataSet();
da.Fill(ds);
GridView1.DataSource=ds;
GridView1.DataBind();
}
}
//forclickonviewlinkongridviewtoredirecttoaontherpage
publicvoidView()
{
Response.Redirect("View.aspx?Id="+(MaID));
}
//thisIdidtokeepthecontentsofthepagewhenitloadedandIcalledinloadmethodbutnotworkingjustitkeepserrormessageforif(!dr.HasRows)
privatevoidview_back()
{
if(TextBox7.Text!=null)
{
SqlConnectioncon1=newSqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con1.Open();
stringquery="SElECtt1.*,t2.*,t3.*fromdbo.MaterialASt1INNERJOINdbo.ModuleRelationAst2ONt2.MID=t1.MIDINNERJOINdbo.UsersASt3ONt3.UniID=t2.UIdwheret3.user_type='Staff'andt1.IsDeleted=0ANDt2.MID='"+TextBox7.Text+"'";
SqlCommandcom=newSqlCommand(query,con1);
SqlDataReaderdr;
dr=com.ExecuteReader();
if(dr.HasRows)
{
dr.Read();
rep_bind();
label10.Text="";
grid.Visible=true;
}
if(!dr.HasRows)
{
label10.Text="ModuleIDNotFound!";//thisappearswhenthepageispostedback
grid.Visible=false;
}
}
}
Posted
Updated 14-Aug-16 23:57pm
v2

1 solution

Your Approach looks good, did you get any errors while doing so?

please refer
asp.net - Maintaining GridView current page index after navigating away from Gridview page - Stack Overflow[^]
 
Share this answer
 
Comments
Member 12618369 15-Aug-16 6:35am    
thanks
I have found on this page on stack overflow this code but I could not understand what is the pageNumber and how can I get it, even thought, I need to show the grid view with its contents which based on search by ID not the permanent contents of the page
protected void GridViewIndexChanging(object sender, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
Session["pageNumber"] = e.NewPageIndex;
//whatever your page index changing does...
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900