我们无法根据静态方法列表框显示值 [英] Unable to display values in ListBox from static method

查看:131
本文介绍了我们无法根据静态方法列表框显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在静态的WebMethod,我无法得到ListBoxcontrol,所以我认为存储在Page_Load事件会话控制:

  HttpContext.Current.Session [LB_AvailFields] = lbavailablefields;

调用的WebMethod我是从数据库中获取价值,并试图以填补列表框列表框,但没有显示任何值之后。
 下面是硬codeD测试code这在Page_Load中,但不是在WebMethod的行为罚款请建议我要去哪里错了?

 列表< MyListItem> LB =新的List< MyListItem>();
MyListItem lbitem;
的for(int i = 0;我小于5;我++)
{
    lbitem =新MyListItem();
    lbitem.PMKey =键+我;
    lbitem.PMSystemName =系统名称:+我;
    LB.Add(lbitem);
}
列表框lbox =(列表框)HttpContext.Current.Session [LB_AvailFields];
lbox.DataSource = LB;
lbox.DataTextField =PMSystemName;
lbox.DataValueField =PMKey;
lbox.DataBind();


解决方案

WebMethod能够概念是给你的体验快速的Web服务。该网页不存在于服务器上,因为它的Page_Load时工作。预期的用途是从调用客户端脚本的Web方法,数据返回到客户端和消费有(JavaScript的)

  [的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共MyReturnResultObject [] GetListBoxData()
{
    清单< MyReturnResultObject>结果=新的List< MyReturnResultObject>();
    循环来填补你的返回结果
    {
        VAR oneResult =新MyReturnResultObject();
        ...
        result.Add(oneResult);
    }
    返回result.toArray();
}

然后在客户端,在那里你用javascript调用该方法,只要使用返回的JSON来填充你的列表框(使用JavaScript)。有很多方法可以做到这在JavaScript中,显示出最基本的;如果jQuery是提供给你,你可以使用它。

  VAR myList中= ...; //获取引用您的列表框中
   VAR anOption;
   通过JSON循环
       anOption = document.createElement方法(选项);
       anOption.text = ...; //从JSON
       anOption.value = ...; //从JSON
       myList.add(anOption);

On Static WebMethod I'm unable to get ListBoxcontrol, so I have stored that control in session on Page_Load event as:

HttpContext.Current.Session["LB_AvailFields"] = lbavailablefields;

After calling WebMethod I'm getting values from Database and trying to fill ListBox but ListBox not showing any values. Following is hardcoded testing code which works fine on Page_Load but not in WebMethod please suggest where I'm going wrong?

List<MyListItem> LB = new List<MyListItem>();
MyListItem lbitem;
for(int i= 0;i<5;i++)        
{
    lbitem = new MyListItem();
    lbitem.PMKey = "Key" + i;
    lbitem.PMSystemName = "SystemName: " + i;
    LB.Add(lbitem);
}
ListBox lbox = (ListBox)HttpContext.Current.Session["LB_AvailFields"];
lbox.DataSource = LB;
lbox.DataTextField = "PMSystemName";
lbox.DataValueField = "PMKey";
lbox.DataBind();

解决方案

The WebMethod concept is to give you a quick web service like experience. The web page doesn't exist on the server, as it does when Page_Load works. The intended use is to call web methods from client script, return data to client side and consume there (javascript)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public MyReturnResultObject[] GetListBoxData()
{
    List<MyReturnResultObject> result = new List<MyReturnResultObject>();
    loop to fill your return result
    {
        var oneResult = new MyReturnResultObject();
        ...
        result.Add(oneResult);
    }
    return result.toArray();
}

Then on the client side, where you use javascript to call that method, just use the returned json to populate your listbox (using javascript). There are many ways to do this in javascript, showing most rudimentary; if jQuery is available to you you could use it.

   var myList = ...; // obtain a reference to your list box
   var anOption; 
   loop through json
       anOption = document.createElement("Option"); 
       anOption.text = ...; //from json
       anOption.value = ...; //from json
       myList.add(anOption);

这篇关于我们无法根据静态方法列表框显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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