我試圖找回這樣對以前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項目的字符串值?
因爲DropDownListReception.SelectedItem.Value這不會工作,返回一個字符串,而不是一個對象 – Juanda 2014-09-04 16:28:20
@Juanda不selecteditem.value唯一的SelectedItem – Sajeetharan 2014-09-04 16:30:22
噢,對不起。仍然不會工作,因爲DropDownListReception.SelectedItem是一個ListItem,我可以這樣做:(DropDownListReception.SelectedItem as ListItem).Value但這仍然會返回「接待」,而不是項目的價值。 – Juanda 2014-09-04 16:33:46