Professional Documents
Culture Documents
Js 1
Js 1
Example : The following web form captures user First Name, Last Name & Email.
why do you use JavaScript
When we click the Submit button, we want to save the data to the following SQL
Server Users table
why use JavaScript
Step 2 : Create a stored procedure to insert user record into Users table
Create procedure spInsertUser
@FirstName nvarchar(50),
@LastName nvarchar(50),
@Email nvarchar(50)
as
Begin
Insert into Users values (@FirstName, @LastName, @Email)
End
Step 3 : Create a new empty asp.net web application project. Name it Demo.
Step 5 : Add a WebForm to the project. Copy and paste the following HTML in the
<form> tag of the webform.
<table style="border:1px solid black; font-family:Arial">
<tr>
<td>
<b>First Name</b>
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:Label ID="lblFirstName" runat="server"
ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Last Name</b>
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:Label ID="lblLastName" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblEmail" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
Step 6 : Copy and paste the following code in the code-behind file.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
cmd.Parameters.Add(paramFirstName);
cmd.Parameters.Add(paramLastName);
cmd.Parameters.Add(paramEmail);
con.Open();
cmd.ExecuteNonQuery();
}
}
if (string.IsNullOrEmpty(txtFirstName.Text))
{
ret = false;
lblFirstName.Text = "First Name is required";
}
else
{
lblFirstName.Text = "";
}
if (string.IsNullOrEmpty(txtLastName.Text))
{
ret = false;
lblLastName.Text = "Last Name is required";
}
else
{
lblLastName.Text = "";
}
if (string.IsNullOrEmpty(txtEmail.Text))
{
ret = false;
lblEmail.Text = "Email required";
}
else
{
lblEmail.Text = "";
}
return ret;
}
}
}
Run the application. Leave all the fields blank and click the Submit button. The
form is posted to the web server, the server validates the form and since all the
fields are required fields the user is presented with the following error messages.
advantages of JavaScript
In this case the form validation is done on the server. Just to validate the form,
there is a round trip between the client browser and the web server. The request
has to be sent over the network to the web server for processing. This means if the
network is slow or if the server is busy processing other requests, the end user
may have to wait a few seconds.
Copy and paste the following JavaScript function in the head section of
WebForm1.aspx
if (document.getElementById("txtLastName").value == "")
{
document.getElementById("lblLastName").innerText = "Last Name is required";
ret = false;
}
else
{
document.getElementById("lblLastName").innerText = "";
}
if (document.getElementById("txtEmail").value == "")
{
document.getElementById("lblEmail").innerText = "Email is required";
ret = false;
}
else
{
document.getElementById("lblEmail").innerText = "";
}
return ret;
}
</script>
For the Submit button set OnClientClick="return ValidateForm()". This calls the
JavaScript ValidateForm() function. The JavaScript code runs on the client browser
and validates the form fields.
If the fields are left blank the user is presented with an error message right
away. This is much faster because there is no round trip between the client and the
web server. This also means the load on the server is reduced, and we are using the
client machine processing power. If all the form fields are populated the form will
be submitted to the server and the server saves the data to the database table.
3. With JavaScript partial page updates are possible i.e only portions of the page
can be updated, without reloading the entire web form. This is commonly called as
AJAX.
4. JavaScript can also be used to animate elements on a page. For example, show or
hide elements and sections of the page.
JavaScript tutorial
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
15 comments:
Reply
Replies
Reply
Reply
Thanks
Raghavendra
Reply
You are helping thousands of people trying to lean JS other stuff. This great act
of your are helping many fresher who can not pay for the coaching class and still
want to learn and work in this competitive environment, Hats off to you, you will
get paid by god for this act of kindness.
Keep it up.
Reply
Your video lessons are helpful to me in great extent, in such a way that creating
self-confidence.
K.Baburao,Hyderabad.
Reply
Reply
UnknownNovem