HighTechTalks DotNet Forums  

Problem with postback not displaying my table again

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Problem with postback not displaying my table again in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Mark
 
Posts: n/a

Default Problem with postback not displaying my table again - 03-01-2004 , 03:28 AM






Hi - I posted this in the ASP.Net forum, but have not had any help
unfortunately:

I am adding a 'gantt chart' style table, to manage peoples availability
- if they are unavailable, the table cell is just coloured - if they are
available (and therefore bookable), I add a link button control to the
table cell.

Trouble is, when I click on the link button, and it posts back to the
server, even although I call the sub to show populate the table from
page_load - it doesn't redraw the table.

I'd really appreciate any advice on where I'm going wrong - my code is
below:


<asp:table id="Table1" runat="server" BorderColor="Black"
BorderStyle="None" BorderWidth="1px" ForeColor="#F7F7F7"
BackColor="#4A3C8C" CellPadding="0" CellSpacing="1"
EnableViewState="False"></asp:table>


..and I add my control like this (first line deals with the persons name
and the dates, the rest is to either block off the table cell, or
provide the 'dynamic' control to be used in postback - however, when
someone clicks on the dynamically created link, even although I call
doDateArray from within Page_load, it does not show on the postback -
however, if I subsequently call the function again, it will appear - I
think it is to do with: AddHandler objBtn.Click, AddressOf btnClicked -
but if anyone could advise, I'd appreciate it.

Code which I think is being called by the dynamic control, and the
dynamic control are shown below.



Sub btnClicked(ByVal obj As Object, ByVal e As EventArgs)

doDateArray()

End Sub






Sub doDateArray()

Dim sDate As DateTime

Dim x As Integer

Dim myDateArr As ArrayList

sDate = calOverview.SelectedDate

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 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)

Next

Table1.Rows.Add(mtr)



Dim strConn As String =
ConfigurationSettings.AppSettings("conString")

Dim mysql As String = "SELECT resource_id, resource_name FROM
tblresource WHERE hotel_id=@hotel_id ORDER BY resource_name"

Dim myConn As New SqlConnection(strConn)

Dim objDR As SqlDataReader

Dim cmd As New SqlCommand(mysql, myConn)

cmd.Parameters.Add(New SqlParameter("@hotel_id", "" &
Session("Hotel_id")))

myConn.Open()

objDR = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Dim resource_id As Integer

Dim resource_name As String

While objDR.Read()

resource_id = objDR("resource_id")

resource_name = objDR("resource_name")

Dim mtr2 As New TableRow

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))

bookingexists = checkBookingExist2(mt_ConvDate,
resource_id)

If bookingexists > 0 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)

Next

Table1.Rows.Add(mtr2)

End While

myConn.Close()



End Sub



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
  #2  
Old   
Alessandro Zifiglio
 
Posts: n/a

Default Re: Problem with postback not displaying my table again - 03-01-2004 , 03:41 AM






hi mark --this is because you are creating the tablerows and adding them to
your table only on button click. After a postback this code is not
re-executed. You are breaking one of the most basic rules for dynamically
created controls in that the code for creating dynamic controls needs to run
and therefore rebuild, after each postback. If you still want to add tables
on your button click, then work around by setting a flag in viewstate. Here
is a work around i posted some time back. Use that same logic in your
control. Not very complicated.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=uwUUS155DHA.2576%40TK2MSFTNGP11.phx.gbl& rnum=1&prev=/groups%3Fq%3DDynamically%2Bloading%2Bascx%2Bpage%2 Band%2Bhaving%2Bevents%2Bfire%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den%26btnG%3DGoogle%2BSearch

"Mark" <anonymous (AT) devdex (DOT) com> wrote

Quote:
Hi - I posted this in the ASP.Net forum, but have not had any help
unfortunately:

I am adding a 'gantt chart' style table, to manage peoples availability
- if they are unavailable, the table cell is just coloured - if they are
available (and therefore bookable), I add a link button control to the
table cell.

Trouble is, when I click on the link button, and it posts back to the
server, even although I call the sub to show populate the table from
page_load - it doesn't redraw the table.

I'd really appreciate any advice on where I'm going wrong - my code is
below:


asp:table id="Table1" runat="server" BorderColor="Black"
BorderStyle="None" BorderWidth="1px" ForeColor="#F7F7F7"
BackColor="#4A3C8C" CellPadding="0" CellSpacing="1"
EnableViewState="False"></asp:table


.and I add my control like this (first line deals with the persons name
and the dates, the rest is to either block off the table cell, or
provide the 'dynamic' control to be used in postback - however, when
someone clicks on the dynamically created link, even although I call
doDateArray from within Page_load, it does not show on the postback -
however, if I subsequently call the function again, it will appear - I
think it is to do with: AddHandler objBtn.Click, AddressOf btnClicked -
but if anyone could advise, I'd appreciate it.

Code which I think is being called by the dynamic control, and the
dynamic control are shown below.



Sub btnClicked(ByVal obj As Object, ByVal e As EventArgs)

doDateArray()

End Sub






Sub doDateArray()

Dim sDate As DateTime

Dim x As Integer

Dim myDateArr As ArrayList

sDate = calOverview.SelectedDate

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 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)

Next

Table1.Rows.Add(mtr)



Dim strConn As String =
ConfigurationSettings.AppSettings("conString")

Dim mysql As String = "SELECT resource_id, resource_name FROM
tblresource WHERE hotel_id=@hotel_id ORDER BY resource_name"

Dim myConn As New SqlConnection(strConn)

Dim objDR As SqlDataReader

Dim cmd As New SqlCommand(mysql, myConn)

cmd.Parameters.Add(New SqlParameter("@hotel_id", "" &
Session("Hotel_id")))

myConn.Open()

objDR = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Dim resource_id As Integer

Dim resource_name As String

While objDR.Read()

resource_id = objDR("resource_id")

resource_name = objDR("resource_name")

Dim mtr2 As New TableRow

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))

bookingexists = checkBookingExist2(mt_ConvDate,
resource_id)

If bookingexists > 0 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)

Next

Table1.Rows.Add(mtr2)

End While

myConn.Close()



End Sub



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply With Quote
  #3  
Old   
Mark
 
Posts: n/a

Default Re: Problem with postback not displaying my table again - 03-01-2004 , 04:58 AM



Hi Alessandro - thank you for the information.

I do call the sub from Page_load too - but not using the method you have
in your other posting. I'll give that a go - thanks again,


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
  #4  
Old   
Alessandro Zifiglio
 
Posts: n/a

Default Re: Problem with postback not displaying my table again - 03-01-2004 , 05:16 AM



Your welcome mark

"Mark" <anonymous (AT) devdex (DOT) com> wrote

Quote:
Hi Alessandro - thank you for the information.

I do call the sub from Page_load too - but not using the method you have
in your other posting. I'll give that a go - thanks again,


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.