我遇到的问题不是紧急情况,但我不知道该怎么做。我有两个 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,它与商店有关。然后我将其保存为字符串并将其添加到位置列表中,然后填充下拉列表。