Problem with postback not displaying my table again

M

Mark

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

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
 
A

Alessandro Zifiglio

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=...re&ie=UTF-8&oe=UTF-8&hl=en&btnG=Google+Search
 
M

Mark

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,
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top