c# - find if dropdown value was added dynamically -


Is a value in a dropdown list dynamically added or is there an initial value?

For example.

  & asp: dropdown list id = "MyDDL" runat = "server" & gt; & Lt; ASP: ListItem & gt; 1 & lt; / ASP: ListItem & gt; & Lt; ASP: ListItem & gt; 2 & lt; / ASP: ListItem & gt; & Lt; ASP: ListItem & gt; 3 & lt; / ASP: ListItem & gt; & Lt; / ASP: DropDownList & gt; // 1 Private Zero AddExtraItemToList (on postback) {If (some conditions) / / Additional items can be inserted MyDDL.Items.Add ("17") or not; } // Private Zero RemoveExtraItemsFromList () {// Remove any non-default values ​​from the // list on postback ...? }  

It is trivial to codify a list of the basic values ​​behind the code behind it, but if you can assume above which values ​​are the initial properties and the viewstate Were born from restoration

The best way to handle this is to keep track of the original values.

< P> Here is a customized version of your code, which stores the original view in the viewstation (you can store them anywhere). The trick is to pull out items in the dropdown before you do any dynamic inserts for new items.

[original] dogdownwalls {return seestate ["genesis"] as string []; } Set {ViewState.Add ("origavalues", value); }} Secure Zero Page_load (Object Sender, EventAgMS E) {if you get value from {// Control !: // ((! Page.IsPostBack)) and put them in the ViewView Trust = new list & lt; String & gt; (); Forex Currency (List Items in MyDDL.Items) {oValues.Add (item.Value); } OriginalDropDownValues ​​= oValues.ToArray (); }} // Private Zero RemoveExtraItemsFromList (on the postback) {list & lt; String & gt; ValuesToKill = new list & lt; String & gt; (); Var oItems = OriginalDropDownValues; Foreach (ListItem currentItem in MyDDL.Items) {if (! OItems.Contains (currentItem.Value)) {// Items can not be deleted yet, modify the collection We are running again through the values. Add (currentItem.Value); }} Foreach (var killVal valuesToKill) // kill the loop values ​​and remove each from the Find + drop down {MyDDL.Items.Remove (MyDDL.Items.FindByValue (killVal)); }}

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -