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.Tabl e control. I
dynamically create rows and add the cells on the fly
then at the end, call the WebControl.Tabl e 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 CreateChildCont rols()
'lots of line skipped to show only what matters
'---VALIDATORS---
If EditMode = True Then
custValRegularH ours = New CustomValidator
custValRegularH ours.EnableView State = False
custValRegularH ours.EnableClie ntScript = False
custValRegularH ours.Display = ValidatorDispla y.Dynamic
Controls.Add(cu stValRegularHou rs)
reqLDWRegularHo ursRow = New RequiredFieldVa lidator
reqLDWRegularHo ursRow.ControlT oValidate =
"txtLDWRegularH oursRow"
reqLDWRegularHo ursRow.Display =
ValidatorDispla y.Dynamic
reqLDWRegularHo ursRow.EnableCl ientScript = False
Controls.Add(re qLDWRegularHour sRow) 'add validator to
control array of user control.
'---STYLE SHEET---
If ValidatorCSSCla ss <String.Empty Then
custValRegularH ours.CssClass = ValidatorCSSCla ss
reqLDWRegularHo ursRow.CssClass = ValidatorCSSCla ss
End If
End If
End Sub
Protected Overrides Sub RenderContents( ByVal writer As
System.Web.UI.H tmlTextWriter)
'Render all the various rows of the table
RenderLWEarning sTableBeginTag( writer)
RenderLWEarning sTitleLabelRow( writer)
RenderLblLWEarn ingsDaysLabelRo w(writer)
RenderLWEarning sDateDataRow(wr iter)
RenderLWEarning sRegHoursDataRo w(writer)
End Sub
'*** <Table>
Protected Sub RenderLWEarning sTableBeginTag( ByVal output As
System.Web.UI.H tmlTextWriter)
'<table>
output.AddAttri bute(HtmlTextWr iterAttribute.C ellspacing,
"0")
output.AddAttri bute(HtmlTextWr iterAttribute.C ellpadding,
"2")
output.AddAttri bute(HtmlTextWr iterAttribute.W idth, "400")
output.AddAttri bute(HtmlTextWr iterAttribute.B order, "0")
output.RenderBe ginTag(HtmlText WriterTag.Table )
End Sub
'*** TITLE
Protected Sub RenderLWEarning sTitleLabelRow( ByVal output As
System.Web.UI.H tmlTextWriter)
'- TABLE ROW FOR TITLE********** *************** *
'<tr>
output.RenderBe ginTag(HtmlText WriterTag.Tr)
'<td>
If EditMode = False Then
output.AddAttri bute(HtmlTextWr iterAttribute.C olspan,
"9")
Else
output.AddAttri bute(HtmlTextWr iterAttribute.C olspan,
"8")
End If
output.RenderBe ginTag(HtmlText WriterTag.Td)
output.Write("< span class=bigred>*</span> ")
lblTitle.Render Control(output)
If EditMode = True Then
output.Write("& nbsp;")
'Render the validators at the appropriate area in the table
reqLDWRegularHo ursRow.RenderCo ntrol(output)
custValRegularH ours.RenderCont rol(output)
End If
'</td>
output.RenderEn dTag()
'</tr>
output.RenderEn dTag()
'TABLE ROW FOR TITLE********** *************** *
End Sub
' ************ CURRENT CONTROL *************** ****
Protected Overrides Sub CreateChildCont rols()
'---VALIDATORS---
If EditMode Then
custValRegularH ours = New CustomValidator
custValRegularH ours.EnableView State = False
custValRegularH ours.EnableClie ntScript = False
custValRegularH ours.Display = ValidatorDispla y.Dynamic
Controls.Add(cu stValRegularHou rs)
reqLDWRegularHo ursRow = New RequiredFieldVa lidator
reqLDWRegularHo ursRow.ControlT oValidate =
"txtLDWRegularH oursRow"
reqLDWRegularHo ursRow.Display = ValidatorDispla y.Dynamic
reqLDWRegularHo ursRow.EnableCl ientScript = False
Controls.Add(re qLDWRegularHour sRow)
'---STYLE SHEET---
If ValidatorCSSCla ss <String.Empty Then
custValRegularH ours.CssClass = ValidatorCSSCla ss
reqLDWRegularHo ursRow.CssClass = ValidatorCSSCla ss
End If
End If
End Sub
Protected Overrides Sub RenderContents( ByVal writer As
System.Web.UI.H tmlTextWriter)
LWETable = New Table
RenderLWEarning sTableBeginTag( ) 'instantiate table, create
all rows
RenderLWEarning sTitleLabelRow( ) 'question
RenderLblLWEarn ingsDaysLabelRo w() 'headers
RenderLWEarning sDateDataRow() 'Dates
RenderLWEarning sRegHoursDataRo w() 'Hours
'Render the whole table in the writer
LWETable.Render Control(writer)
End Sub
Protected Sub RenderLWEarning sTableBeginTag( )
Dim i As Integer
Dim aCell As TableHeaderCell 'th
Dim aLabel As Label
LWETable.Attrib utes.Add("cellp adding", "0")
LWETable.Attrib utes.Add("cells pacing", "0")
LWETable.Attrib utes.Add("borde r", "0")
LWETable.Attrib utes.Add("width ", "495")
'Create all the rows right up front
LWETable.Rows.A dd(New TableRow) 'Title
LWETable.Rows.A dd(New TableRow) 'Header
LWETable.Rows.A dd(New TableRow) 'Sunday
LWETable.Rows.A dd(New TableRow) 'Monday
LWETable.Rows.A dd(New TableRow) 'Tuesday
LWETable.Rows.A dd(New TableRow) 'Wednesday
LWETable.Rows.A dd(New TableRow) 'Thursday
LWETable.Rows.A dd(New TableRow) 'Friday
LWETable.Rows.A dd(New TableRow) 'Saturday
LWETable.Rows.A dd(New TableRow) 'Footer
If DaysOfWeek.Coun t = 0 Then
SetDayArray()
End If
'Write the days in the first cell of the rows
For i = 0 To DaysOfWeek.Coun t - 1
'<td>
aCell = New TableHeaderCell
aCell.Horizonta lAlign = HorizontalAlign .Left
lblDays.ID = strDaysOfWeekDi splay.Item(i)
lblDays.Text = strDaysOfWeekDi splay.Item(i)
aLabel = QuickClone(lblD ays)
' 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(aC ell)
Next
If EditMode = False Then
aCell = New TableHeaderCell
aCell.Horizonta lAlign = HorizontalAlign .Center
aCell.Controls. Add(lblTotal)
LWETable.Rows(T ableRows.Footer ).Cells.Add(aCe ll)
End If
End Sub
'*** TITLE
Protected Sub RenderLWEarning sTitleLabelRow( )
Dim curRow As TableRow = LWETable.Rows(T ableRows.Title)
Dim aCell As TableCell
'- TABLE ROW FOR TITLE********** *************** *
'<td>
aCell = New TableCell
If EditMode = False Then
aCell.ColumnSpa n = 3
Else
aCell.ColumnSpa n = 3
End If
Dim aSpan As HtmlGenericCont rol = New
HtmlGenericCont rol("span")
aSpan.InnerText = "*"
aSpan.Attribute s.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(custValReg ularHours)
'aCell.Controls .Add(txtLDWRegu larHoursRow)
End If
curRow.Cells.Ad d(aCell)
'TABLE ROW FOR TITLE********** *************** *
End Sub