Unwanted Dropdown list reinitialalization

E

Ed

I have in a Web page a drop down list. It gets initialized in
Page_Load:

ddlTop.Items.Clear()

ddlTop.Items.Add("All")

ddlTop.Items.Add("Hard")

ddlTop.Items.Add("Soft")



In the same page I have a button with. In the code section I have :

Private Sub GetTable_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGetTable.Click

.....

Response.Write("Top = " & ddlTop.SelectedItem.ToString)

.....

End sub

Yet when I open the page in the browser and select "Hard" from the
dropdown list then click the button the Response is always "All".

The problem is the dropdown list is getting reinitializied when the
button is clicked.



What am I missing here?



TIA



Ed
 
M

Martin Honnen

Ed said:
I have in a Web page a drop down list. It gets initialized in
Page_Load:

ddlTop.Items.Clear()

ddlTop.Items.Add("All")

ddlTop.Items.Add("Hard")

ddlTop.Items.Add("Soft")



In the same page I have a button with. In the code section I have :

Private Sub GetTable_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGetTable.Click

....

Response.Write("Top = " & ddlTop.SelectedItem.ToString)

....

End sub

Yet when I open the page in the browser and select "Hard" from the
dropdown list then click the button the Response is always "All".

The problem is the dropdown list is getting reinitializied when the
button is clicked.



What am I missing here?

Use
If Not(IsPostBack) Then
ddlTop.Items.Clear()

ddlTop.Items.Add("All")

ddlTop.Items.Add("Hard")

ddlTop.Items.Add("Soft")
End If
in your Page_Load to ensure you only initialize the drop drown list once
on the first load but not if a post back occurs.
 
E

Ed Sowell

It works! Thanks, Martin.

Ed

Martin Honnen said:
Use
If Not(IsPostBack) Then
ddlTop.Items.Clear()

ddlTop.Items.Add("All")

ddlTop.Items.Add("Hard")

ddlTop.Items.Add("Soft")
End If
in your Page_Load to ensure you only initialize the drop drown list once
on the first load but not if a post back occurs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top