0

Page_Load我在Web 表单的方法中有以下代码:

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点击事件处理程序,它SelectedValue从以下获取SelectList

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属性只返回列表中的第一个项目,而不管实际选择了哪个项目。我确定我遗漏了一些明显的东西,但我找不到问题;任何帮助深表感谢。

提前致谢。

戴夫

4

2 回答 2

0

您是正确的,您的问题源于 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;
于 2012-11-12T16:55:08.370 回答
0

将 MyButton.Click+=... 语句包装在 (!IsPostBack) 中,如

if(!IsPostBack)
{
   MyButton.Click += new EventHandler(MyButton_Click);
   BindSelectList();
}
于 2012-11-12T14:40:37.590 回答