hi guys...i have written this piece of code of javascript which calls server side function and sets value for one code...i tried to return the array also but its just not working ...javascript doesn't give the result that i want.
Javascript :
<script type="text/javascript">
function CallMe(src,dest,dest1)
{
var ctrl = document.getElementById(src);
PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}
function CallSuccess(res, destCtrl)
{
var dest = document.getElementById(destCtrl);
dest.value = res;
}
function CallFailed(res, destCtrl)
{
alert(res.get_message());
}
</script>
<script type="text/javascript">
function CallMe(src,dest)
{
var ctrl = document.getElementById(src);
PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}
function CallSuccess(res, destCtrl)
{ alert(res);
var dest = document.getElementById(destCtrl);
dest.value = res;
}
function CallFailed(res, destCtrl)
{
alert(res.get_message());
}
</script>
Page Load: I add the atrribute at pageload
txtmobileno.Attributes.Add("onblur", "javascript:CallMe('" + txtmobileno.ClientID + "', '" + txtname.ClientID + "')");
Fucntion :
[System.Web.Services.WebMethod]
public static string getData(string _mobileno)
{
if (_mobileno == null || _mobileno.Length == 0)
{
return String.Empty;
}
else
{
OleDbConnection conn = null;
string cnstring = ConfigurationSettings.AppSettings["ConnectionString"];
conn = new OleDbConnection(cnstring);
string _strQry = "Select distinct VISITOR_NAME from VISITOR_REG where MOBILE_NO=" + _mobileno + "";
OleDbCommand cmd = new OleDbCommand(_strQry, conn);
cmd.Parameters.AddWithValue("MOBILE_NO", _mobileno);
conn.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
return name;
}
}
Now it works fine for just Visitor name, but if i change query and add COMPANY_NAME field to the query and make necessary changes which returns the array as it would be 2 fields..javascript doesnt give me the correct output.
changed javascript :
<script type="text/javascript">
function CallMe(src,dest,dest1)
{
var ctrl = document.getElementById(src);
PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}
function CallSuccess(res, destCtrl,destCtrl1)
{
var dest = document.getElementById(destCtrl);
alert(dest);
var dest1 = document.getElementById(destCtrl1);
alert(dest1);
dest.value = res[0];
dest1.value=res[1];
}
function CallFailed(res, destCtrl,destCtrl1)
{
alert(res.get_message());
}
</script>
changed attribute at page load
txtmobileno.Attributes.Add("onblur", "javascript:CallMe('" + txtmobileno.ClientID + "', '" + txtname.ClientID + "','"+txtcompany.ClientID+"')");
changed function :
[System.Web.Services.WebMethod]
public static string[] getData(string _mobileno)
{
OleDbConnection conn = null;
string cnstring = ConfigurationSettings.AppSettings["ConnectionString"];
conn = new OleDbConnection(cnstring);
string _strQry = "Select distinct VISITOR_NAME,COMPANY from VISITOR_REG where MOBILE_NO=" + _mobileno + "";
OleDbCommand cmd = new OleDbCommand(_strQry, conn);
cmd.Parameters.AddWithValue("MOBILE_NO", _mobileno);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
string[] data = new string[ds.Tables[0].Columns.Count];
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
data[i] = ds.Tables[0].Columns[i].ToString();
}
return data;
}
Help me...