Sunteți pe pagina 1din 8

asp.net provides a new paradigm for developing web-based applications.

this
includes a set of server-based controls that are analogous to html form elements
such as text boxes, buttons, and so forth. the problem with these controls is the
need to call the server. javascript provides an alternative for many tasks that
negates the need to go to the server. let's take a closer look at combining the
power of javascript with asp.net.

performance is a must
making calls to the server requires bandwidth and server processing time. this may
not be a problem with an intranet application, which has the advantage of a high-
speed lan, but an internet application is a different story. an internet user's
connection speed varies depending on whether the user has a dial-up modem, dsl, or
a cable modem. utilising client-side javascript negates the need for a server
round-trip.

the traditional approach


a standard web form includes numerous standard areas such as the head, body, and
form. javascript functions are traditionally placed in the head portion of the web
form. this allows the functions to be loaded and be available to the rest of the
page. once loaded, the functions may be called from html elements.

let's take a look at a simple javascript example:

function valsubmit() {
var doc = document.forms[0];
var msg = "";
if (doc.firstname.value == "") {

msg += "- please enter a first name.n";


}

if (doc.lastname.value == "") {
msg += "- please enter a last name.n";
}

if (msg == "") {
doc.submit();
} else {
alert("the following errors were
encountered.nn" + msg);
} }

this function verifies that values are entered into two html fields on the form.
if either field is empty, an error message displays and execution stops. if both
fields are populated, the form is submitted. you may call this function from an
html button with the following code:

<input type="button" value="submit"


name="btnsubmit" onclick="valsubmit();">

with this relationship, the form isn't submitted to the server until the fields
are populated. the code may be simple, but it doesn't have any adverse effect on
performance since no extra server calls are necessary; and the javascript code is
short and sweet, which means the form doesn't require additional load time. this
solution utilises normal html form elements, but using it with an asp.net web form
isn't as straightforward.

combining javascript with asp.net


asp.net web forms allow the use of standard html, so you can easily use the
previous example--but it negates the power asp.net offers. asp.net user controls
allow you to call server code easily to process the web form. fortunately, it's
possible to combine the power of the user controls and javascript. i'll
demonstrate this with an asp.net button control.

the button control's attributes property provides a way to tie javascript to the
control. first, the javascript function is placed on the asp.net web form, but
it's altered in a particular way: a return value is added. the function returns
true if the validation is successful; this signals that the form may be submitted
to the server. the submission to the server signals that the server function
associated with the button is called. a return value of false signals the function
failed, so the form isn't submitted.

<%@ page language="c#" %>


<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html><head>
<title>webform1</title>
<script language="javascript">
function valsubmit() {
var doc = document.forms[0];
var msg = "";
if (doc.firstname.value == "") {
msg += "- please enter a first name.n";
}
if (doc.lastname.value == "") {
msg += "- please enter a last name.n";
}
if (msg == "") {
doc.submit();
return true;
} else {
alert("the following errors were encountered.nn" + msg);
return false;
} }
</script>
<script language="c#" runat="server">
private void btnsearch_click(object sender, system.eventargs e) {
response.write("search");
}
private void page_load(object sender, system.eventargs e) {
btnsubmit.attributes.add("onclick", "return valsubmit();");
}
</script></head>
<body>
<form id="frmbuildertest" method="post" runat="server">
<label style="z-index: 101; left: 10px; position: absolute; top: 48px">
first name:</label>
<input style="z-index: 102; left: 88px; position: absolute; top: 48px"
type="text" name="firstname" id="firstname">
<label style="z-index: 103; left: 10px; position: absolute; top: 88px">
last name:</label>
<input style="z-index: 104; left: 88px; position: absolute; top: 88px"
type="text" name="lastname" id="lastname"><br /><br />
<asp:button id="btnsubmit" style="z-index: 105; left: 64px; position:
absolute;
top: 128px" runat="server" text="submit" width="136px"
onclick="btnsearch_click"></asp:button>
</form></body></html>

this is the key line in the code:

btnsubmit.attributes.add("onclick", "return valsubmit();");

the elements are placed on the html form using css via their style attribute. the
form ties the javascript function to the asp.net button (btnsubmit) and to its
html onclick event. the onclick attribute of the asp.net button tells the system
what function is called when the form is submitted to the server.

if you're a vb.net developer, the only change to the previous code is the c# block
of code. the vb.net version follows:

<script language="vb" runat="server">


private btnsearch_click (sender as object, e as system.eventargs)
response.write("search")
end sub
private page_load(sender as object, e as system.eventargs)
btnsubmit.attributes.add("onclick", "return valsubmit();")
end sub
</script>

a powerful combination
javascript is the de facto standard for client-side web development. combining it
with asp.net web forms provides the developer with a powerful set of tools for
building robust applications that consider performance a key ingredient.

tony patton began his professional career as an application developer earning


java, vb, lotus, and xml certifications to bolster his knowledge.

* print this
* e-mail this
* leave a comment
* clip this
*

close

please login or sign up to clip this article.


e-mail
password
remember melogin
log in | sign up | why join builder au's community? see the benefits.

please login or sign up to clip this article.


e-mail
password
remember me login

tags: asp.net | form | forms | javascript | web

do you need help with .net? gain advice from builder au forums

* related links

* preloading and the javascript image() object


* builder's javascript quickstart tool
* hierarchical menus with javascript
* locate errors in your javascript code

* comments

1
oscar spila - 07/10/04

why the code:

private void btnsearch_click(object sender, system.eventargs e) {


response.write("search");

in opera and netscape not work? but en ie is work?


� report offensive content

2
tejas shah - 12/02/05

hello evreyone
i also have the same problem
but i also want to get back the return value of function.
then how can i get that valuewith vb.net in asp.net

thanks in advance
� report offensive content

3
bradley thomas - 18/04/05

this article shows what i find everywhere on the web, how to link from your
asp.net page to your html/javascript page, but not the other way around. i would
love to know how to get details that have been changed through javascript back to
my asp.net page so that they may be stored in the database. how do i do this
� report offensive content

4
ravi - 28/04/05

how to access c# label controls in javascript. all other controls i can access
only lables i cant. please help.
� report offensive content

5
steven oesterreicher - 04/10/05

isn't using java script frowded upon because you don't know whether a client has
java script turned on?
� report offensive content

6
rajib ahmed - 10/05/06

using javasript is not frowned upon. client side validation is necessary, but
doesn't imply server side validation is not. it only makes applications stronger
to have validation on both sides. if you are using client/server side validation,
make sure in you vb.net or c# code checks if the page is valid. (page.isvalid)
� report offensive content
7
sagar - 21/07/06

hello everyone,
i want to add a javascript function on my project's one .aspx file to validate a
textbox so that the charector '&' should not be entered in it
� report offensive content

8
newasp.netter - 11/08/06

what to do if "onclick" event already defines something else?


� report offensive content

9
smruti ranjan rout - 14/08/06

hello everyone
if i want call a java script function in onclick of a button attribut,
if i am writing btnsubmit.attributes.add("onclick","return
checkrequiredfield();");

regular expression validator dosn't work

what i should do???


� report offensive content

10
bill donnelly - 06/09/06

in my situation, i have some logic which occurs as the page (c#/asp.net) gets
loaded (it determines whether the record the user is trying to edit is locked by
another user).
i want to give the user the opportunity to take some action; the only way to get a
yes/no answer (afaik) is to use a javascript confirm() dialog.

the code is already in the process of handling an action (onload) so i can't fire
the javascript from a button click or anything. i need to fire the javascript and
get the return value from the function, so that the following c# code can choose
action a or b based on the result.

how can i do this? it's frustrating that i can call the javascript code to present
the dialog (by doing response.write() with the script) but it doesn't seem to be
possible to get the result value.

code here shows the basic outline of my loadpage() function with the unrelated
junk stripped out.

public void loadpage(object sender, system.eventargs e) {

if ( ! ispostback ) {

if ( recordislocked() ) {

lockfields();

string lockedmessage = "send email to lock owner?";


response.write("<script>return confirm('"+

lockedmessage +"');</script>");

// is there any way to make the script store the

// return value somewhere that can be checked

// from the c# world?

} else {

lockrecord();

� report offensive content

11
dev bhagat - 12/09/06

thanks for this wonderful article.


this script is working very fine in dotnet 1.1

now, i just shifted to 2.0 version, this is no more working there.

please tell me what to do, if i have to code it in a master page as there is no


form tag available there.
<asp:content id="content1" contentplaceholderid="contentplaceholder1"
runat="server">

</asp:content>
� report offensive content

12
engin dikici - 03/11/06

perfect article; solved my problems.

thank you ver much.


� report offensive content

13
asim - 17/11/06

hi

i want ot call javascript function on onselectedindexchanged event of asp.net.


what can i do?

thanks
regards
� report offensive content
14
vinod - 15/02/07

hi,
i have html server control with required field validator and it was working fine
when i click on save button.

now i have added the onclientclick="return confrim('are your sure');" with save
button, and conform dialog box is working fine but the required fields validator
with the html controls are by passed and not working.

thanks
vinod
� report offensive content

15
charles - 28/02/07

how do i combine javascript and asp.net when using master page.


i tried your function on a regular asp.net form and it worked perfectly. however,
it does work out on a content page when using asp.net 2.0 master page. i need your
help to fix this problem. thanks
� report offensive content

16
krishna agrawal - 02/04/07

how can i pop-up a window in button click in asp.net?


� report offensive content

17
david - 18/05/07

i want to call an javascript function whenever i am deleting an row in datagrid. i


have an imagebutton in itemtemplate and i am caling javascript function which
returns true of false. whenever i am clicking th imagebutton i am getting the
javascript function but after that i need to get the return value from the
javascript functiont to datagrid rowcommand event. please help me regarding this.
� report offensive content

18
kuldeep - 14/06/07

this is very short and unsufficient example ...

no thanks

� report offensive content

19
deepak - 13/08/07

give me code for opening a form in diffrent style


� report offensive content

20
deepak - 13/08/07
give me code for opening a form in diffrent style
� report offensive content

21
deepak - 13/08/07

give me code for opening a form in diffrent style


� report offensive content

22
meenu - 05/09/07

i have to call a html page using the javascript in the specified portion of the
page without using frames.
� report offensive content

23
sumiya - 13/12/07

i wish to know if a javascript code can be called from an asp.net webcontrol(not


htmlcontrol). e.g. i wish to populate a drop down list (webcontrol) using
javascript, how do i go about it? i know that htmlcontrols can be used, but my
application isn't suited to that. please explain.

S-ar putea să vă placă și