(Forum Q & A) Why is the selectedindexchanged event not allowed if autopostback = "true" of dropdownlist is set?

Source: Internet
Author: User

Someone asked

(1) autopostback = "true"

 

< ASP: dropdownlist ID = "Dropdownlist1" Runat = "Server" Autopostback = "True" > </ ASP: dropdownlist >

(2) The event is also registered.

This . Dropdownlist1.selectedindexchanged + =   New System. eventhandler ( This . Dropdownlist1_selectedindexchanged );

(3) the event is also written.

 

Private   Void Dropdownlist1_selectedindexchanged ( Object Sender, system. eventargs E)

{

Response. Write (This. Dropdownlist1.selecteditem );

}


Why can't I output the selected items? Debugging finds that the selectedindexchanged event cannot be entered.

In fact, another possibility is that you set the same value for different options in dropdownlist.

For example, write in the background:

If ( ! Ispostback)

{

For ( Int I = 0 ; I < 10 ; I ++ ) This . Dropdownlist1.items. Add ( New Listitem (I. tostring (), " Same_value " ));

}


This will not trigger the selectedindexchanged event and change it
If ( ! Ispostback)

{< br> for ( int I = 0 ; I 10 ; I ++ ) This . dropdownlist1.items. add ( New listitem (I. tostring (), I. tostring ();

}


Everything is normal. According to msdn:

Listcontrol. selectedindexchanged event

When the selected items of the list control change between the information sent to the server

This is different from the onchange event in Js.

If ( ! Ispostback)

{

For ( Int I = 0 ; I < 10 ; I ++ ) This . Dropdownlist1.items. Add ( New Listitem (I. tostring (), " Same_value " ));

This . Dropdownlist1.attributes. Add ( " Onchange " , " Alert ('test '); " );

}

Test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.