firing selectindexchanged

c19h28O2

Member
Joined
Jul 24, 2006
Messages
6
Programming Experience
1-3
Guys,

can you tell me how to fire a selectindexchanged event from code.

I have a sub procedure that changes the selectedindex value of dropdown1, i've been told that changing this value won't fire the selectedindexchanged event(which it doesn't!). it will fire if i click the ddl (which makes sense) but not when the procedure is called that changes its indexvalue

any ideas?

Cheers
 
1st let me str8 are you talking about dropDownList Web control or ComboBox control (the forum where you've posted doesn't suit best your question if you talk about dropDownList).
However, if it's web control and you still want to fire selectedIndexChanged event of certain control you should set its AutoPostBack property to True (in case i have understod your question).
Otherwise please explain it again.

Regards ;)
 
Apologies if this is the wrong forum...

The autopostback is set to true as when i click the first dropdown list the second is populated so that works fine...

What i would like to know is how to fire the selectindexchanged event from code.

A procedure runs dropdownlist1.selectedindex = 3 so i was expecting it to fire the dropdownlist1_selectedindexchanged event but it doesn't any idea how to fire this event WITHOUT clicking the control?
 
I know exactly what do you mean and the problem is that the DropDownList controls do not have view state ... so, you would have to hook addhandler inside onLoad but notice that i would resolve it in another way.
Creating procedure with all the stuff from selecetedIndexChanged within and call it no matter from dropDownList or any other control event like button_click for example.
Anyway, Please, explain what is your idea and i'll try to find more appropriate solution for you (i am certainly sure there is)

Regards ;)

P.S. assuming you want to perform selectingindex programatically from button you can simply add a handler in load:
VB.NET:
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] Button1.Click, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] DropDownList1_SelectedIndexChanged[/SIZE]
[COLOR=green]'view state will be associated with the control button1[/COLOR]
then within Button1_Click Sub to add this:
VB.NET:
dropdownlist1.selectedindex = 3
And ti will work but as i said it is not the happiest solution :)
 
Hi Kulrom,

Thanks for getting back to me, i've basically done what you have said and called the selectindexchanged and it works, so for now i'm happy with that, although not the most elegant solution....

Thanks for your help
 
The whole reason of a postback system is to trigger server-side code. When you in server-side code set dropdownlist1.SelectedIndex you are already in server-side code and consequently don't need any additional trigger to get where you already are. So you have code that is done in SelectedIndexChanged method, move that code to it's own sub method. now you call this sub method both from SelectedIndexChanged and where you set SelectedIndex. Example:
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
If Page.IsPostBack = False Then
  DropDownList1.Items.Add("ax")
  DropDownList1.Items.Add("bx")
  DropDownList1.Items.Add("cx")
  DropDownList1.SelectedIndex = 1 [COLOR=darkgreen]'setting SelectedIndex server-side[/COLOR]
  myDDL_SelectedIndexChanged() [COLOR=darkgreen]'<---see?[/COLOR]
End If
End Sub

 
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
  myDDL_SelectedIndexChanged()[COLOR=darkgreen] '<---see?[/COLOR]
End Sub
 
Sub myDDL_SelectedIndexChanged()
  Me.Title = DropDownList1.SelectedIndex
End Sub
 
Incidentally, you could also use the existing event handler method directly, (now that you know the link between ASP.Net events and postbacks:)) do this by calling the method and provide the parameters; sender is the control that invoked the event and in this case there are no additional arguments in e event data, so just provide a new empty instance of EventArgs class. Example:
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
If Page.IsPostBack = False Then
  DropDownList1.Items.Add("ax")
  DropDownList1.Items.Add("bx")
  DropDownList1.Items.Add("cx")
  DropDownList1.SelectedIndex = 1 [COLOR=darkgreen]'setting SelectedIndex server-side[/COLOR]
  DropDownList1_SelectedIndexChanged(DropDownList1, New EventArgs) [COLOR=darkgreen]'call the event handler method server-side[/COLOR]
End If
End Sub
 
 
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
  Me.Title = DropDownList1.SelectedIndex
End Sub
 
Back
Top