2012-11-12 26 views
0

我有一個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和LinkBut​​ton的定義:

<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財產只有永遠回報是列表中的第一個項目,而不管實際選擇哪個項目。我確定我錯過了一些明顯的東西,但我找不到問題;任何幫助深表感謝。

在此先感謝。

戴夫

+0

更多的代碼,將有助於。 – MikeSmithDev

+0

@MikeSmithDev - 特別想看到什麼?這是背後的所有代碼,因此我不確定我還能提供什麼。 – DaveParsons

+0

是的,您的選擇列表的代碼前面。更多關於LinkBut​​ton的事件處理程序。鏈接按鈕代碼前面。你有沒有調試過,看看發生了什麼,就像你在某處重新綁定一樣? – MikeSmithDev

回答

1

你是正確的,你的問題,從jQuery UI的對話框梗...你可以得到通過使用隱藏字段來記錄下拉列表的值。然後在你的代碼中,引用隱藏的字段。

前端可能看起來像:

<div id="myModal" style="display: none;"> 
     <asp:DropDownList runat="server" ID="SelectList" /> 
     <asp:LinkButton runat="server" ID="MyButton" Text="Go" /> 
    </div> 
    <input type="hidden" id="countryVal" runat="server" /> 
    <a id="choose" href="#">Choose</a> 
    <script type="text/javascript"> 
     $(document).ready(function() { 

      $('#choose').click(function() { 
       $('#myModal').dialog({ 
       }); 
       return false; 
      }); 

      $('#<%= SelectList.ClientID %>').change(function() { 
       var country = $(this).val(); 
       $('#<%= countryVal.ClientID %>').val(country); 
      }); 
     }); 
    </script> 

那麼後面的代碼:

var selected = countryVal.Value; 
+0

如果您使用隱藏字段,則必須執行您已獲得的附加驗證,如果您使用DropDownList。另一種方法就是綁定'$(document).on('submit',function()...'抓住jQuery模式容器,並將其追加回'

'。jQuery模式將輸入元素移動到''元素,這就是爲什麼價值觀不符合你的預期。 –

+0

不錯的建議,我需要檢查一下。 – MikeSmithDev

0

包裹MyButton.Click + = ...語句中(!的IsPostBack)像

if(!IsPostBack) 
{ 
    MyButton.Click += new EventHandler(MyButton_Click); 
    BindSelectList(); 
} 
+0

感謝您的建議,但這會阻止我的按鈕在事件發回時綁定,並沒有幫助解決問題。 – DaveParsons