DropDownList的烦恼:相同的值将不会触发事件 [英] DropDownList annoyance: same value won't trigger event

查看:185
本文介绍了DropDownList的烦恼:相同的值将不会触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经填充了不同的文本属性DropDownList控件但每个文本属性具有相同的值(text属性为A,价值属性是胡说,text属性为B,价值属性是blahblah,等等)

i've populated a dropdownlist control with different text properties but each text properties had THE SAME value (text property was A, value properties is blah,text property was B, value properties is blahblah, etc... )

ASP.net只在回发检查的价值属性和因为所有值均相同(
测试的原因)这个小恼人的行为发生。有一个变通?这并不意味着你不能永远不会有相同的价值?

ASP.net only checks value properties on postback and because ALL values were the same (for testing reason) this little annoying behavior happened. Is there a work around? does this mean you can't never have the value to be the same?

推荐答案

听起来你是在错误的事件工作。尝试<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.onselectedindexchanged(VS.85).aspx\">SelectedIndexChanged.

Sounds like you are working on the wrong event. Try SelectedIndexChanged.

确保你也有的AutoPostBack 属性设置为

Ensure you also have the AutoPostBack property set to True.

OK,所以我就在这挖,因为我很好奇:)

OK, so I got digging on this since I was curious :)

有一个问题与非唯一值绑定时。

所以,首先,我对公开说,否则道歉。

So, firstly, I publicly apologise for saying otherwise.

要复制:

    <asp:DropDownList ID="myDDL" runat="server" AutoPostBack="True">
    </asp:DropDownList>
    <asp:Label ID="lblSelItem" runat="server"Text="Currently Selected Item: 0"></asp:Label>
    <asp:Label ID="lblSelVal" runat="server" Text="Currently Selected Value: X"></asp:Label>

code-背后

    List<string> MyData()
    {
        List<string> rtn = new List<string>();
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!2");
        return rtn;
    }

    protected void Page_Init()
    {
        if (!Page.IsPostBack)
        {
            // Load the Data for the DDL.
            myDDL.DataSource = MyData();
            myDDL.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Display the Currently Selected Item/Value.
        lblSelItem.Text = "Currently Selected Item: " + myDDL.SelectedIndex.ToString();
        lblSelVal.Text = "Currently Selected Value: " + myDDL.SelectedValue;
    }

运行,在DropDownList更改值。请注意,回发不会发生。

Run, changing the values in the DropDownList. Note that a PostBack does not occur.

当在源看,我意识到我们需要明确设置了为属性的&LT;选项&GT; 由服务器控制产生的元素,这使我做这样的事情:

When looking at the Source, I realised that we need to explicitly set the "value" attribute for the <option> elements generated by the server control, which lead me to do something like:

    Dictionary<string, string> MyTwoColData()
    {
        Dictionary<string, string> rtn = new Dictionary<string, string>();
        rtn.Add("1", "I am the same value!");
        rtn.Add("2", "I am the same value!");
        rtn.Add("3", "I am the same value!");
        return rtn;
    }

    protected void Page_Init()
    {
        if (!Page.IsPostBack)
        {
            // Load the Data for the DDL.
            Dictionary<string, string> data = MyTwoColData();

            foreach (KeyValuePair<string, string> pair in MyTwoColData())
            {
                myDDL.Items.Add(new ListItem(pair.Value, pair.Key));
            }

            myDDL.DataBind();
        }
    }

此explcitly设3等的值的1,2,使它们唯一的,同时仍然在列表中显示正确的数据。

This explcitly sets the values to the "1", "2", "3" etc making them unique, while still displaying the correct data within the list.

很明显,你可以改变这种与单列名单工作,但只是通过一个运行循环,并使用的价值或什么的。

Obviously, you can change this to work with single-column lists but just running through a for loop and using the value of i or something.

由于良好的解决方法与数据集,不知道。

As to good workarounds with DataSets, not sure.

实际上,我们将present选项列表与用户完全一样的价值观?

我个人认为不是,这可能是为什么这个问题还没有得到解决:)

I personally think not, which is probably why this "problem" hasn't been addressed :)

享受!

呵呵,我还要补充,如果你想在修复,使用文本值,然后将其更改为的SelectedItem ,而不是的SelectedValue

Oh, I should also add, if you want to use the text value in the "fix" then change it to SelectedItem rather than SelectedValue.

这篇关于DropDownList的烦恼:相同的值将不会触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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