我有一個web表單的Page_Load
方法如下代碼:另一個視圖狀態下拉列表的問題
protected void Page_Load(object sender, EventArgs e)
{
CountrySelectButton.Click += new EventHandler(CountrySelectButton_Click);
if (HomePage.EnableCountrySelector) //always true in in this case
{
if(!IsPostBack)
BindCountrySelectorList();
}
}
的BindCountrySelectorList
方法是這樣的:
private void BindCountrySelectorList()
{
NameValueCollection nvc = HttpUtility.ParseQueryString(HomePage.CountryList);
var ds = nvc.AllKeys.Select(k => new { Text = k, Value = nvc[k] });
CountrySelector.DataSource = ds;
CountrySelector.DataTextField = "Text";
CountrySelector.DataValueField = "Value";
CountrySelector.DataBind();
}
而且我有一個LinkButton
click事件處理器從SelectList
得到SelectedValue
如下:
void CountrySelectButton_Click(object sender, EventArgs e)
{
//get selected
string selectedMarket = CountrySelector.SelectedValue; //this is always the first item...
//set cookie
if (RememberSelection.Checked)
Response.Cookies.Add(new HttpCookie("blah_cookie", selectedMarket) { Expires = DateTime.MaxValue });
//redirect
Response.Redirect(selectedMarket, false);
}
編輯:
這是DDL和LinkButton的定義:
<asp:DropDownList runat="server" ID="CountrySelector" />
<asp:LinkButton runat="server" ID="CountrySelectButton" Text="Go" />
結果標記:
<select name="CountrySelector" id="CountrySelector">
<option value="http://google.com">UK</option>
<option value="http://microsoft.com">US</option>
<option value="http://apple.com">FR</option>
</select>
<a id="CountrySelectButton" href="javascript:__doPostBack('CountrySelectButton','')">Go</a>
編輯完
ViewState是允許的,但SelectedValue
財產只有永遠回報是列表中的第一個項目,而不管實際選擇哪個項目。我確定我錯過了一些明顯的東西,但我找不到問題;任何幫助深表感謝。
在此先感謝。
戴夫
更多的代碼,將有助於。 – MikeSmithDev
@MikeSmithDev - 特別想看到什麼?這是背後的所有代碼,因此我不確定我還能提供什麼。 – DaveParsons
是的,您的選擇列表的代碼前面。更多關於LinkButton的事件處理程序。鏈接按鈕代碼前面。你有沒有調試過,看看發生了什麼,就像你在某處重新綁定一樣? – MikeSmithDev