Aspx page Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="OnRowcommand.Registration" %>
<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" TagPrefix="Ajax" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function validation() {
var errorsummary = "";
errorsummary += checkname();
errorsummary += checksalary();
errorsummary += checkgmail();
errorsummary += checkpassword();
errorsummary += checkcpassword();
if (errorsummary != "") {
alert(errorsummary);
return false;
}
else
{
return true;
}
}
function checkname()
{
var TBN = document.getElementById('Textname');
var pp = /^[a-zA-Z ]+$/
if(TBN.value=="")
{
return 'Plese enter your name !!\n';
}
else if (pp.test(TBN.value)) {
return "";
}
else {
return 'Name should be onley alphabets !!\n';
}
}
function checksalary() {
var TBS = document.getElementById('Textsalary');
var ss = /^-?\d*(\.\d+)?$/
if (TBS.value == "") {
return 'Plese enter your salary!!\n';
}
else if (ss.test(TBS.value)) {
if (TBS.value > 10000 && TBS.value < 50000) {
return "";
}
else {
return 'Salary should be 10000 between 50000 !!\n';
}
}
else {
return 'Plese enter onley integer values !!\n';
}
}
function checkgmail() {
var TBG = document.getElementById('Textgmail');
var TG = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (TBG.value == "") {
return 'Plese enter your Gmail!!\n';
}
else if (TG.test(TBG.value)) {
return "";
}
else {
return 'Plese enter alphabets and special symbole!!\n';
}
}
function checkpassword() {
var TBP = document.getElementById('Textpassword');
var TP = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,15}$/
if (TBP.value == "") {
return 'Plese enter your password!!\n';
}
else if (TP.test(TBP.value)) {
return "";
}
else {
return 'password should be Minimum 8 characters at least 1 Alphabet and 1 Number !!\n';
}
}
function checkcpassword() {
var TBC = document.getElementById('Textcpassword');
var TBP = document.getElementById('Textpassword');
if (TBC.value == "") {
return 'Plese enter your cpassword!!\n';
}
else if (TBP.value == TBC.value) {
return "";
}
else {
return 'Plese do not match your password !!\n';
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<Ajax:ToolkitScriptManager ID="calander" runat="server"></Ajax:ToolkitScriptManager>
<div>
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="Textname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gender:</td>
<td><asp:RadioButtonList ID="rblgender" runat="server" RepeatColumns="3">
<asp:ListItem Value="1" Text="Male"></asp:ListItem>
<asp:ListItem Value="2" Text="Female"></asp:ListItem>
<asp:ListItem Value="3" Text="Other"></asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:DropDownList ID="ddlcountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>State:</td>
<td><asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlstate_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>Salary:</td>
<td><asp:TextBox ID="Textsalary" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gmail:</td>
<td><asp:TextBox ID="Textgmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="Textpassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>CPassword:</td>
<td><asp:TextBox ID="Textcpassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>DOB:</td>
<td><asp:TextBox ID="Textdob" runat="server"></asp:TextBox></td>
<td><Ajax:CalendarExtender ID="calandera" runat="server" PopupButtonID="Textdob" PopupPosition="BottomRight" TargetControlID="Textdob"></Ajax:CalendarExtender></td>
</tr>
<tr>
<td>Hobbies:</td>
<td><asp:CheckBoxList ID="cblhobbies" runat="server" RepeatColumns="3">
<asp:ListItem Value="1" Text="Football"></asp:ListItem>
<asp:ListItem Value="2" Text="Cricket"></asp:ListItem>
<asp:ListItem Value="3" Text="Music"></asp:ListItem>
<asp:ListItem Value="4" Text="Dancing"></asp:ListItem>
<asp:ListItem Value="5" Text="Swimming"></asp:ListItem>
<asp:ListItem Value="6" Text="Yoga"></asp:ListItem>
</asp:CheckBoxList></td>
</tr>
<tr>
<td>File_Upload</td>
<td><asp:FileUpload ID="Textfn" runat="server" /></td>
</tr>
<tr>
<td>Active:</td>
<td><asp:CheckBox ID="cbchecked" runat="server" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnsave" runat="server" Text="Save" OnClientClick="return validation()" OnClick="btnsave_Click" /></td>
</tr>
</table>
<table border="1" style="background-color:pink">
<tr>
<td></td>
<td><asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Emp_name">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<%#Eval("Gender").ToString()=="1" ? "Male" : Eval("Gender").ToString()=="2" ? "Female" : "Other"%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<%#Eval("Cname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<%#Eval("Sname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gmail">
<ItemTemplate>
<%#Eval("Gmail") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gmail">
<ItemTemplate>
<%#Eval("Salary") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<ItemTemplate>
<%#Eval("DOB") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hobbies">
<ItemTemplate>
<%#Eval("Hobbies") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File_Upload">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("File_Upload","~/File_Uploads/{0}") %>' Width="70px" Height="60px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Yes/No">
<ItemTemplate>
<%#Eval("isactive").ToString()=="1" ? "True": "False" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkbtndelete" runat="server" OnClientClick="return confirm('Do you want to delete !!')" Text="Delete" CommandName="DEL" CommandArgument='<%#Eval("Empid") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" runat="server" Text="Edit" CommandName="EDT" CommandArgument='<%#Eval("Empid") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</div>
</form>
</body>
</html>
.CS page Code C#:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace OnRowcommand
{
public partial class Registration : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_country();
ddlstate.Items.Insert(0, new ListItem("--seletc--", "0"));
Fill_grd();
}
}
public void clear()
{
Textname.Text = "";
rblgender.SelectedValue = "0";
ddlcountry.SelectedValue = "0";
ddlstate.SelectedValue = "0";
Textsalary.Text = "";
Textgmail.Text = "";
Textpassword.Text = "";
Textcpassword.Text = "";
Textdob.Text = "";
cblhobbies.ClearSelection();
cbchecked.Checked = false;
}
public void Fill_country()
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
ddlcountry.DataValueField = "Cid";
ddlcountry.DataTextField = "Cname";
ddlcountry.DataSource = ds;
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--seletc--", "0"));
}
}
public void Fill_state(string Cid)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_state_get", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Cid", Cid);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
ddlstate.DataValueField = "Sid";
ddlstate.DataTextField = "Sname";
ddlstate.DataSource = ds;
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("--seletc--", "0"));
}
}
public void Fill_grd()
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
grd.DataSource = ds;
grd.DataBind();
}
else
{
grd.DataSource = null;
grd.DataBind();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
Fill_state(ddlcountry.SelectedValue);
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
cbchecked.Checked = true;
}
protected void btnsave_Click(object sender, EventArgs e)
{
string HOB = "";
for (int i = 0; i < cblhobbies.Items.Count; i++)
{
if (cblhobbies.Items[i].Selected == true)
{
HOB += cblhobbies.Items[i].Text + ',';
}
}
HOB = HOB.TrimEnd();
string FN = "";
FN = Path.GetFileName(Textfn.PostedFile.FileName);
Textfn.SaveAs(Server.MapPath("File_Uploads" + "\\" + FN));
if (btnsave.Text == "Save")
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_Employee_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", Textname.Text);
cmd.Parameters.AddWithValue("@Gender", rblgender.SelectedValue);
cmd.Parameters.AddWithValue("@Country", ddlcountry.SelectedValue);
cmd.Parameters.AddWithValue("@State", ddlstate.SelectedValue);
cmd.Parameters.AddWithValue("@Salary", Textsalary.Text);
cmd.Parameters.AddWithValue("@Gmail", Textgmail.Text);
cmd.Parameters.AddWithValue("@Password", Textpassword.Text);
cmd.Parameters.AddWithValue("@Cpassword", Textcpassword.Text);
cmd.Parameters.AddWithValue("@DOB", Textdob.Text);
cmd.Parameters.AddWithValue("@Hobbies", HOB);
cmd.Parameters.AddWithValue("@File_Upload", FN);
cmd.Parameters.AddWithValue("@isactive", cbchecked.Checked == true ? 1 : 0);
cmd.ExecuteNonQuery();
con.Close();
Fill_grd();
clear();
}
else
{
FN = Path.GetFileName(Textfn.PostedFile.FileName);
con.Open();
SqlCommand cmd = new SqlCommand("usp_Employee_insert_upd", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", ViewState["Eid"]);
cmd.Parameters.AddWithValue("@Name", Textname.Text);
cmd.Parameters.AddWithValue("@Gender", rblgender.SelectedValue);
cmd.Parameters.AddWithValue("@Country", ddlcountry.SelectedValue);
cmd.Parameters.AddWithValue("@State", ddlstate.SelectedValue);
cmd.Parameters.AddWithValue("@Salary", Textsalary.Text);
cmd.Parameters.AddWithValue("@Gmail", Textgmail.Text);
cmd.Parameters.AddWithValue("@Password", Textpassword.Text);
cmd.Parameters.AddWithValue("@Cpassword", Textcpassword.Text);
cmd.Parameters.AddWithValue("@DOB", Textdob.Text);
cmd.Parameters.AddWithValue("@Hobbies", HOB);
if (FN != "")
{
cmd.Parameters.AddWithValue("@File_Upload", FN);
File.Delete(Server.MapPath("File_Uploads" + "\\" + ViewState["fn"]));
Textfn.SaveAs(Server.MapPath("File_Uploads" + "\\" + FN));
}
else
{
cmd.Parameters.AddWithValue("@File_Uploads", ViewState["fn"]);
}
cmd.Parameters.AddWithValue("@isactive", cbchecked.Checked == true ? 1 : 0);
cmd.ExecuteNonQuery();
btnsave.Text = "Save";
con.Close();
Fill_grd();
clear();
}
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DEL")
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", e.CommandArgument);
//SqlParameter param = cmd.Parameters.Add("@File", SqlDbType.VarChar, 100);
//param.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
con.Close();
Fill_grd();
//string str = cmd.Parameters["@File"].Value.ToString();
//File.Delete(Server.MapPath("@File_Uplods" + "\\" + str));
}
else if (e.CommandName == "EDT")
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_edit", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", e.CommandArgument);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
Textname.Text = ds.Tables[0].Rows[0]["Name"].ToString();
rblgender.SelectedValue = ds.Tables[0].Rows[0]["Gender"].ToString();
ddlcountry.SelectedValue = ds.Tables[0].Rows[0]["Country"].ToString();
Fill_state(ddlcountry.SelectedValue);
ddlstate.SelectedValue = ds.Tables[0].Rows[0]["State"].ToString();
Textsalary.Text = ds.Tables[0].Rows[0]["Salary"].ToString();
Textgmail.Text = ds.Tables[0].Rows[0]["Gmail"].ToString();
Textpassword.Text = ds.Tables[0].Rows[0]["Password"].ToString();
Textcpassword.Text = ds.Tables[0].Rows[0]["Cpassword"].ToString();
Textdob.Text = ds.Tables[0].Rows[0]["dob"].ToString();
string[] arg = ds.Tables[0].Rows[0]["Hobbies"].ToString().Split(',');
cblhobbies.ClearSelection();
for (int i = 0; i < cblhobbies.Items.Count; i++)
{
for (int j = 0; j < arg.Length; j++)
{
if (cblhobbies.Items[i].Text== arg[j])
{
cblhobbies.Items[i].Selected = true;
break;
}
}
}
ViewState["fn"] = ds.Tables[0].Rows[0]["File_Upload"].ToString();
if (ds.Tables[0].Rows[0]["isactive"].ToString() == "1")
{
cbchecked.Checked = true;
}
else
{
cbchecked.Checked = false;
}
btnsave.Text = "Update";
ViewState["Eid"] = e.CommandArgument;
Fill_grd();
}
}
}
}
}
Server site validation:-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<table>
<tr>
<td>Name :</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvname" runat="server" ValidationGroup="A" ControlToValidate="txtname" ErrorMessage="please enter your name !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revname" runat="server" ValidationGroup="A" ForeColor="Red" Font-Bold="true" Display="None" ControlToValidate="txtname" ErrorMessage="please enter only alphabets !!" ValidationExpression="^[a-zA-Z]+$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>Password :</td>
<td>
<asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvpassword" runat="server" ValidationGroup="A" ControlToValidate="txtpassword" ErrorMessage="please enter your password !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Confirm Password :</td>
<td>
<asp:TextBox ID="txtcpassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvcpassword" runat="server" ValidationGroup="A" ControlToValidate="txtcpassword" ErrorMessage="please enter your confirm password !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvpassword" runat="server" ValidationGroup="A" ControlToValidate="txtcpassword" ControlToCompare="txtpassword" ErrorMessage="passowrd do not match !!" ForeColor="Red" Display="None" Font-Bold="true"></asp:CompareValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save" ValidationGroup="A" OnClick="btnsave_Click" /></td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Age :</td>
<td>
<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvage" runat="server" ValidationGroup="B" ControlToValidate="txtage" ErrorMessage="please enter your age !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvage" runat="server" ControlToValidate="txtage" ValidationGroup="B" ValueToCompare="18" Type="Integer" Operator="GreaterThanEqual" ErrorMessage="age should be greater than or equal to 18 !!" ForeColor="Red" Display="None" Font-Bold="true"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>Salary :</td>
<td>
<asp:TextBox ID="txtsalary" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvsalary" runat="server" ValidationGroup="B" ControlToValidate="txtsalary" ErrorMessage="please enter your salary !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvsalary" runat="server" ValidationGroup="B" ControlToValidate="txtsalary" MinimumValue="10000" MaximumValue="50000" Type="Integer" ErrorMessage="salary should be between 10000 and 50000 !!" ForeColor="Red" Display="None" Font-Bold="true"></asp:RangeValidator>
</td>
</tr>
<tr>
<td>Address :</td>
<td>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvaddress" runat="server" ValidationGroup="B" ControlToValidate="txtaddress" ErrorMessage="please enter your address !!" ForeColor="Red" Font-Bold="true" Display="None"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" ValidationGroup="B" OnClick="btnsubmit_Click" /></td>
</tr>
</table>
</td>
</tr>
</table>
<asp:ValidationSummary ID="vsA" runat="server" ValidationGroup="A" ShowMessageBox="true" ShowSummary="false" />
<asp:ValidationSummary ID="vsB" runat="server" ValidationGroup="B" ShowMessageBox="true" ShowSummary="false" />
</div>
</form>
</body>
</html>
2 comments:
Really good your tutorials
Good Tutorials sr
Post a Comment