Microsoft Ajax Control Toolkit Combo Box SelectedIndex, SelectedValue 的問題以及解決方法 (小秘技)

不明白為有人何會把 Ajax Control Toolkit  和jquery 比較…….

跟本control toolkit 就有好大的不同而且 dom manipulation 好難會好過jquery….(咁當然一齊最簡單…)

最多chowky只會當ajax control toolkit 作為User Control library 用就算….

ASP.NET 無combobox control…都唔想再加新LIBRARY,因為有很多control 用不到…..

Scenario:
當combox 的dropdown list is not empty 就可以改到selectedIndex and selectedValue….如果係無的話就唔比改…..

你只要用多過一個combox 就會有呢個問題…

e.g. Combox1 and Combox2 : 用家free hand 改左combox1 (not exist in the list of values)…PostBack changes…..Combox2 之前selected 的value 會freeze 左…..

你改唔到selectedIndex,  selectedValue (assign 左value 不變), clear Combox2 的item list 會throw error….(無奈)

online 無人講無人提…..乜事?…..個control 有野?

http://stackoverflow.com/questions/880986/asp-net-ajaxs-combobox-cannot-programatically-change-the-selected-item

唯一方法是自己睇code=.= ….睇完真係呆左…

Combobox.cs

676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            if (!Enabled)
                return false;
 
            int newSelectedIndex = Convert.ToInt32(postCollection.GetValues(HiddenFieldControl.UniqueID)[0], CultureInfo.InvariantCulture);
            EnsureDataBound();
 
            if (newSelectedIndex == -2 && (DropDownStyle == ComboBoxStyle.Simple || DropDownStyle == ComboBoxStyle.DropDown))
            {
                string newText = postCollection.GetValues(TextBoxControl.UniqueID)[0];
                ComboBoxItemInsertEventArgs eventArgs = new ComboBoxItemInsertEventArgs(newText, ItemInsertLocation);
                this.OnItemInserting(eventArgs);
                if (!eventArgs.Cancel)
                {
                    this.InsertItem(eventArgs);
                }
                else
                {
                    TextBoxControl.Text = (SelectedIndex < 0) ? string.Empty : SelectedItem.Text;
                }
            }
 
            else if (newSelectedIndex != SelectedIndex)
            {
                SelectedIndex = newSelectedIndex;
                return true;
            }
 
            return false;
        }

int newSelectedIndex = Convert.ToInt32(postCollection.GetValues(HiddenFieldControl.UniqueID)[0], CultureInfo.InvariantCulture);
呢句……..我真係想問下有幾new ….HiddenFieldControl 估計會係save 住postback之前個個value….咁都係new?

then change:

676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            if (!Enabled)
                return false;
 
            int newSelectedIndex = Convert.ToInt32(postCollection.GetValues(HiddenFieldControl.UniqueID)[0], CultureInfo.InvariantCulture);
            EnsureDataBound();
 
            if (newSelectedIndex == -2 && (DropDownStyle == ComboBoxStyle.Simple || DropDownStyle == ComboBoxStyle.DropDown))
            {
                string newText = postCollection.GetValues(TextBoxControl.UniqueID)[0];
                ComboBoxItemInsertEventArgs eventArgs = new ComboBoxItemInsertEventArgs(newText, ItemInsertLocation);
                this.OnItemInserting(eventArgs);
                if (!eventArgs.Cancel)
                {
                    this.InsertItem(eventArgs);
                }
                else
                {
                    TextBoxControl.Text = (SelectedIndex < 0) ? string.Empty : SelectedItem.Text;
                }
            }
 
            else if (newSelectedIndex != SelectedIndex)
            {
                if (SelectedIndex != -1) //added: expect -1 is special case.
                {
                    SelectedIndex = newSelectedIndex;
                }
                else
                {
                    //clear textbox...
                    TextBoxControl.Text = String.Empty;
                }
                return true;
            }
 
            return false;
        }

Use at your own risk.
This piece of code handles combox.SelectedIndex == -1 is an exception value and clear the Textbox value.
I have found this function will be called when the value changed is triggered by textbox of the combobox. The dropdownlist of the combobox will run another program path to process. It should be the different flow of IPostBackDataHandler and ListControl. Interesting…

Share
About Chowky
Chowky 希望可以透過這個blog 來分享自己學到的知識。亦希望可以分享最新的資訊。 IT 轉變得很快,要常常留意市場的變化,面對大陸的競爭,香港的 IT 方面的人才要更有市場觸覺,緊貼市塲走勢。因為其他地方的IT技術已經走得比以往更快。不進則退,就讓大家一起來交流、提昇IT的心得! Chowky 因為工作關係,所以一開始接觸的是visual basic, foxpro ( 這不代表我老 ) 之後是 .NET, MSSQL 和 Crystal Report. 由工作學到的project management, system analysis and design 以及 system testing, user acceptance tests and different documentations. 另外,由不同的freelance 方面學到dreamweaver, javascript, php, mysql. 後來興起CMS wave, Chowky 亦學會了Joomla! 以及常用的javascript framwork: jquery and mootool. 連deisgn 都要自己包辦,所以接觸了photoshop, illustrator, firework. 新的工作方面,要利用RIA -- Flex and AIR 方面的技術。所以緊接下來應該會多一點分享ria 方面的技術 Chowky 所說的未必是對的,但我深信我所說的是未來 IT Trend. 大家亦應抱著懷疑的態度去分析每一件事,未經思考不要輕易相信。

No Comments, Be The First!

Leave a Reply