另一种视图状态的DropDownList问题 [英] Yet another viewstate dropdownlist issue

查看:85
本文介绍了另一种视图状态的DropDownList问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单的的Page_Load 方法如下code:

I have the following code in the Page_Load method of a web form:

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();
}

和我有一个的LinkBut​​ton 单击事件处理它得到的的SelectedValue 的SelectList 像这样:

And I have a LinkButton click event handler which gets the SelectedValue from the SelectList as so:

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的定义:

This is the DDL and LinkButton definition:

<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>

END修改

ViewState是允许的,但的SelectedValue 属性,无论哪个项目实际上是选择了永远只能返回列表中的第一项。我敢肯定,我失去了一些东西明显,但我不能发现问题;任何帮助是非常AP preciated。

ViewState is enabled but the SelectedValue property only ever returns the first item in the list regardless of which item is actually selected. I'm certain I'm missing something obvious but I can't find the problem; any help is much appreciated.

先谢谢了。

戴夫

推荐答案

您是正确的,你的问题,从jQuery UI的对话框梗...你可以通过使用一个隐藏字段记录的DropDownList价值解决这个问题。然后在你的code,引用隐藏字段。

You are correct that your issue stems from the jquery ui dialog... you can get around this by using a hidden field to record the value of the dropdownlist. Then in your code, reference the hidden field.

前端可能看起来像:

<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>

那么你的code背后:

Then your code behind:

var selected = countryVal.Value;

这篇关于另一种视图状态的DropDownList问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆