Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Button-click fire before or after page_load?

Reply
Thread Tools

Button-click fire before or after page_load?

 
 
Mark
Guest
Posts: n/a
 
      03-02-2004
Hi - thanks for the help in this group so far.

Regarding the click event of a link_button (created within a table and
added to a placeholder at run-time) - there are a number of link_buttons
added to the dyanmic table I am using (a gantt chart) - when you click
on one of the link_buttons, it calls a sub which takes the reference of
the table cell (a persons name, and the date - it's a resource chart) -
and updates the DB with a 'booked' flag for that person for that date.

However, if I redraw the table and link_buttons at the end of this
update routine, the changes do not show until a subsequent post_back has
been made - in effect, it looks like the updating is one click behind
me.

If I redraw the table from the page_load too (following Alessandro's
post), it works, but only if I have also called the redraw from the
update sub above (effectively doubling teh load on the app, and the
calls to the database). If I don't call the redraw from both the sub,
and the page_load - it doesn't work as soon as I click the link_button.

Have I missed something here?

Thanks,




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      03-02-2004
Mark, on that same old article you will notice that i have addressed this
issue too. The reason you are seeing this retardation behavior is because
you are recreating the controls in two different places and this can mess up
the events because after a postback round trip the event information, who
posted back and who should handle the event is passed back to the page.
Suppying an explicit ID to your dynamically created controls assures that
the approriate event handlers receive this information. Follow that post
again, i think you missed this part.

http://groups.google.com/groups?hl=e...oogle%2BSearch

"Mark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi - thanks for the help in this group so far.
>
> Regarding the click event of a link_button (created within a table and
> added to a placeholder at run-time) - there are a number of link_buttons
> added to the dyanmic table I am using (a gantt chart) - when you click
> on one of the link_buttons, it calls a sub which takes the reference of
> the table cell (a persons name, and the date - it's a resource chart) -
> and updates the DB with a 'booked' flag for that person for that date.
>
> However, if I redraw the table and link_buttons at the end of this
> update routine, the changes do not show until a subsequent post_back has
> been made - in effect, it looks like the updating is one click behind
> me.
>
> If I redraw the table from the page_load too (following Alessandro's
> post), it works, but only if I have also called the redraw from the
> update sub above (effectively doubling teh load on the app, and the
> calls to the database). If I don't call the redraw from both the sub,
> and the page_load - it doesn't work as soon as I click the link_button.
>
> Have I missed something here?
>
> Thanks,
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      03-02-2004
I think what I'm asking is how do I update the database in the
link_button click, redraw the control, without having to wait for a
subsequent post_back to 'catch up'.

Thanks again,


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      03-02-2004
Hi Alessandro - thanks again - I went back to the article, and reread it
- I had named a couple of things incorrectly, which I think I've
resolved - but I still appear to be calling the display twice - once at
postback, and once for the link_button click. The code below loops 10
times, and reads a database table - but that part isn't important - it's
the calling it twice I just can't get right. I'd appreciate if you
could look over the code if you have time, to see where I've misread
your original posting... sorry I'm taking so much of your time - but I
really do appreciate your guidance.

In aspx:

<asplaceHolder id="PlaceHolder1" runat="server"
EnableViewState="False"></asplaceHolder>

In codebehind:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Response.Write("<br>page_load at " & DateTime.Now)
If IsPostBack Then
If Not viewstate("controls") Is Nothing Then
Response.Write("<br>calling from page_load at " &
DateTime.Now)
doDateArray()
'End If
End If
End If
Response.Write("<br>page_end at " & DateTime.Now)
End Sub



Sub doDateArray()
Dim sDate As DateTime
Dim x As Integer
Dim myDateArr As ArrayList
sDate = "2004-04-02"
myDateArr = New ArrayList
For x = 0 To 31
myDateArr.Add(sDate.AddDays(x).ToShortDateString.T oString)
Next
x = myDateArr.Count
Dim mystring As String
Dim mt_ConvDate As String
Dim bookingexists As Integer
Dim Table1 As New Table
Dim i As Integer
Dim mtr As New TableRow
Dim mtcresourcename2 As New TableCell
mtcresourcename2.Controls.Add(New LiteralControl("resource"))
mtr.Cells.Add(mtcresourcename2)
For i = 0 To x - 1
Dim mtc As New TableCell
Select Case CDate(myDateArr(i)).DayOfWeek
Case DayOfWeek.Saturday, DayOfWeek.Sunday
mtc.Controls.Add(New
LiteralControl(CDate(myDateArr(i)).Day))
Case Else
mtc.Controls.Add(New
LiteralControl(CDate(myDateArr(i)).Day))
End Select
mtc.Width = Unit.Pixel(22)
mtr.Cells.Add(mtc)
mtc.ID = "mtc" & i.ToString
Next
Table1.Rows.Add(mtr)
mtr.ID = "mtrr"
Dim resource_id As Integer
Dim resource_name As String
Dim a As Integer
Dim j As Integer
j = 0
For a = 0 To 10
resource_id = a.ToString
resource_name = a.ToString & "- name"
Dim mtr2 As New TableRow
j = j + 1
Dim mtcresourcename As New TableCell
mtcresourcename.Controls.Add(New LiteralControl("<div
style='background-color:Blue;color:white;'>" & resource_name &
"</div>"))
mtr2.Cells.Add(mtcresourcename)
For i = 0 To x - 1
Dim mtc2 As New TableCell
'mt_ConvDate = mt_date(myDateArr(i))
Dim exists As Integer
exists = 0
exists = checkexist("mtc2i" & i.ToString & "_" &
j.ToString)
Response.Write("<br>checking:" & "mtc2i" & i.ToString &
"_" & j.ToString)
If exists > 1 Then
mtc2.Controls.Add(New LiteralControl("<div
style='background-color:limegreen;color:white;'>&nbsp;</div>"))
Else
Dim objBtn As New LinkButton
objBtn.ID = "btnID" & resource_id.ToString & "_" &
i.ToString
objBtn.EnableViewState = False
objBtn.CausesValidation = False
objBtn.Text = "-"
AddHandler objBtn.Click, AddressOf btnClicked
mtc2.Controls.Add(objBtn)
mtc2.BackColor = System.Drawing.Color.White
mtc2.HorizontalAlign = HorizontalAlign.Center
mtc2.VerticalAlign = VerticalAlign.Middle
End If
mtr2.Cells.Add(mtc2)
mtc2.ID = "mtc2i" & i.ToString & "_" & j.ToString
Next
Table1.Rows.Add(mtr2)
Next
PlaceHolder1.Controls.Add(Table1)
Table1.ID = "mycontrol1"
viewstate("controls") = 1

End Sub

Sub btnClicked(ByVal obj As Object, ByVal e As EventArgs)
PlaceHolder1.Controls.Clear()
Response.Write("<br>button clicked at " & DateTime.Now)
doDateArray()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
doDateArray()
End Sub

Function checkexist(ByVal text As String) As Integer
Dim strConn As String =
ConfigurationSettings.AppSettings("conString")
Dim myoledb As String = "SELECT id, text FROM table1 WHERE
text=@text"
Dim myConn As New OleDbConnection(strConn)
Dim objDR As OleDbDataReader
Dim cmd As New OleDbCommand(myoledb, myConn)
cmd.Parameters.Add(New OleDbParameter("@text", "" & text))
myConn.Open()
objDR = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If objDR.Read Then
checkexist = 1
End If
myConn.Close()
End Function

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      03-02-2004
Mark, can you resolve by having an additional check in your buttonclick.
Page load is always fired before your button click event handler and
executes your code, adding your dynamic controls. And then this is
re-executed in the click event handler of the button for the second time.
You are seeing doubled data because of this. Also if you will notice, when
clicking one of your dynamically created link buttons you will see that you
dont receive double data because you are clearing the placeholders controls
collection here. This is fine if you want to re-execute the code and are
loading different data. On the other hand in your static button click event
you are not clearing the placeholder and I guess you dont want to re-execute
here. On the older post i had made this worked fine because that user was
loading different controls as well after postback. So clearing the
placeholders controls collection and recreating the controls at this point
was not very inefficient and required this step. This does not seem
to be your case. So why not check in your button if the viewstate flag is
set and if so dont let it re-run the code as this has already been executed
on page_load.


Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
If viewstate("controls") Is Nothing Then
doDateArray()
End If
End Sub

"Mark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I think what I'm asking is how do I update the database in the
> link_button click, redraw the control, without having to wait for a
> subsequent post_back to 'catch up'.
>
> Thanks again,
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      03-02-2004
Hi Alessandro - thanks again - that does stop it executing twice, but
the code needs to execute the buttonclick, and then redraw - I plan to
have the buttonclick event add an entry to the database, which will
'block off' that resources time for that day (the cell clicked). So by
not redrawing at the buttonclick stage, my controls display will always
appear to be one step behind (as it won't be redrawn with the updated
database checks until the next postback).

Is this just something I have to live with, or have my code execute the
redraw twice? (I'm getting there with your help - but slowly!)









*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      03-02-2004
I'm unable to follow.
There is no retardation of events. I have tested and the code in your
button1 click fires correctly. The code in your btnClick event fires
correctly as soon as a link button is clicked and your doDateArray sub
routine is re-executed.

The only undesired effect was the part where you had your data doubled when
your doDateArray sub routine got re-executed again your button1 click. You
can resolve this by clearing your placeholder here, or checking against the
viewstate flag. Thats about it mark. It has to be working. I have executed
your code in debug mode and watched it run line by line ;P

If you still feel that i have missed your point try and describe exactly
what parts are not working for you. Try and replicate in a very simple
sample showcasing the part that is not working.

For any of your events to fire, that is events of your dynamic
controls --like your linkbuttons, the code in your doDateArray sub routine
has to be re-executed and all those controls including the linkbuttons have
to get recreated which is what your doDateArray sub routine is doing, only
then does it maintainstate and finally rem "oh, linkbutton_bla got click,
lets go and wire it up to the appropriate handler and handle the event" . .
..etc




"Mark" <(E-Mail Removed)> wrote in message
news:e$(E-Mail Removed)...
> Hi Alessandro - thanks again - that does stop it executing twice, but
> the code needs to execute the buttonclick, and then redraw - I plan to
> have the buttonclick event add an entry to the database, which will
> 'block off' that resources time for that day (the cell clicked). So by
> not redrawing at the buttonclick stage, my controls display will always
> appear to be one step behind (as it won't be redrawn with the updated
> database checks until the next postback).
>
> Is this just something I have to live with, or have my code execute the
> redraw twice? (I'm getting there with your help - but slowly!)
>
>
>
>
>
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      03-06-2004
Wish i could help Mark. Your on your own there. Not sure if anybody else can
help either. I can only assure you that its very awkward to have the event
you want fired being one click behind always. Your going to have to debug
this and find the bug.


"Mark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Alessandro - thank you once again. I too managed to get the step
> through working in VS - and can as you say get the doDateArray to fire
> only the once. However, and this is where I was probably not too clear,
> the sequence of events needs to be:
>
> 1. When the user first visits the page, the table is built, and the user
> sees what resources/people are available on which days.
>
> 2. The days they are unavailable are blocked off (ie. they are just
> coloured - the others have linkbutton controls)
>
> 3. If the user wants to book a day of a resources time, they click on
> the cell (column is the day, the row is the resource/person)
>
> 4. By clicking on the linkbutton in the appropriate cell, a sub should
> be fired, which inserts a record into a database table, which shows that
> resources ID and the date of the relative cell they have been booked for
>
> 5. The table should then be rebuilt, reflecting the new booking just
> made - which will result in the cell just being clicked, being drawn as
> just a coloured cell
>
> It is step 5 which only catches up on the next postback - or the current
> postback, if I allow doDataArray to be called twice.
>
> Am I making any sense? It is so clear in my head! Sorry!
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fire with fire Little Green Eyed Dragon Digital Photography 0 08-16-2006 12:15 AM
On fighting fire with fire... skip@pobox.com Python 16 08-01-2005 07:09 PM



Advertisments