Skip to main content

How To Check user exist or not by using Client CallBack




<%@ 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>Asp.net 2 Client Callback and AJAX Patterns</title>

<script type="text/ecmascript">
function lookupValue()
{
var control = document.getElementById("UserNameTextBox");
var value = control.getAttribute("value");
CallServer(value, "");
}

function displayResult(rValue)
{
document.getElementById("result").innerHTML = rValue;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="UserNameTextBox" style="width: 200px" />
<input type="button" id="ValidateButton" value="Check Availability" onclick="javascript:lookupValue()" />
<br />
<span id="result" style="color: Red;"></span>
</div>
</form>
</body>
</html>


using System;
using System.Web.Security;
using System.Web.UI;

public partial class _Default : Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "displayResult", "", true);

string CallbackScript = "function CallServer(arg, context){" + cbReference + "; }";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallBack", CallbackScript, true);
}

protected string IsUserExist;
public void RaiseCallbackEvent(string eventArgument)
{
try
{
if (GetUser(eventArgument) != "")
IsUserExist = "User name not available";
else
IsUserExist = "User name available";
}
catch (Exception ex)
{
IsUserExist = ex.Message;
}
}

public string GetCallbackResult()
{
return IsUserExist;
}
protected string GetUser(string s)
{
//I have used here a dummy method that returns user you
// can used return user from database here.
string strTemp;

System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add("santosh", "santosh");
ht.Add("test", "test");
if (ht.Contains(s))
{
strTemp = ht[s].ToString();
}
else
{
strTemp = "";
}
return strTemp;
}
}


Comments

Popular posts from this blog