简体   繁体   English

如何获取Dropdownbox C#ASP.NET的Datasource()

[英]How to get Datasource() for Dropdownbox C# ASP.NET

I have table tblDepartments with columns DeptID, Department, Description . 我有表tblDepartmentsDeptID, Department, Description I also have dropdownbox drpDepartments . 我也有dropdownbox drpDepartments I want to display the contents of column Department on the dropdownbox. 我想在下拉框中显示“ Department ”列的内容。 I tried using this C# code for winforms but it didn't work: 我尝试对Winforms使用此C#代码,但没有成功:

        drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
        drpDepartments.DisplayMember = "Department";
        drpDepartments.ValueMember = "DeptID";
        drpDepartments.Text = "Choose Department";

How do I do it using ASP.NET C#? 如何使用ASP.NET C#? Thanks. 谢谢。

New code (still not working) 新代码(仍然无法正常工作)

        sConn = new SqlConnection(sStr);
        daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
        daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
        dsEmp = new DataSet();
        dsDep = new DataSet();

        daEmp.Fill(dsEmp, "tblEmployee");
        daDep.Fill(dsDep, "tblDepartment");

        dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

        DataTable dt=new DataTable();
        daDep.Fill(dt);

        drpDepartments.DataTextField = "Department";
        drpDepartments.DataValueField = "DeptID";
        drpDepartments.DataSource = dt;
        drpDepartments.DataBind();

You have to call it like this in asp.net webForms 您必须在asp.net webForms中这样称呼它

        drpDepartments.DataSource = dsDep.Tables["tblDepartment"];// Set DataSource Table First
        drpDepartments.DataTextField = "Department";// Set Column Name of DataTable to set as Text Field
        drpDepartments.DataValueField = "DepartmentID";// Set Column Name of DataTable to set as Value Field
        drpDepartments.DataBind();

You must be using it from using System.Web.UI.WebControls; 您必须using System.Web.UI.WebControls;使用它using System.Web.UI.WebControls; namespace. 命名空间。 It seems like you were using winforms namespaces. 似乎您正在使用winforms命名空间。

DataTextField is equivalent to DisplayMember and ValueMember is equivalent to DataValueField . DataTextField等效于DisplayMemberValueMember等效于DataValueField

Did you call DataBind()? 您是否致电DataBind()?

drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
drpDepartments.DisplayMember = "Department";
drpDepartments.ValueMember = "DepartmentID";
drpDepartments.Text = "Choose Department";
drpDepartments.DataBind();
drpDepartments.DataSource = ds1;
drpDepartments.DataTextField = "textcol";
drpDepartments.DataValueField = "valuecol";
drpDepartments.DataBind();

after doing this in btn_save method add this.. 在btn_save方法中执行此操作后,添加此。

 cmd.Parameters.AddWithValue("@a", drpDepartments.SelectedValue);

hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在GridView上更改数据源(C#,ASP.Net) - How to change the DataSource on a GridView (C#, ASP.Net) 在ASP.NET的C#中更新DropDownList的DataSource - Updating the DataSource of a DropDownList in ASP.NET's C# ASP.net GridView - 使用dataSource - C# - ASP.net GridView - using dataSource - C# 如何从存储在C#.net客户端的下拉框中显示的proc n中获取数据? - how to get the data from stored proc n show on the dropdownbox in c# .net client side? C#Asp.Net使用DropDownList更改GridView的数据源 - C# Asp.Net changing DataSource for GridView using DropDownList 如何使用C#将数据源绑定到ASP.NET中动态ListView中的DropDownList? - How I an bind a DataSource to a DropDownList in a dynamic ListView in ASP.NET with C#? 如何从asp.net C#中的控制数据源反向解析 - how to reverse parse from control datasource in asp.net C# 如何在代码后面访问和/或修改(sql)DataSource(ASP.net c#)? - How to access and/or modify the (sql)DataSource in code behind (ASP.net c#)? 如果DataSource不包含任何项目,如何在ASP.NET C#中隐藏转发器? - How can I hide a repeater in ASP.NET C# if the DataSource contains no items? 如何使用C#将数据源分配给gridview ASP.net中的转发器内的下拉列表 - how to assign a datasource to a dropdownlist inside a repeater in a gridview ASP.net with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM