Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
 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
Updated 17-Nov-12 21:50pm
v2
Comments
[no name] 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..
[no name] 18-Nov-12 3:57am    
Try to refresh DataGridView before Response.Redirect();

1 solution

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
C#
GridView1.DataSource = (DataSet)Session["mydataset"]; 
GridView1.DataBind();
 
Share this answer
 
Comments
[no name] 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

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