分页上有多个下拉列表和值

时间:2012-07-18 09:26:09

标签: asp.net

我有一个GridView,我通过DATASET填充它。其中三列是三个DropDownLists,而网格视图的AllowPaging设置为true。我的问题是,当我在ddl的任何一个上选择一个值并点击serach按钮时我会得到gridview上的数据,这将有paginig,但是当我点击第二页时,我松了teh过滤下来的calue我再次得到早期的数据集。

是否有任何方式/想法可以保留所选值?谢谢你的帮助。 请你帮帮我。如果我没有过滤任何一个下拉菜单,然后点击第二或第三页,我会得到该特定页面的相关数据。只有问题是我在下拉列表中选择了一个值。

代码:

按钮点击:     {     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();
        }
}

分页事件:

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

1 个答案:

答案 0 :(得分: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 

现在获得选定值后,您必须将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
}

希望它适合你。