简体   繁体   中英

How do I force a VB.NET script to run before the page itself in ASP.NET WebForms?

I have a script which is included in my page:

<script runat="server" language="VB" src="../AdditionalField/AdditionalFieldSave.aspx"></script>

edit: wrong script, I meant this one:

<!-- #include file="../AdditionalField/AdditionalFieldDisplayHTML.aspx" -->

yes, you can #include in WebForms, apparently!

Inside this script I am adding some values to a list:

If myAdditionalFieldTempReader("Mandatory") Then
    MandatoryAdditionalFields.Add("Notes" & additionalFieldFoundCount)
End If

And then back in the page I am retrieving the values from the list:

jQuery("#YouthEdit").validate({
    rules: {
        BirthDate: {
            required: true,
            date: true
        },
        FirstName: {
            required: true,
        },
        LastName: {
            required: true,
        }
        <%
        For Each mf As String In MandatoryAdditionalFields
            Response.Write("," & mf & ": {required: true}")
        Next
        %>
    },
    messages: {
        BirthDate: "Enter a birth date",
        FirstName: "Enter a First Name",
        LastName: "Enter a Last Name"
    },
    submitHandler: function(form) { 
        //form.submit();
        document.YouthEdit.submit();
    }
    });

My problem is that when I retrieve the values from the list, the list is still empty; it hasn't been populated yet. How can I force the script to run before the page itself so the values are present when I need them?

edit: OK, I tried this:

<input id="mandatoryAdditionalFieldsHidden" type="hidden" value="" runat="server" />

on the include page and this:

mandatoryAdditionalFieldsHidden.Value = String.Join(",", MandatoryAdditionalFields)

in a function called from Page_Init on the main page and

var mandatoryAdditionalFields = jQuery("#mandatoryAdditionalFieldsHidden").val().split(',');
for (field in mandatoryAdditionalFields)
    rules.push([field, 'required: true']);

in the main page JavaScript. And now I get an error saying I can't call split on undefined ... why would the hidden field have an undefined value when I set it in the Page_Init?

You should write your jquery code in this way, so it's executed only once the page has been loaded:

$( document ).ready(function() {
  // Put your code here.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM