<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
User ID:
<asp:TextBox ID = "txtUserID" runat = "server" />
<input id="btnCheckID" value="Check availability" type="button" onclick="usernameChecker();" />
<span id="spanAvailability"></span>
</div>
<div id="divRadiolist">
</div>
</asp:Content>
var usernameCheckerTimer;
var spanAvailability = document.getElementById("spanAvailability");
var dvSuggest = document.getElementById("divRadiolist");
function usernameChecker() {
var username1 = document.getElementById("ctl00_ContentPlaceHolder1_txtUserID");
var username = username1.value;
clearTimeout(usernameCheckerTimer);
spanAvailability.innerHTML = "<span style='color: #ccc;'>checking...</span>";
usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 750);
}
function checkUsernameUsage(username) {
// initiate the ajax pagemethod call
// upon completion, the OnSucceded callback will be executed
PageMethods.IsUserAvailable(username, OnSucceeded);
}
// Callback function invoked on successful completion of the page method.
function OnSucceeded(result, userContext, methodName) {
if (methodName == "IsUserAvailable") {
if (result == "0") {
spanAvailability.innerHTML = "<span style='color: DarkGreen;'>Available</span>";
divRadiolist.style.display = "none";
}
else {
spanAvailability.innerHTML = "<span style='color: Red;'>Unavailable</span>";
divRadiolist.style.visibility = "visible";
/** This code is for show the suggesstion.Added by zafor 2010/10/27 **/
var checkAvailable = result.toString().split('|');
//Two dimentional array hold suggested user id
var arrData = [["0", checkAvailable[0]], ["1", checkAvailable[1]], ["2", checkAvailable[2]], ["3", checkAvailable[3]]];
//read div which show available user id
var radioDiv = document.getElementById("divRadiolist");
var username1 = document.getElementById("ctl00_ContentPlaceHolder1_txtUserID");
var username = username1.value;
radioDiv.innerHTML = "<p style='color: Red;'>" + username + " is not available but the followings are</p>";
//This for loop used to create the radio list
for (var i = 0; i < arrData.length; i++) {
var createRadioItem = document.createElement("input");
createRadioItem.type = "radio";
createRadioItem.name = "radioButtonGroup";
createRadioItem.id = "radioItem_" + i;
createRadioItem.value = arrData[i][1]
createRadioItem.setAttribute("onclick", "JavaScript:return getSelectedRadioValue()");
var objTextNode = document.createTextNode(" " + arrData[i][1]);
var createLabel = document.createElement("label");
createLabel.htmlFor = createRadioItem.id;
createLabel.appendChild(createRadioItem);
createLabel.appendChild(objTextNode);
var createBreak = document.createElement("br");
radioDiv.appendChild(createLabel);
radioDiv.appendChild(createBreak);
};
/** End the suggestion code **/
divRadiolist.style.display = "block";
}
}
}
</script>
<%-- This javascript function is for select suggesstion. Added by zafor 2010/10/27--%>
<script type="text/javascript">
function getSelectedRadioValue() {
var node = document.getElementsByName('radioButtonGroup');
for (var i = 0; i < node.length; i++) {
if (node[i].checked) {
var rad_val = node[i].value;
}
}
var userID = document.getElementById('ctl00_ContentPlaceHolder1_txtUserID');
userID.value = rad_val;
}
</script>
{
"shimul","zakir","zafor","sohel","sumon"
};
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string IsUserAvailable(string username)
{
string userAvailability = string.Empty;
bool userExists = false;
string tempOid = string.Empty;
//Check the username in user list
foreach (var item in UserList)
{
if (item.Equals(username))
{
userExists = true;
}
}
if (!userExists)
userAvailability += "0";
else
{
//hold suggested user id
List<string> LstUserOID = new List<string>();
System.Random random = new Random();
while (LstUserOID.Count < 4)
{
tempOid = string.Empty;
//create temporary oid
tempOid += username + random.Next(0, 100);
// test is the id exists in login table
if (!UserList.Contains(tempOid))
{
//if not exists then put it in the list
LstUserOID.Add(tempOid);
}
}
foreach (var item in LstUserOID)
{
userAvailability += item + "|";
}
}
return userAvailability;
}