不同aspx页面上的下拉列表需要"关联"彼此

时间:2013-08-28 17:06:24

标签: c# jquery asp.net

我遇到的问题不是紧急情况,但我不知道该怎么办。我有两个aspx webform页面。每个都有一个下拉列表。两者都由来自sql server的相同数据源填充。问题是,如果我在第1页上选择一个值,然后转到第2页,它会重新填充下拉列表(因为它是从datasrouce中填充的)。例如,如果第2页下拉列表是汉密尔顿,然后单击第2页,则需要显示汉密尔顿。目前它将显示数据库中的第一条记录。这只是一个用户友好的功能,并不是一个问题。我不知道在这个上真的在哪里,所以我没有任何代码可以显示。这似乎是一个独特的问题,因为我找不到任何论坛。我不确定jQuery是否是一种有效的方式。这是我在页面加载期间填充下拉列表的方式..

protected void Page_Load(object sender,EventArgs e)     {

    //Session["dship"] = ddlDealershipRec.SelectedIndex;

    conn.Open();

    //This selects the user's ID where the user name equals the user that is currently logged in. 
    SqlCommand cmdUserID = new SqlCommand("SELECT UserID from Users WHERE UserName = '" + User.Identity.Name + "'", conn);
    selectUserID = Convert.ToString(cmdUserID.ExecuteScalar());

    //Selections the location ID where the userID is equal the the UserName.
    SqlCommand cmdLocationID = new SqlCommand("SELECT LocationID from UserControl WHERE UserID = '" + selectUserID + "'", conn);
    selectLocationID = Convert.ToString(cmdLocationID.ExecuteScalar());

    //Selects the Coporate or Store where the userID is equal to the UserName.
    SqlCommand cmdCorporateStore = new SqlCommand("SELECT MAX(CorporateStore) from Users WHERE UserID = '" + selectUserID + "'", conn);
    selectCorporateStore = Convert.ToString(cmdCorporateStore.ExecuteScalar());

    //Selects if the user is an Admin.
    SqlCommand cmdAdmin = new SqlCommand("SELECT MAX(Admin) from Users WHERE UserID = '" + selectUserID + "'", conn);
    selectAdmin = Convert.ToString(cmdAdmin.ExecuteScalar());

    conn.Close();

    //use to display "Garage" when an admin logs in.
    if (selectAdmin == "Yes")
    {
        Control allUsers = this.Page.Master.FindControl("login").FindControl("loginview").FindControl("ulmenu").FindControl("allUsers");
        allUsers.Visible = true;
    }

    gvVehicleTEMP.ControlStyle.Font.Size = 8;

    if (!IsPostBack)
    {
        ddlDealershipRec.Items.Clear();
        List<int> locationIDList = new List<int>();
        List<string> locationList = new List<string>();

        conn.Open();

        //used to populate the dropDownList depending who is logged in. 
        using (SqlDataReader reader = cmdLocationID.ExecuteReader())
        {
            while (reader.Read())
            {
                int locationID = reader.GetInt32(0);
                locationIDList.Add(locationID);
            }
            conn.Close();
        }

        foreach (int id in locationIDList)
        {
            conn.Open();
            SqlCommand cmdLocation = new SqlCommand("SELECT LocationName FROM Location WHERE LocationID = '" + id + "' ORDER BY LocationName ASC", conn);
            using (SqlDataReader reader = cmdLocation.ExecuteReader())
            {
                while (reader.Read())
                {
                    string location = reader.GetString(0);
                    locationList.Add(location);
                }
                conn.Close();
            }
        }

        foreach (string location in locationList)
        {
            ddlDealershipRec.Items.Add(new ListItem(location));
        }

    }

}

提前谢谢!

编辑:我已从第一页添加了整个页面加载。正如你所看到的,这里发生了很多事情,而且数据库驱动就像疯了一样。因为我正在使用Active Directory进行表单身份验证,所以我必须采用与预期不同的方式做事。 select cmdLocationID用于从“UserControl”表中获取数据。该表只有两列,UserID和LocationID。由于我的UserID是1,并且我拥有所有7或8个商店,因此有7个或8个UserID与1并且它与商店有关。然后我将其保存为字符串并将其添加到位置列表,然后填充下拉列表。

1 个答案:

答案 0 :(得分:0)

从高级视图,您可以做的是将选择的唯一标识符(如DropDownList的SelectedIndex)保存到用户状态,然后在加载另一个页面时,将DropDownList与该唯一标识符匹配。

There are many ways to save information in user state。一种方式是在会话状态。以下是如何在会话状态中保存信息:

Session["VariableName"] = (int)ddlNameOfDropdownList.SelectedIndex;

然后你可以在另一个页面上访问另一个页面上的Session变量(默认情况下它们会在20分钟内超时,所以如果你的表单花费的时间超过了这个,你可能想要使用像ViewState这样的东西。)

要访问会话变量并在另一个页面上使用它,可以在PageLoad中添加:

ddlOtherDropdownList.SelectedIndex = Convert.ToInt32(Session["VariableName"]);

当你访问会话变量时我没有检查空值,但那些是辅助问题。

编辑:添加了明确的演员。

编辑:您可能希望在第一页上的按钮中添加点击事件,将您重定向到第二页,如下所示:

void btnToSecondPage_Click(Object sender, EventArgs e)
{
    Session["dropdownSelected"] = (int)ddlDealershipRec.SelectedIndex;
    Response.Redirect("SecondPage.aspx");
}

这会将所选索引保存为会话变量,然后再转到下一页。然后,在第二页加载后,在填充ddlDealershipRec后,您会想要这样:

ddlDealershipRec.SelectedIndex = Convert.ToInt32(Session["dropdownSelected");