下拉列表项是隐藏的 [英] drop down list items are hidden

查看:88
本文介绍了下拉列表项是隐藏的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我创建服务的代码:





  public   String  GetEmployeerid()
{

string strConnection = ConfigurationManager.ConnectionStrings [ MyConsString]。ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand( 选择Employeertable的雇员,conn);
conn.Open();
// cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ;

}





这是获取雇员身份证的代码,从下拉列表中选择:



 受保护  void  Page_Load( object  sender,EventArgs e)
{

objService = new ServiceReference1.Service1Client();
DropDownList1.DataSource = objService.GetEmployeerid();
DropDownList1.DataBind();

}









但是之后调试我得到空的下拉列表项被隐藏。如何继续请建议。

解决方案

你必须让下拉列表知道应该用什么来显示作为文本,每个项目的价值应该是什么。在 databind 之前执行以下操作。



 DropDownList1.DataTextField =   Employeerid; 
DropDownList1.DataValueField = Employeerid;


您还需要更改此功能:

  public  DataTable GetEmployeerid( )
{

string strConnection = ConfigurationManager.ConnectionStrings [ MyConsString]。ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand( 选择Employeertable的雇员,conn);
conn.Open();
// cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}


在数据绑定方法之前写这个

 DropDownList1.DataTextField = Employeerid; 
DropDownList1.DataValueField = Employeerid;


this my code for creating the Service:


public  String GetEmployeerid()
      {

          string strConnection = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
          SqlConnection conn = new SqlConnection(strConnection);
          SqlCommand cmd = new SqlCommand("Select Employeerid from Employeertable", conn);
          conn.Open();
          //cmd.ExecuteNonQuery();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          return "";

      }



This is the code for get the employeer id, which is going to be selected from drop down:

protected void Page_Load(object sender, EventArgs e)
{

    objService = new ServiceReference1.Service1Client();
    DropDownList1.DataSource = objService.GetEmployeerid();
    DropDownList1.DataBind();

}





but after debugging i m getting empty dropdown the items are hidden. how to proceed please suggest.

解决方案

You will have to let the dropdownlist know what should be used to display as text and what should be the value of each item. to do that do the following before databind.

DropDownList1.DataTextField="Employeerid" ;
DropDownList1.DataValueField="Employeerid" ;


You also need to change this function:

public  DataTable GetEmployeerid()
        {
 
            string strConnection = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConnection);
            SqlCommand cmd = new SqlCommand("Select Employeerid from Employeertable", conn);
            conn.Open();
            //cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt; 
        }


write this before databind method

DropDownList1.DataTextField=Employeerid ;
DropDownList1.DataValueField=Employeerid ;


这篇关于下拉列表项是隐藏的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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