2013-08-28 34 views
2

我遇到的問題不是緊急情況,但我不知道該怎麼做。我有兩個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,它與商店有關。然後我將它保存到一個字符串並將其添加到位置列表中,然後填充下拉列表。

+1

您基本上只需要從ddl回發該ID。從那裏,您將第2頁上ddl的選定項目設置爲剛剛回發的ID。如果您顯示一些關於如何將數據返回給控制器的代碼,我們可以爲您提供更具體的幫助。 –

+0

@JonLaMarr上面是我在兩個頁面上填充下拉列表的方式。我在表中有8條記錄,一旦在頁面加載時,將列表中的項目添加到下拉列表中。正如你在我的foreach循環中看到的那樣,它添加了用戶有權訪問的位置。如果用戶有權訪問兩個位置,則只有那兩個位於下拉菜單中。我可以訪問所有位置,以便在登錄下拉列表後查看所有位置。 – Humpy

回答

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"]); 

我已經離開了檢查空當您訪問會話變量,但這些都是輔助問題。

編輯:增加了一個明確的演員。

編輯:你會想要一個click事件添加到按鈕將您重定向到第二頁,像這樣在第一頁上:

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

這節省了所選擇的指數作爲前一個會話變量進入下一頁。然後,在第二頁加載中,在填充了ddlDealershipRec之後,您需要:

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

很酷。我剛剛給了這個鏡頭。謝謝。 – Humpy

+0

我得到一個不能隱式地將類型'對象'轉換爲'int'。在使用ddlOtherDropDownList.SelectedIndex = Session [「VariableName」];的第二頁中存在明確的轉換(是否缺少轉換?並且該錯誤強調了Session [「VariableName」]。我錯過了什麼嗎?再次感謝! – Humpy

+0

我添加了Convert.ToInt32,它擺脫了錯誤,但它沒有解決問題。 – Humpy