0

I have a GridView and I populate it via a DATASET . Three of its columns are three DropDownLists and AllowPaging is set to true for the grid view. My problem is when I choose a value on anyone of the ddl and click on serach button i get the data on the gridview which will have paginig, but when i click on the second page i loose teh filtered calue onthe drop down and i again get the earlier dataset.

Is there any way/idea to persist the selected values? Thanks for your help. Please can you help me with this. if i am not filtering anyone of teh dropdown and then clicking on the second or third page i get the relevant data of that particular page. teh only problem is when i have a value selected on the dropdown .

code:

button click: { string _strBU = BUDropDownList.SelectedValue; string _strOU = OUDropDownList.SelectedValue;

string _strPortalID = !string.IsNullOrEmpty(TxtEmpPortalID.Text.Trim()) ?   TxtEmpPortalID.Text.Trim() : string.Empty;
string _strRU = RUDropDownList.SelectedValue;
string _strMngrPortalID = System.Web.HttpContext.Current.User.Identity.Name.ToString();
_strMngrPortalID = _strMngrPortalID.Substring(4, 6);

            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = "server=;uid=;pwd=;database=HROrgchartDB";
            sqlConnection.Open();

            SqlCommand sqlEmployeeDetailsCommand = new SqlCommand();
            sqlEmployeeDetailsCommand.Connection = sqlConnection;
            sqlEmployeeDetailsCommand.CommandText = "EmployeeSearch";
            sqlEmployeeDetailsCommand.CommandType = CommandType.StoredProcedure;
            sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("@BU", SqlDbType.VarChar, 50)).Value = _strBU;
            sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("@OU", SqlDbType.VarChar, 50)).Value = _strOU;
            sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("@PORTALID", SqlDbType.VarChar, 6)).Value = _strPortalID;
            sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("@RU", SqlDbType.VarChar, 50)).Value = _strRU;
            sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("@ManagerPortalID", SqlDbType.VarChar, 6)).Value = _strMngrPortalID;

            SqlDataAdapter da = new SqlDataAdapter(sqlEmployeeDetailsCommand);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds != null)
            {
                // gvAddorRelease.Visible = true;
                gridReportees.DataSource = ds;
                Cache["D2"] = ds;
                gridReportees.PageIndex = 0;
                gridReportees.DataBind();
            }
            else
            {


            }
            sqlConnection.Close();
        }
}

event for paging :

gridReportees_PageIndexChanging:
{
gridReportees.PageIndex = e.NewPageIndex;
DataSet ds = new DataSet();
ds=(DataSet)Cache["D2"];
gridReportees.DataSource= ds;
gridReportees.DataBind();
}

1 Answers1

1

Problem: When you click on the next page, dropdown also rebinds due to which there selected index changed to 0.

Solution: When you press the search button at that time you have to save the selectedvalues of the 3 dropdownlists. the dropdowns are present inside your gridview,so first you have to get the gridview row index. You can get the row index from gridveiw_selectedindexchange event. [Hint]

ViewState["svalue1"] = ((DropDownList) gv.Rows[index].FindControl("dropdownlistID")).Text;

// also get the selected values of other 2 dropdowns 

Now after getting the selectedvalues you have to set the dropdwonlist.selected value to the values which we have saved in the ViewState.

gridReportees_PageIndexChanging()
{
  // After binding the grid
  // in this metho set the dropdown seleted value
  // example get a reference of your dropdown
   DropDownList ddl1 = (DropDownList) gv.Rows[index].FindControl("dropdownlistID");
   ddl.SelectedValue = ViewState["svalue1"].ToString();
  // follow the same steps for other 2 dropdownlists
}

Hope that it works for you.

Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
  • i followed but i am getting null for ddl.SelectedValue = ViewState["svalue1"].ToString(); in the page indexchanging i need to have paging with the filter ( that would be loaded with one dataset) and if there is a filter i need to load the filtered dataset and the paging shoul work for this as well – tulajaram kulkarni Jul 19 '12 at 04:56
  • @tulajaramkulkarni do you save the value in the viewstate correctly ? – Waqar Janjua Jul 19 '12 at 06:14
  • I Think i figured it out... the view state value i will get from dropdownlist select index changing and nit from gridview select indexchange. anyways Thanks for your supprt – tulajaram kulkarni Jul 19 '12 at 09:57
  • But one more thing after i filter and search i get the data and also the paging works... but once i click on the back button on the explorer the selected value of dropdown remains intact but the gridview refreshes to the original data,, and after this if i click on the paging it will go to the dropdown filterd second page... hope you got the point – tulajaram kulkarni Jul 19 '12 at 14:17
  • @tulajaramkulkarni do one thing in the page_load event check the ispostback property if the page is not a postback then set the viewstates of all the dropdwons to null. – Waqar Janjua Jul 19 '12 at 14:23
  • can you tell me how do i do it,, cose my default value for all the drop downs will be "Select something" – tulajaram kulkarni Jul 19 '12 at 14:40
  • @tulajaramkulkarni i mean in your page_load event. do this if(!IsPostBack) { ViewState["svalue1"] = null; } do this for all dropdowns. if not work then do this for if(IsPostBack). – Waqar Janjua Jul 19 '12 at 14:57
  • when i click on back button nothing happens,i mean the page does not load. i tried putting break point to see if the page loads but the page not loads i donno why but the paeg will show reatin the dropdown value and the gird view shows the old data. – tulajaram kulkarni Jul 20 '12 at 05:56
  • @tulajaramkulkarni yes please ask – Waqar Janjua Jul 20 '12 at 10:46
  • when i click on back button nothing happens,i mean the page does not load. i tried putting break point to see if the page loads but the page not loads i donno why but the paeg will show reatin the dropdown value and the gird view shows the old data. – tulajaram kulkarni Jul 20 '12 at 11:21
  • see this http://stackoverflow.com/questions/2175285/what-happens-when-i-press-browser-back-button – Waqar Janjua Jul 20 '12 at 12:11