We have to play with 4 functions,
Two client side functions i.e. java script function,
JavaScript Functions
RaiseCallbackEvent function gets call automatically whenever there is a CallbackEvent. And after this function 2nd function i.e. GetCallbackResult get called automatically.
DataBase Design
<html xmlns="http://www.w3.org/1999/xhtml">
function ReceiveServerData(retValue) {
using System.Data.SqlClient;
public partial class AddPage : System.Web.UI.Page,ICallbackEventHandler
}
public void RaiseCallbackEvent(String eventArgument)
Output
Two server side function i.e. c# (in this case).
1 function ReceiveServerData(retValue)
2 function Checkusername
Two client side functions i.e. java script function,
JavaScript Functions
RaiseCallbackEvent function gets call automatically whenever there is a CallbackEvent. And after this function 2nd function i.e. GetCallbackResult get called automatically.
DataBase Design
<html xmlns="http://www.w3.org/1999/xhtml">
function ReceiveServerData(retValue) {
using System.Data.SqlClient;
public partial class AddPage : System.Web.UI.Page,ICallbackEventHandler
}
public void RaiseCallbackEvent(String eventArgument)
Output
Two server side function i.e. c# (in this case).
1 function ReceiveServerData(retValue)
2 function Checkusername
.cs functions
1 public void RaiseCallbackEvent(String eventArgument)
2 public String GetCallbackResult()
AddPage.aspx
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function Checkusername ()
{
var values = document.getElementById("txtEname").value;
CallServer(values, "");
}
if (retValue == "N") {
alert("Please select another name; empname already exists");
document.getElementById("txtEname").value = "";
document.getElementById("txtEname").focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEno" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEname" runat="server" onblur="javascript:Checkusername()"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
AddPage.aspx.cs
using System.Configuration;
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg","ReceiveServerData", "context");
string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript,true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
con.Open();
SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
int i=cmd.ExecuteNonQuery();
if(i>0)
{
Response.Redirect("Default.aspx");
}
con.Close();
{
SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
SqlCommand com = new SqlCommand("select *from employee where ename='" +eventArgument + "'", con);
con.Open();
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows == true)
{
returnValue = "N";
}
else
{
returnValue = "Y";
}
con.Close();
}
public String GetCallbackResult()
{
return returnValue;
}
}
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function Checkusername ()
{
var values = document.getElementById("txtEname").value;
CallServer(values, "");
}
if (retValue == "N") {
alert("Please select another name; empname already exists");
document.getElementById("txtEname").value = "";
document.getElementById("txtEname").focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEno" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEname" runat="server" onblur="javascript:Checkusername()"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
AddPage.aspx.cs
using System.Configuration;
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg","ReceiveServerData", "context");
string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript,true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
con.Open();
SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
int i=cmd.ExecuteNonQuery();
if(i>0)
{
Response.Redirect("Default.aspx");
}
con.Close();
{
SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
SqlCommand com = new SqlCommand("select *from employee where ename='" +eventArgument + "'", con);
con.Open();
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows == true)
{
returnValue = "N";
}
else
{
returnValue = "Y";
}
con.Close();
}
public String GetCallbackResult()
{
return returnValue;
}
}
No comments:
Post a Comment
Thank you for visiting my blog