gpt4 book ai didi

c# - 不同aspx页面上的下拉列表需要 "relate"互相

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:08 25 4
gpt4 key购买 nike

我遇到的问题不是紧急情况,但我不知道该怎么做。我有两个 aspx 网络表单页面。每个都有一个下拉列表。两者都由来自 sql server 的相同数据源填充。问题是,如果我在第 1 页选择一个值,然后转到第 2 页,它会重新填充下拉列表(因为它是从数据源中填充的)。例如第2页下拉列表是Hamilton,然后点击第2页Hamilton需要显示。目前它将显示数据库中的第一条记录。这只是变得更加用户友好的功能,并不是什么大问题。我不知道真正在这个地方在哪里,所以我没有任何代码可以显示。这似乎是一个独特的问题,因为我找不到任何关于此的论坛。我不确定 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,并且它与商店相关。然后我将它保存到一个字符串并将它添加到位置列表基本上然后填充下拉列表。

最佳答案

从高层次的角度来看,您可以做的是将选择的唯一标识符(如 DropDownList 的 SelectedIndex)保存到用户状态,然后在其他页面加载时,将 DropDownList 与该唯一标识符匹配。

There are many ways to save information in user state .一种方式是在 session 状态中。以下是在 session 状态中保存信息的方法:

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

然后您可以在另一个时间在另一个页面上访问该 Session 变量(默认情况下它们会在 20 分钟内超时,因此如果您的表单花费的时间比这更长,您可能需要使用 ViewState 之类的东西)。

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

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

当您访问 session 变量时,我没有检查空值,但这些都是辅助问题。

编辑:添加了显式转换。

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

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

这会在转到下一页之前将所选索引保存为 session 变量。然后,在第二个页面加载时,在填充 ddlDealershipRec 之后,您会希望这样:

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

关于c# - 不同aspx页面上的下拉列表需要 "relate"互相,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com