Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

asp.net - DropDownList annoyance: same value won't trigger event

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Ensure you also have the AutoPostBack property set to True.

Resolved

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

There is a "problem" when databinding with non-unique values.

So, firstly, I publicly apologise for saying otherwise.

To replicate:

ASPX

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

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

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

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:

New Code-Behind

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

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.

Realistically, would we present a list of options with the exact same values to the user?

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

Enjoy!

PS:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...