从javascript调用服务器端函数并返回字符串数组并设置文本框的值 [英] call serverside function from javascript and return string array and set values of textbox

查看:77
本文介绍了从javascript调用服务器端函数并返回字符串数组并设置文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们...我写了这段JavaScript代码,它调用了服务器端函数并为一个代码设置了值...我也试图返回该数组,但它只是不能正常工作... javascript不能.''不能给出我想要的结果.

Javascript:

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;
    }
}


现在,它仅适用于访问者姓名,但如果我更改查询并向查询中添加COMPANY_NAME字段并进行必要的更改,则返回该数组,因为它将是2个字段..javascript不能为我提供正确的输出.

更改了javascript:


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...

推荐答案

您将得到一个字符串作为结果

但是

如果是数组,您不会

对吧?

然后使用分隔符将数组作为单个字符串传递

例子

如果您的字符串数组是

res = {一个",两个",三个",四个"}

使用分隔符将其转换为单个字符串,例如"[?]"

喜欢

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

在javascript中,您可以使用
对其进行拆分
string.split("[?]")此代码将返回字符串数组

试试吧
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


这篇关于从javascript调用服务器端函数并返回字符串数组并设置文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆