2014-02-23 61 views
3

內的下拉列表中選擇的值,我打算給下拉列表中選擇值從C#代碼更改:改變嵌套轉發

<repeater > 
    <repeater> 
     <dropdown> 

,我在aspx頁面的下拉 每一件事工作正常,添加一個數據源。但是我想改變下拉 的設定值的onload能夠顯示用戶以前選擇的名稱時,編輯表

protected void PrepairDropDownList(object sender,EventArgs e) 
    { 
     shiftrepeater.DataBind(); 
     if (shiftrepeater.Items.Count > 0) 
     { 
      for (int shiftcount = 0; shiftcount < shiftrepeater.Items.Count; shiftcount++) 
      { 
       Repeater temp = (Repeater)shiftrepeater.Items[shiftcount].FindControl("saturdayrepeater"); 
       if (temp.Items.Count > 0) 
       { 
        for (int count = 0; count < temp.Items.Count; count++) 
        { 
         DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown"); 
         HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");//contain the id if the field 
         SarcShiftUser user = CRUD<SarcShiftUser>.Get(int.Parse(hf.Value)); //a method to select a user with a specific id and add it to object from class sarcshiftuser 

         if (user.id == 0) 
          ds.SelectedValue = ""; 
         else 
         { 
          ds.SelectedValue = user.user_id + ""; 

         } 
        } 
       } 

      } 
     } 
     shiftrepeater.DataBind(); 
    } 

我加入這個方法的Onload中繼器: 但沒有任何變化,以前的名字沒有顯示

P.S:用戶對象是正確的,user.user_id也是正確的 P.S 2:我試圖添加一個ds.DataBind();改變選擇,但給該錯誤後:

System.ArgumentOutOfRangeException: 'userdropdown' has a SelectedValue which is invalid because it does not exist in the list of items. 
+0

不知道從哪裏開始,你怎麼綁定/填充你的下拉菜單?您的下拉列表的值是[user.user_id +「」]? –

+0

」SelectCommand =「select id,firstname +''+ lastname as sarcuser name」> value:<%#Eval(「id」)%> text:<%#Eval(「name」)%> –

回答

1

設置DropdownListSelectedItem最保險的辦法是設置SelectedIndex這樣的:

ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString())); 
+0

仍然不能正常工作......它採用兩種方式的正確值...但它沒有改變,所以選定的值仍然爲空 –

+0

@ Samy Sammour - 確保'DropDownList'的'DataValueField'被設置爲'user_id'或者代表'user_id'的東西。 – afzalulh

+0

我很確定,但我想問題是,當我從中繼器的下拉,它是一個副本不採取真正的一個 –