zoukankan      html  css  js  c++  java
  • ASP.NET DropDownList FindByValue 未将对象引用设置到对象的实例 解决方法

    1、粗心把DataValueField中的字段名称写错了。

    2、把DataBind()写在了FindByValue()后面了,以下代码是正确顺序:

     1 BLL.Student bllStu = new BLL.Student();
     2 //设置数据源
     3 StuDDL.DataSource = bllStu.GetStudentList();
     4 //设置显示数据字段名称
     5 StuDDL.DataTextField = "StuName";
     6 //设置Value值字段名称
     7 StuDDL.DataValueField = "StuId";
     8 //绑定
     9 StuDDL.DataBind();
    10 //设置选中值
    11 StuDDL.Items.FindByValue(stuId).Selected = true;

    3、数据源中确实没有找到该值。那就该加非Null判断了,代码如下:

     1  BLL.Student bllStu = new BLL.Student();
     2  //设置数据源
     3  StuDDL.DataSource = bllStu.GetStudentList();
     4  //设置显示数据字段名称
     5  StuDDL.DataTextField = "StuName";
     6  //设置Value值字段名称
     7  StuDDL.DataValueField = "StuId";
     8  //绑定
     9  StuDDL.DataBind();
    10  //获取选中值
    11  object findValue=StuDDL.Items.FindByValue(stuId);
    12  if(findValue!=null)
    13  {
    14    findValue.Selected = true;
    15  }
  • 相关阅读:
    Spring 发生 has not been refreshed yet 异常
    rsyslog config
    grok
    阿里云态势
    Unity的asm笔记
    Unity2020或Unity2019安装后无法启动
    rider代码折叠
    使用rider调试lua
    MacType更好的字体渲染
    Unity字体和画面花屏处理
  • 原文地址:https://www.cnblogs.com/2333/p/5076202.html
Copyright © 2011-2022 走看看