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();
}
4

1 回答 1

1

问题: 当您单击下一页时,下拉菜单也会重新绑定,因此选择的索引更改为 0。

解决方案: 当您按下搜索按钮时,您必须保存 3 个下拉列表的选定值。下拉菜单存在于您的 gridview 中,因此首先您必须获取 gridview 行索引。您可以从 gridveiw_selectedindexchange 事件中获取行索引。[暗示]

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

// also get the selected values of other 2 dropdowns 

现在,在获得 selectedvalues 后,您必须将 dropdwonlist.selected 值设置为我们保存在 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
}

希望它对你有用。

于 2012-07-18T14:17:12.770 回答