you can do it same without post back of your page by using ajax.
function GetNameById(EmpCode,EmpName)
{
var retVal = doAjax('frmGetDetail.aspx','EmpCode='+EmpCode.value,0);
if(retVal.length>0)
{
EmpName.value=retval;
return true;
}
else
{
alert('The Employee Code Is not Exist.');
return false;
}
}
function doAjax(url,query,getxml)
{
var req;
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
req.open("POST", url + '?' + query,false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send();
if(req.readyState == 4)
{
if(req.status == 200)
{
var item = req.responseText;
if(getxml==1)
{
item = req.responseXML;
}
}
}
return item;
}
in your .cs page add attribute to your textbox in which you enter id
protected void Page_Load(object sender, EventArgs e)
{
txtEmpcode.Attributes.Add("onblur", "return GetNameById(" + txtEmpcode.ClientID + "," + txtEmpName.ClientID + ")");
}
now you add a page of ajax in which you call your query to get empname:-
in this aspx page only enter page directive which i have written and no any line needed to add
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmGetDetail.aspx.cs" Inherits="frmGetDetail" Theme=""%>
now in this page cs call your query like that
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(getEmpNameById(Request.QueryString["EmpCode"].ToString()));
}
private string getEmpNameById(string EmpCode)
{
string ConStr = @"server="serverName";database=dbName;uid=username;pwd=password";
DataSet ds = new DataSet();
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand cmd = new SqlCommand("Select EMPNAME from TableName Where id='"+ EmpCode +"'", Con);
ds= cmd.ExecuteDataSet());
Con.Close();
if (ds.Tables[0].Rows.Count > 0)
return(ds.Tables[0].Rows["EmpName"].ToString())
else
return "";
}