Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first Listitem selection not firing event

Status
Not open for further replies.

vishalmarya

Programmer
Aug 19, 2001
28
I am dynamically adding a dropdownlist to a webpage and binding its SelectedIndexChanged event to a sub.

few listitems are added to the control. the code in written in page_load . the first item comes
preselected. changing the listitem fires the associated event. but when the first item is selected , the event is not fired.


now if the same code is written in Page_Init , everything works fine.


i know that the viewstate is loaded and set after the Page_Int and before the Page_Load, but its doing for all items except the first one.

what's exactly happening here ?

( i am using asp.net 2.0 )

My code :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Odl.ID = "odl1"
Odl.AutoPostBack = True
Page.Form.Controls.Add(Odl)
AddHandler Odl.SelectedIndexChanged, AddressOf EventSub


Odl.Items.Clear()

Odl.Items.Add("a")
Odl.Items.Add("b")
Odl.Items.Add("c")
End Sub

Protected Sub EventSub(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Write(CType(sender, DropDownList).SelectedItem.Text)


End Sub
 
First, always create dynamically created controls in the Page_Init event.
Second, The first item is always going to be selected unless you specify otherwise, so the event won't fire until you change to "b" for example, then back to "a
 

thanks for the reply jbenson001 .

i have a problem creating this control in the Page_Init event .

actually i am creating this control inside a cell of gridview .

program flow is like This :

1. gridview gets populated in form_load

2. onclick of a button dropdownlist needs to be added in the cell .

so if i write this dropdownlist addition code inside page_init ( postback ) , it will give me a error because the cell is not there . why : because gridview hasn't yet got its data from viewstate. if i do it in page_load its ok.

so what is the way out ?
 
Create a template column in the grid, then drag and drop a dropdownlist in the appropriate template.
 

can't do it.

no of columns are only known at runtime.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top