Click here to Skip to main content
12,881,824 members (28,048 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
I have a gridview which gets loaded on click of button and in the gridview i have button on click of which i navigate to another page. and in second page i have a back buttobn to navigate to the gridview page. but when i am navigating back to the page the grid view is not retaning the values.plzz help

This is code in first page where i have my grid on click of button
 protected void BtnviewEmployee_Click(object sender, EventArgs e)
{
  if (DdlCity.SelectedIndex == 0)
        {
            cityid = branchid = deptid = 0;
        }
        else
        {
            cityid=Convert.ToInt64(DdlCity.SelectedValue.ToString());
            branchid=Convert.ToInt64(DdlBranch.SelectedValue.ToString());
            if (branchid == 0)
            {
                deptid = 0;
            }
            else
            {
                deptid = Convert.ToInt64(DdlDepartment.SelectedValue.ToString());
 
            }
 
        }
        
       
        countryid = Convert.ToInt64(Ddlcountry.SelectedValue.ToString());
        if (countryid != 0 && cityid != 0 && branchid != 0 && deptid != 0)
        {
            dsinfo = bl.BsearchEmp(countryid, cityid, branchid, deptid);
        }
        else if (countryid != 0 && cityid != 0 && branchid != 0)
        {
            dsinfo = bl.searchEmployee(countryid, cityid, branchid);
        }
        else if (countryid != 0 && cityid != 0 )
        {
            dsinfo = bl.searchEmployee(countryid, cityid);
        }
        else
        {
            dsinfo = bl.searchEmployee(countryid);
        }
 

        
       
        Session["mydataset"] = dsinfo;
        GridView1.DataSource = (DataSet)Session["mydataset"];
        GridView1.DataBind();
        BtnDumpToExcel.Visible = true;
        BtnPrint.Visible = true;
}
  protected void Get_Employee_Id_from_grid(object sender, EventArgs e)
    {
        Button btn= sender as Button;
        if (btn != null)
        {
            string Employee_Id = btn.CommandArgument;
            
            Session["Gridview_Employee_id"] = Employee_Id;
 
            Response.Redirect("ViewEmployeeDetail.aspx");
        }

after coming back from ViewEmployeeDetail.aspx i want to see the same grid which is loaded before.
and my grid is in update panel ang i made my button as a trigger to that grid.

please advice..
Posted 17-Nov-12 21:26pm
Updated 17-Nov-12 21:50pm
__TR__29K
v2
Comments
Krunal R 18-Nov-12 3:27am
   
Can you show your code.??
pinky1810 18-Nov-12 3:37am
   
This is code in first page where i have my grid on click of button
protected void BtnviewEmployee_Click(object sender, EventArgs e)
{
if (DdlCity.SelectedIndex == 0)
{
cityid = branchid = deptid = 0;
}
else
{
cityid=Convert.ToInt64(DdlCity.SelectedValue.ToString());
branchid=Convert.ToInt64(DdlBranch.SelectedValue.ToString());
if (branchid == 0)
{
deptid = 0;
}
else
{
deptid = Convert.ToInt64(DdlDepartment.SelectedValue.ToString());

}

}


countryid = Convert.ToInt64(Ddlcountry.SelectedValue.ToString());
if (countryid != 0 && cityid != 0 && branchid != 0 && deptid != 0)
{
dsinfo = bl.BsearchEmp(countryid, cityid, branchid, deptid);
}
else if (countryid != 0 && cityid != 0 && branchid != 0)
{
dsinfo = bl.searchEmployee(countryid, cityid, branchid);
}
else if (countryid != 0 && cityid != 0 )
{
dsinfo = bl.searchEmployee(countryid, cityid);
}
else
{
dsinfo = bl.searchEmployee(countryid);
}




Session["mydataset"] = dsinfo;
GridView1.DataSource = (DataSet)Session["mydataset"];
GridView1.DataBind();
BtnDumpToExcel.Visible = true;
BtnPrint.Visible = true;
}
protected void Get_Employee_Id_from_grid(object sender, EventArgs e)
{
Button btn= sender as Button;
if (btn != null)
{
string Employee_Id = btn.CommandArgument;

Session["Gridview_Employee_id"] = Employee_Id;

Response.Redirect("ViewEmployeeDetail.aspx");
}

after coming back from ViewEmployeeDetail.aspx i want to see the same grid which is loaded before.
and my grid is in update panel ang i made my button as a trigger to that grid.

please advice..
Krunal R 18-Nov-12 3:57am
   
Try to refresh DataGridView before Response.Redirect();

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Bind the gridview on pageLoad event , check for the session and bind the grid.

If I am not wrong , you have to write these codes on Page_Load event
GridView1.DataSource = (DataSet)Session["mydataset"]; 
GridView1.DataBind();
  Permalink  
Comments
Krunal R 18-Nov-12 4:00am
   
Nope it is on ButtonClick Event...
Pro Idiot 18-Nov-12 4:03am
   
"after coming back from ViewEmployeeDetail.aspx" , the page , to which you are coming back , that is the 1st page.
Write the above code, in it's Pageload event , and hence the grid will be autopopulated
pinky1810 18-Nov-12 4:04am
   
I wrote this code but if the dataset is null than it displays error, how to check for whether tae dataset is null or not. here i am getting error.
please advice
Pro Idiot 18-Nov-12 4:06am
   
Just check for session null
if(Session["mydataset"] != null)
{
DataSet dataSet = (DataSet)Session["mydataset"];
if(dataSet != null)
{
GridView1.DataSource = (DataSet)Session["mydataset"];
GridView1.DataBind();
}
}
Pro Idiot 18-Nov-12 4:23am
   
If this helped ,Please Mark as answer
pinky1810 18-Nov-12 5:19am
   
Thaks Pro.
Its working.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170422.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100