绑定列表<>使用dropdwnlist [英] bind list<> with dropdwnlist

查看:62
本文介绍了绑定列表<>使用dropdwnlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< asp:DropDownList ID =HWadrrunat =serverDataSource =<%#LoadHwAddr()%> DataTextField =textDataValueField =Value>



<asp:DropDownList ID="HWadr" runat="server" DataSource="<%# LoadHwAddr() %>" DataTextField="text" DataValueField="Value">

public List<int> LoadHwAddr()
  {
      int _start = int.Parse(ViewState["StartAddress"].ToString());
      int _End = int.Parse(ViewState["EndAddress"].ToString());



      List<int> list = new List<int>();
      for (int i = _start; i < _End; i++)

      {

          list.Add(i);

      }



      return list;

  }







错误发生对象引用未设置为instance..wht我错过了..




error occured object reference not set to instance..wht i have missed in it..

推荐答案

1.问题是来自 ViewState 的值可能为空,例如在第一次调用的情况下,当页面还没有回发。这个空值会产生错误。



2.解决方案是管理这两种情况,如下面的代码所示:

1.The problem is that your values from the ViewState may be null, for example in the case of first call, when the page was not yet postback. And this null values generate the error.

2.The solution is to manage these two cases like in the code bellow:
public List<int> LoadHwAddr()
  {
    if(Page.IsPostBack)
    {  
      //
      // This case is when your page is postback (POST), so only now you could have something in your ViewState!
      // 
      int _start = int.Parse((string)ViewState["StartAddress"]); //Unboxing and not ToString()!
      int _End = int.Parse((string)ViewState["EndAddress"]);
    }
    else
    {
        //This is when your page is first created by a GET request.
        //...
    } 
    //...
  }


这篇关于绑定列表&lt;&gt;使用dropdwnlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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