Dynamic user control and validation

E

Eniac

Hi,

I've been working on a custom user control that needs to be modified
and the validation is causing me headaches.

The control used to generate a table of 4 rows x 7 columns to display
all the days in the week with dates and textboxes to fill in some
data.

row 1: question
row 2: days of the week
row 3: dates for the days of the week
row 4: textboxes to enter data for each day of the week

The control was using 4 procedures to generate its rows, each
procedure generates a row using loops for each days of the week. It
was using a series of RenderBeginTag, add content, RenderEndTag and
RenderControl calls

A required field validator is instantiated created on the onload event
of the user control, then added to the control array

At some point during the table generation, the validator is rendered
in a cell.

That works fine.

But now, I had to modify the layout to display 4 columns by 7 rows. I
can no longer treat the data "per row".

So i created all the rows upfront using a WebControl.Table control. I
dynamically create rows and add the cells on the fly
then at the end, call the WebControl.Table RenderControl method.

The problem I have with that is that the validator wont work. I know
it has to be added to the control array of the user control but using
my technique i can no longer tell it to render in X or Y cell of the
table. (or am i wrong ?)

I think i know what i have to do but i have no idea how to do it. I'm
pretty new to controls like that (without a .asmx) I've already
searched a lot on google without finding anything that could help me
understand.

Could someone help me ?

Here's some Before and After code to make it clearer ...

' ************ PREVIOUS CONTROL *******************
Protected Overrides Sub CreateChildControls()
'lots of line skipped to show only what matters
'---VALIDATORS---
If EditMode = True Then
custValRegularHours = New CustomValidator
custValRegularHours.EnableViewState = False
custValRegularHours.EnableClientScript = False
custValRegularHours.Display = ValidatorDisplay.Dynamic
Controls.Add(custValRegularHours)

reqLDWRegularHoursRow = New RequiredFieldValidator
reqLDWRegularHoursRow.ControlToValidate =
"txtLDWRegularHoursRow"
reqLDWRegularHoursRow.Display =
ValidatorDisplay.Dynamic
reqLDWRegularHoursRow.EnableClientScript = False
Controls.Add(reqLDWRegularHoursRow) 'add validator to
control array of user control.

'---STYLE SHEET---
If ValidatorCSSClass <> String.Empty Then
custValRegularHours.CssClass = ValidatorCSSClass
reqLDWRegularHoursRow.CssClass = ValidatorCSSClass
End If
End If

End Sub

Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)
'Render all the various rows of the table

RenderLWEarningsTableBeginTag(writer)
RenderLWEarningsTitleLabelRow(writer)
RenderLblLWEarningsDaysLabelRow(writer)
RenderLWEarningsDateDataRow(writer)
RenderLWEarningsRegHoursDataRow(writer)
End Sub

'*** <Table>
Protected Sub RenderLWEarningsTableBeginTag(ByVal output As
System.Web.UI.HtmlTextWriter)
'<table>
output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,
"0")
output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
"2")
output.AddAttribute(HtmlTextWriterAttribute.Width, "400")
output.AddAttribute(HtmlTextWriterAttribute.Border, "0")
output.RenderBeginTag(HtmlTextWriterTag.Table)
End Sub

'*** TITLE
Protected Sub RenderLWEarningsTitleLabelRow(ByVal output As
System.Web.UI.HtmlTextWriter)
'- TABLE ROW FOR TITLE**************************
'<tr>
output.RenderBeginTag(HtmlTextWriterTag.Tr)
'<td>
If EditMode = False Then
output.AddAttribute(HtmlTextWriterAttribute.Colspan,
"9")
Else
output.AddAttribute(HtmlTextWriterAttribute.Colspan,
"8")
End If
output.RenderBeginTag(HtmlTextWriterTag.Td)
output.Write("<span class=bigred>*</span>&nbsp;")
lblTitle.RenderControl(output)
If EditMode = True Then
output.Write("&nbsp;")

'Render the validators at the appropriate area in the table

reqLDWRegularHoursRow.RenderControl(output)
custValRegularHours.RenderControl(output)

End If
'</td>
output.RenderEndTag()
'</tr>
output.RenderEndTag()
'TABLE ROW FOR TITLE**************************
End Sub

' ************ CURRENT CONTROL *******************

Protected Overrides Sub CreateChildControls()
'---VALIDATORS---
If EditMode Then
custValRegularHours = New CustomValidator
custValRegularHours.EnableViewState = False
custValRegularHours.EnableClientScript = False
custValRegularHours.Display = ValidatorDisplay.Dynamic
Controls.Add(custValRegularHours)

reqLDWRegularHoursRow = New RequiredFieldValidator
reqLDWRegularHoursRow.ControlToValidate =
"txtLDWRegularHoursRow"
reqLDWRegularHoursRow.Display = ValidatorDisplay.Dynamic
reqLDWRegularHoursRow.EnableClientScript = False
Controls.Add(reqLDWRegularHoursRow)

'---STYLE SHEET---
If ValidatorCSSClass <> String.Empty Then
custValRegularHours.CssClass = ValidatorCSSClass
reqLDWRegularHoursRow.CssClass = ValidatorCSSClass
End If
End If
End Sub

Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)

LWETable = New Table

RenderLWEarningsTableBeginTag() 'instantiate table, create
all rows
RenderLWEarningsTitleLabelRow() 'question
RenderLblLWEarningsDaysLabelRow() 'headers
RenderLWEarningsDateDataRow() 'Dates
RenderLWEarningsRegHoursDataRow() 'Hours

'Render the whole table in the writer
LWETable.RenderControl(writer)

End Sub

Protected Sub RenderLWEarningsTableBeginTag()
Dim i As Integer
Dim aCell As TableHeaderCell 'th
Dim aLabel As Label

LWETable.Attributes.Add("cellpadding", "0")
LWETable.Attributes.Add("cellspacing", "0")
LWETable.Attributes.Add("border", "0")
LWETable.Attributes.Add("width", "495")

'Create all the rows right up front
LWETable.Rows.Add(New TableRow) 'Title
LWETable.Rows.Add(New TableRow) 'Header
LWETable.Rows.Add(New TableRow) 'Sunday
LWETable.Rows.Add(New TableRow) 'Monday
LWETable.Rows.Add(New TableRow) 'Tuesday
LWETable.Rows.Add(New TableRow) 'Wednesday
LWETable.Rows.Add(New TableRow) 'Thursday
LWETable.Rows.Add(New TableRow) 'Friday
LWETable.Rows.Add(New TableRow) 'Saturday
LWETable.Rows.Add(New TableRow) 'Footer

If DaysOfWeek.Count = 0 Then
SetDayArray()
End If

'Write the days in the first cell of the rows
For i = 0 To DaysOfWeek.Count - 1
'<td>
aCell = New TableHeaderCell
aCell.HorizontalAlign = HorizontalAlign.Left

lblDays.ID = strDaysOfWeekDisplay.Item(i)
lblDays.Text = strDaysOfWeekDisplay.Item(i)

aLabel = QuickClone(lblDays)
' aLabel.ID &= "_" & i

aCell.Controls.Add(aLabel)

'2 is the number of rows before the rows for the days
LWETable.Rows(2 + i).Cells.Add(aCell)
Next

If EditMode = False Then
aCell = New TableHeaderCell
aCell.HorizontalAlign = HorizontalAlign.Center
aCell.Controls.Add(lblTotal)

LWETable.Rows(TableRows.Footer).Cells.Add(aCell)
End If
End Sub

'*** TITLE
Protected Sub RenderLWEarningsTitleLabelRow()
Dim curRow As TableRow = LWETable.Rows(TableRows.Title)
Dim aCell As TableCell
'- TABLE ROW FOR TITLE**************************
'<td>
aCell = New TableCell
If EditMode = False Then
aCell.ColumnSpan = 3
Else
aCell.ColumnSpan = 3
End If
Dim aSpan As HtmlGenericControl = New
HtmlGenericControl("span")
aSpan.InnerText = "*"
aSpan.Attributes.Add("class", "mandatory")
aCell.Controls.Add(aSpan)
aCell.Controls.Add(lblTitle)

If EditMode = True Then
'i know i cannot add the validator to the control array
of the table because it has to be form level to trigger
' but how can i make it render in this cell ?
'aCell.Controls.Add(custValRegularHours)
'aCell.Controls.Add(txtLDWRegularHoursRow)
End If

curRow.Cells.Add(aCell)
'TABLE ROW FOR TITLE**************************
End Sub
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top