我遇到的問題不是緊急情況,但我不知道該怎麼做。我有兩個aspx webform頁面。每個都有一個下拉列表。兩者都由來自sql server的相同數據源填充。問題是,如果我在第1頁上選擇一個值,然後轉到第2頁,它會重新填充下拉列表(因爲它是從datasrouce填充的)。例如,如果第2頁下拉列表是漢密爾頓,然後點擊第2頁漢密爾頓需要顯示。目前它將顯示數據庫中的第一條記錄。這只是更多的用戶友好功能,並不是什麼問題。我不知道該在哪裏,因此我沒有任何代碼可以顯示。這似乎是一個獨特的問題,因爲我找不到任何論壇。我不確定jQuery是否會成爲這樣做的有效方式。下面是我填充下拉頁面加載過程中列出的方式..不同的aspx頁面下拉列表需要「相互關聯」
保護無效的Page_Load(對象發件人,EventArgs的){
//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。由於我的用戶ID是1,並且我擁有所有7或8個商店,所以有7或8個用戶ID和1,它與商店有關。然後我將它保存到一個字符串並將其添加到位置列表中,然後填充下拉列表。
您基本上只需要從ddl回發該ID。從那裏,您將第2頁上ddl的選定項目設置爲剛剛回發的ID。如果您顯示一些關於如何將數據返回給控制器的代碼,我們可以爲您提供更具體的幫助。 –
@JonLaMarr上面是我在兩個頁面上填充下拉列表的方式。我在表中有8條記錄,一旦在頁面加載時,將列表中的項目添加到下拉列表中。正如你在我的foreach循環中看到的那樣,它添加了用戶有權訪問的位置。如果用戶有權訪問兩個位置,則只有那兩個位於下拉菜單中。我可以訪問所有位置,以便在登錄下拉列表後查看所有位置。 – Humpy