Last post Feb 27, 2020 10:01 AM by Sean Fang
Feb 26, 2020 03:39 AM|gbrown|LINK
I'm creating textboxes whose TextMode=Date at runtime along with their associated required field validators. When the textbox isn't filled out the client-side error message displays to the user but when they click the submit button the Page.IsValid is set
to True. On this same page I also have textboxes defined in my .aspx page that, when not filled out, both show the client-side error and set the Page.IsValid to False.
creates the controls and validators. I'm setting the ValidationGroup properly and the button has CausesValidation=True.
Why does the client-side validation fire but yet still set Page.IsValid to True? Is this too late in the page lifecycle? The dynamic controls also don't maintain viewstate even though I set that. Is my parent <asp:table> not a valid container for validators?
In the below code, i is my loop counter.
<asp:table id="tStatuteHTMLTable" runat="server"></asp:table>
<asp:Button ID="bAddCase" runat="server" Text="Add Case" Enabled="true" CausesValidation="true" ValidationGroup="grpAddCaseOrReturnToBookingList"/>
Dim tRow As New TableRow()
Dim cell As New TableCell
Dim oDPOffenseDate As New TextBox
oDPOffenseDate.TextMode = TextBoxMode.Date
oDPOffenseDate.CausesValidation = True
oDPOffenseDate.EnableViewState = True
oDPOffenseDate.ValidationGroup = "grpAddCaseOrReturnToBookingList"
oDPOffenseDate.ID = "OffenseDateSeq" & i.ToString()
cell .Controls.Add(New LiteralControl("<label>Offense Date:</label> "))
Dim rfvOffenseDate As RequiredFieldValidator = New RequiredFieldValidator()
.ID = "rfvOffenseDate" + i.ToString()
.ControlToValidate = oDPOffenseDate.ID
.SetFocusOnError = True
.EnableViewState = True
.ErrorMessage = "* required"
.EnableViewState = True
.ForeColor = Drawing.Color.Red
.Visible = True
.ValidationGroup = "grpAddCaseOrReturnToBookingList"
.Enabled = True
tRow.Cells.Add(cell) 'Offense Date label and Offense Date data
Feb 26, 2020 03:16 PM|l.laxmikant|LINK
create dynamic controls in page load event in both scenario Page.IsPostbakc = True or Page.Ispostback = false.
Feb 26, 2020 03:52 PM|gbrown|LINK
Thanks for responding. I tried that but it didn't work for me. I think I'm running into this issue from 7 years ago which I'm still trying to digest and I assume is still the case today,
Dynamic controls: associated events don't fire if recreated during Page_Load
Feb 26, 2020 04:12 PM|mgebhard|LINK
IMO, you should use standard Web Forms tools and programming patterns like using bound server controls rather than building control dynamically. When you build control dynamically, it is up to you to design and manage state on every request. If you use
data bound controls then you get all the baked in goodness ASP.NET Web Forms framework.
Dynamic controls is better suited for the MVC or ASP.NET Core Razor pages.
Feb 26, 2020 06:26 PM|gbrown|LINK
This is inherited code that I'm maintaining but, yes, I should probably bite the bullet and convert these to a DataList or something.
Feb 27, 2020 10:01 AM|Sean Fang|LINK
I have tried you code and it worked well from my side (.NET Framework 4.7, Chrome).
Page.IsValid should be accessed from server side. Could you please tell me
when you checked the value of it? It would be useful to reproduce the problem.
I debugged in the VS and found the value for the first validator.
See below result:
So, I think the settings that you have added like "EnableViewState" has took effect.
If you could provide the information of the environment (version of framework, browser), it might be helpful to reproduce the problem.
Surely, it would be a better choice to use standard web forms tools since the framework would take the reponsibility for designing and managing state/event, which will reduce your work and make the process of debugging much easier.