Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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);
    // call server side method
    //alert(ctrl.value);
    PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}

// set the destination textbox value with the ContactName
function CallSuccess(res, destCtrl)
{
    var dest = document.getElementById(destCtrl);
    dest.value = res;
}

// alert message on some failure
function CallFailed(res, destCtrl)
{
    alert(res.get_message());
}
   </script>
   <script type="text/javascript">
   function CallMe(src,dest)
{
    var ctrl = document.getElementById(src);
    // call server side method
    //alert(ctrl.value);
    PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}

// set the destination textbox value with the ContactName
function CallSuccess(res, destCtrl)
{    alert(res);
    var dest = document.getElementById(destCtrl);
    dest.value = res;
}

// alert message on some failure
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);
    // call server side method
    //alert(ctrl.value);
    PageMethods.getData(ctrl.value, CallSuccess, CallFailed, dest);
}

// set the destination textbox value with the ContactName
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];
}

// alert message on some failure
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();
        //string name = Convert.ToString(cmd.ExecuteScalar());
        //return name;
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        da.Fill(ds);
        //return (ds.Tables[0]);
        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...
Posted
Updated 4-Apr-11 23:52pm
v4
Comments
Toniyo Jackson 5-Apr-11 5:38am    
Use pre tag for code
ashu2188 5-Apr-11 5:39am    
yeah
Sandeep Mewara 5-Apr-11 5:42am    
1. .javascript doesnt give me the correct output
Elaborate more.

make necessary changes which returns the array as it would be 2 fields
Add what changes you did. Might have done wrong.
ashu2188 5-Apr-11 5:48am    
Its been updated...with the changes that i made.
Karthik. A 5-Apr-11 19:41pm    
jquery simplifies all of this, I would strongly advice using that - a link - http://api.jquery.com/category/ajax/ - look mainly into $.load(...), $jQuery.get(...), $jQuery.post(...), $jQuery.ajax(...)

1 solution

you are getting a single string as the result

but

in case of array you wont

right?

then pass the array as single string by using a separator

example

if your string array is

res={"one","two","three","four"}

convert it to a single string using separator as '[?]'

like

resString="one[?]two[?]three[?]four[?]";

and in javascript you can split it using

string.split("[?]") this code will return string array

try it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900