03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

jQuery <strong>and</strong> Validation x 415<br />

2. View the HTML for the page <strong>in</strong> the browser by right-click<strong>in</strong>g the page <strong>and</strong> choos<strong>in</strong>g View Source<br />

or View Page Source.<br />

3. Locate the <strong>in</strong>put field for the user’s name <strong>and</strong> then look at the code for the validator that is placed<br />

directly below it. You should see someth<strong>in</strong>g like this:<br />

*<br />

4. Scroll down to the end of the code until you see the large chunk of JavaScript code that is responsible<br />

for the validation. For the Name field, you’ll see someth<strong>in</strong>g like this:<br />

var cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1 = document.all ?<br />

document.all["cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1"] :<br />

document.getElementById(<br />

"cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1");<br />

cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1.controltovalidate<br />

= "cpMa<strong>in</strong>Content_ContactForm_Name";<br />

cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1.errormessage<br />

= "Enter your name";<br />

cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1.evaluationfunction<br />

= "RequiredFieldValidatorEvaluateIsValid";<br />

cpMa<strong>in</strong>Content_ContactForm_RequiredFieldValidator1.<strong>in</strong>itialvalue = "";<br />

For each of the validation controls you see similar code, all add<strong>in</strong>g to the size of the page.<br />

5. Go back to VS, right-click the website <strong>in</strong> the Solution Explorer, <strong>and</strong> choose Add Í Add New Item.<br />

Locate the Global Application Class item. You don’t have to enter a name because the default of<br />

Global.asax is the only allowed name for this file.<br />

6. Add the follow<strong>in</strong>g code to the Application_Start method that is already part of the code template<br />

of the Global.asax file:<br />

<strong>VB</strong><br />

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)<br />

ScriptManager.ScriptResourceMapp<strong>in</strong>g.AddDef<strong>in</strong>ition("jquery",<br />

New ScriptResourceDef<strong>in</strong>ition With<br />

{ .Path = "~/Scripts/jquery-1.7.2.m<strong>in</strong>.js" })<br />

End Sub<br />

C#<br />

void Application_Start(object sender, EventArgs e)<br />

{<br />

ScriptManager.ScriptResourceMapp<strong>in</strong>g.AddDef<strong>in</strong>ition("jquery",<br />

new ScriptResourceDef<strong>in</strong>ition<br />

{<br />

Path = "~/Scripts/jquery-1.7.2.m<strong>in</strong>.js"<br />

}<br />

);<br />

}<br />

The spell<strong>in</strong>g of the word jquery (all lowercase) is case sensitive, so make sure you type it exactly<br />

as shown here. If you have a newer version of the jQuery library, don’t forget to update the version<br />

number.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!