Farmer
Farmer

Reputation: 11003

Prevent the page to load elements in the page refresh ASP?

I have a page that add Items to RadioButtonList with this code :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
RD.Read()
    RBQ1.Items.Add(RD.GetString(3))
    RBQ1.Items.Add(RD.GetString(4))
    RBQ1.Items.Add(RD.GetString(5))
    RBQ1.Items.Add(RD.GetString(6))
End Sub

When I click in any button in the same page, the entire page reload and it display 8 items in the RadioButtonList, If I click for the second time I get 12 items in the RBL...

How can I prevent the page to reload if I click in this button. ?

Thanks.

Upvotes: 0

Views: 673

Answers (2)

DOK
DOK

Reputation: 32851

If you only want to populate the RadioButtonList the first time the page loads, you need to wrap that code in

If Not IsPostBack Then
   // this is the first time the page loads > populate your RadioButtonList here
Else
   // this is when the page reloads after postback
End If

Upvotes: 0

Chris Shaffer
Chris Shaffer

Reputation: 32575

You should only add the items to the control if IsPostBack is false.

If not IsPostBack then
    RD.Read()
    RBQ1.Items.Add(RD.GetString(3))
    ...
End If

As far as not having the page reload - generally when a button is clicked you do want to submit a postback to the server so you can perform whatever action the user initiates with the button.

Upvotes: 2

Related Questions