2014-09-04 57 views
0

我試圖找回這樣對以前databinded的DropDownList值:下拉列表不返回正確的值

<asp:DropDownList ID="DropDownListReception" runat="server" CssClass="span3 drop-down-reception" 
      OnPreRender="DropDownListReception_PreRender" OnSelectedIndexChanged="DropDownListReception_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> 

protected void Page_Load(object sender, EventArgs e) 
    { 
     var receptions = BLLFactory.ReceptionBLL.GetListAll(); 
     DropDownListReception.DataSource = receptions; 
     DropDownListReception.DataBind(); 
    } 

的下拉的PreRender我很個性化此下拉是這樣的:

protected void DropDownListReception_PreRender(object sender, EventArgs e) 
    { 
     if (DropDownListReception.DataSource != null) 
     { 
      DropDownListReception.Items.Clear(); 
      DropDownListReception.Items.Add(new ListItem("-- Select --", "NA")); 
      foreach (Reception item in (DropDownListReception.DataSource as IEnumerable)) 
      { 
       DropDownListReception.Items.Add(new ListItem(item.Name + " " + item.Number, item.Id.ToString())); 
      } 
     } 
    } 

這是完美的工作,我的DropDown加載,因爲它應該,我的問題是當我嘗試檢索SelectedIndexChanged事件SelectedValue,它不會返回值作爲一個字符串,但作爲一種類型,我在做什麼是:

protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //CurrentReception is a string i want to save in ViewState 
     //I also tried (sender as DropDownList).SelectedValue 
     //Tried DropDownListReception.SelectedValue 
     CurrentReception = DropDownListReception.SelectedItem.Value; 
    } 

但這個「DropDownListReception.SelectedItem.Value」將始終返回作爲項目類型的「接待」,而不是我在PreRender事件中作爲項目值分配的標識。這也發生如果我這樣做:「DropDownListReception.SelectedItem.Text」,這也返回「接待」。我如何返回分配給DropDown項目的字符串值?

回答

0

我想通了,我是DataBinding PageLoad上的DropDownList,它在SelectedIndexChanged事件之前觸發。由於DropDown在其值發生更改時執行PostBack,因此PageLoad是「重新創建」DropDown,並且在獲取SelectedIndexChanged代碼之前我正在丟失更改。

謝謝大家對你的答案。:)

0
var CurrentReception = DropDownListReception.SelectedItem as Reception; 
string val = CurrentReception.PropertyYouNeed; 
+0

因爲DropDownListReception.SelectedItem.Value這不會工作,返回一個字符串,而不是一個對象 – Juanda 2014-09-04 16:28:20

+0

@Juanda不selecteditem.value唯一的SelectedItem – Sajeetharan 2014-09-04 16:30:22

+0

噢,對不起。仍然不會工作,因爲DropDownListReception.SelectedItem是一個ListItem,我可以這樣做:(DropDownListReception.SelectedItem as ListItem).Value但這仍然會返回「接待」,而不是項目的價值。 – Juanda 2014-09-04 16:33:46

0

DropDownListReception.SelectedItem.Text和DropDownListReception.SelectedItem.Value將返回的選擇,這是在列表項中的第二項使用時將其添加到列表中的值。換句話說,問題在於item.Id.ToString()。它返回對象的類型而不是ID。我不確定你的item對象實際上包含了什麼,所以我不確定你實際需要什麼,但是你確定它不只是item.Id? ToString()通常是對象的字符串表示形式,如果item.Id是一個int,那麼ToString應該給你這個int的字符串......但是它不起作用的事實表明item.Id並不是實際的一個int。

+0

item.id.ToString()是由於該項目的id是一個UniqueIdentifier,它表示爲c#上的Guid。這是完美的,問題是SelectedIndexChanged在DataBinding DropDown的PageLoad之後觸發。 – Juanda 2014-09-04 17:07:04

0

我認爲你需要轉換列表項爲您存儲在它(接收)的類型,然後從您想要的接待對象訪問屬性(從你的描述中聽起來像你想要的ID)。就像這樣:

protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //CurrentReception is a string i want to save in ViewState 
    CurentReception = ((Reception)DropDownListReception.SelectedItem).Id.ToString(); 
} 
+0

因爲DropDownListReception.SelectedItem是一個ListItem,所以不能完成。 – Juanda 2014-09-04 17:12:55