| |
Tuesday, January 18, 2011
Java Script Code to allow only one checkbox selection in GridView
To allow only one checkbox selection in GridView (like radio button selection from group).
Monday, January 17, 2011
Uploadin a file and save it in a specified location
In this article, I am going to show how we can upload a file. Here I am uploading a .txt file and saving it in a predefined location. Let see this by a simple programme. Here I am using an input tag highlighted with yellow colour for browsing the uploaded file.
The fileupload.aspx code is:
The fileupload.aspx code is:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>File Upload Demostration</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<table cellpadding="0" cellspacing="0" width="80%" align="center" border="4">
<tr><td height="20px"></td></tr>
<tr><td height="200px" align="center" valign="middle">
<input id="MyFile" type="file" size="81" name="File1" runat="server" />
<br /><br />
<asp:Button id="btnSubmit" runat="server" Text="Submit" Width="139px"Height="30px" OnClick="btnSubmit_Click"></asp:Button>
<asp:Label id="lbl" runat="server" Width="402px" Height="33px"></asp:Label>
</td></tr></table>
</form>
</body>
</html>
The fileupload.aspx.cs is:
The fileupload.aspx.cs is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (MyFile.PostedFile.ContentLength == 0)
{
lbl.Text = "Cannot upload zero length file";
return;
}
lbl.Text = MyFile.PostedFile.FileName;
MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");
}
}
When user run the application then the window will look like this:

When user run the application then the window will look like this:
Figure 1.
When user click on Browse button:

Figure 2.
After selecting the file the window will look like this:

Figure 3.
When user click on submit button.

Figure 4. The uploaded file will save in the directory specified as the destination path in .cs file.
When user click on Browse button:
Figure 2.
After selecting the file the window will look like this:
Figure 3.
When user click on submit button.
Figure 4. The uploaded file will save in the directory specified as the destination path in .cs file.
JavaScript Popup Boxes
JavaScript Popup Boxes
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
| alert("sometext"); |
Example
|
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
| confirm("sometext"); |
Example
|
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
| prompt("sometext","defaultvalue"); |
Example
|
Java script validations
Javascript Form Validation
_ ____ _ _
Put all of the code examples between these script tags.<script language="JavaScript">
E-Mail format checking script.if (document.formname.fieldname.value.length >0) {
i=document.formname.fieldname.value.indexOf("@")
j=document.formname.fieldname.value.indexOf(".",i)
k=document.formname.fieldname.value.indexOf(",")
kk=document.formname.fieldname.value.indexOf(" ")
jj=document.formname.fieldname.value.lastIndexOf(".")+1
len=document.formname.fieldname.value.length
if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
}
else {
alert("Please enter an exact email address.\n" +
document.formname.fieldname.value + " is invalid.");
return false;
}
}
|
Check if field has special characters.var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < document.formname.fieldname.value.length; i++) {
if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
return false;
}
}
|
Check if certian radio buttons have not been selected.if (!document.formname.fieldname[0].checked &&
!document.formname.fieldname[1].checked &&
!document.formname.fieldname[2].checked &&
!document.formname.fieldname[3].checked) {
alert("Please choose a group designation.\n");
return false;
}
|
Check if textbox has any characters in it.if (document.formname.fieldname.value.length == 0) {
alert("Please fill out your name.\n");
return false;
}
|
Check if multiple checkboxes have not been selected.Replace false with true to see if all are selected.if (document.formname.fieldname.checked == false &&
document.formname.fieldname.checked == false &&
document.formname.fieldname.checked == false) {
alert("Please select at least one checkbox.\n");
return false;
}
|
Check drop down has been selected. Set drop down's value to Not_Selected for this to work.if (document.formname.fieldname.value == 'Not_Selected') {
alert("Please provide us with a selection.\n");
return false;
}
|
Scan values in a field and if they are all letters then alert. The second block of code does the same but for numbers//alert on finding all letters
var noalpha = /^[a-zA-Z]*$/;
if (noalpha.test(document.formname.fieldname.value)) {
alert("Please enter at least one number in the \"username\" field.");
return false;
}
//alert on finding all numbers
var nonums = /^[0-9]*$/;
if (nonums.test(document.formname.fieldname.value)) {
alert("Please enter at least one letter in the \"username\" field.");
return false;
}
|
Remove special characters from a string.function clearText() {
document.formname.fieldname.value=filterNum(document.formname.fieldname.value)
function filterNum(str) {
re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
// remove special characters like "$" and "," etc...
return str.replace(re, "");
}
}
|
Detect special characters in text box. Or any character you subsitute for the special characters.var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < document.formname.fieldname.value.length; i++) {
if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
alert ("The box has special characters. \nThese are not allowed.\n");
return false;
}
}
|
Subscribe to:
Comments (Atom)