locked
Dynamically created textbox and associated required field validator - client-side error shows but Page.IsValid = True RRS feed

  • Question

  • User-1996987520 posted

    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.  

    From my page_init event I'm calling a sub that checks session variables, makes a database call to load a recordset to bind to an unrelated control, attaches some unrelated javascript to other controls then it calls a separate sub that actually 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.

    Thanks!

    <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>&nbsp;&nbsp;"))
    cell .Controls.Add(oDPOffenseDate)

    Dim rfvOffenseDate As RequiredFieldValidator = New RequiredFieldValidator()
    With rfvOffenseDate
         .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
         .Attributes.Add("runat", "server")
    End With
    cell.Controls.Add(rfvOffenseDate)

    tRow.Cells.Add(cell) 'Offense Date label and Offense Date data
    tStatuteHTMLTable.Rows.Add(tRow)

    Wednesday, February 26, 2020 3:39 AM

All replies

  • User503812343 posted

    create dynamic controls in page load event in both scenario Page.IsPostbakc = True or Page.Ispostback = false.

    Wednesday, February 26, 2020 3:16 PM
  • User-1996987520 posted

    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

    Wednesday, February 26, 2020 3:52 PM
  • User475983607 posted

    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.  

    Wednesday, February 26, 2020 4:12 PM
  • User-1996987520 posted

    This is inherited code that I'm maintaining but, yes, I should probably bite the bullet and convert these to a DataList or something.

    Wednesday, February 26, 2020 6:26 PM
  • User-1330468790 posted

    Hi, gbrown,

    I have tried you code and it worked well from my side (.NET Framework 4.7, Chrome).

     

    About Page.IsValid:

    I checked the "Page_IsValid" value from Javascript side and it changed after the validation worked (validation worked normally). 

    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.

    About ViewState:

    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.

    Best regards,

    Sean

    Thursday, February 27, 2020 10:01 AM