Unwanted Dropdown list reinitialalization

  • Thread starter Thread starter Ed
  • Start date Start date
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
 
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.
 
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.
 
Back
Top