AddressOf Operator and IsPostback

R

RA

btnAdd_Click does not get hit; if I have IsPostBack check in Page_load. If I
don't have IsPostBack check; I am able to debug through btnAdd_Click.
If I don't look for IsPostBack then it re-populates the table before doing
anything; which I don't want.

Following is the code.
private sub page_load(.......)
'
if ispostback = false then
populatetable
end if
End sub

private sub populatetable()
' besides other data, there is a button has been added to a TableCell of a
TableRow and for that button I also have following line 'of code
btnAdd.CommandName = "TableInfo"
AddHandler btnAdd.Command, AddressOf btnAdd_Click
end sub

Public Sub btnAdd_Click(ByVal sender As Object, e As _
System.EventArgs)
' code here, when "Add" button is clicked
End Sub

would really appreciate any help on this
 
K

Karl Seguin

Your event needs to be hooked up on postback...forgetting about
populatetable for now, in reality all you need is:

dim btn as new Button()
if Page.IsPostBack then
AddHandler btn.Click, AdressOf ...
end if

what you are doing is the opposite - only hooking the event when it isn't
postback.

You should call populatetable() again - I realize that isn't what you want
to hear. You might be able to avoid it by looking at Request.Form and
seeing which button was clicked and then calling the function directly.

Karl
 
K

Kumar Reddi

Hi RA,
The problem is with the position where you adding the btnAdd_Click from.
Remember if you use "if ispostback = false then" --> this means, execute
the following code only when the page loads for the first time. So, you are
calling populatetable() only the first time the page loads. In this method,
you are adding the event handler to the "click" event. Thats good. Now the
page postbacks, this code doesnt get executed, so there is no event handler
added for the button click. You see the point here. Web applications are
stateless. All the controls or variables are repopulated each time your page
postbacks.
So, the code is not preserved during postbacks. thats why you are losing
that event handler, when the page postbacks. So, you should move the code
that adds event handler outside the populatetable() method, or call this
method on postback.

HTH
Kumar
 
R

RA

I got your point about AddressOf Operator.

But as long as populating table is concerned, shouldn't it hold the data in
the table even if it is postback?
I see it holds data for a lable.
 
K

Kumar Reddi

If you havent set the EnableViewState property to false, it does keep the
data. Be default this property is true, so ViewState stores all the data
about this control, so it gets preserved during postbacks
 
R

RA

I have not set the table's EnableViewState property to False; even though it
loses data.
Is it because I am populating table dynamically(TableRow and TableCell)?
Should I set their (TableRows' and TableCells') EnableViewState Property to
true programatically?
 
K

Kumar Reddi

ohh you are populating the TableRows and TableCells dynamically too, I didnt
know that. yeah they wouldnt be preserved either, even if you put their
EnableViewState to true. You should bind all these on every postback
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top