【问题标题】:Populate DropDownList with DataTable from CodeBehind使用 CodeBehind 中的 DataTable 填充 DropDownList
【发布时间】:2015-10-14 00:06:39
【问题描述】:

如何使用后面代码中的 DataTable 设置此下拉列表?

<asp:GridView ID="gvTemplateFields"
                runat="server"
                CssClass="grid"
                AutoGenerateColumns="false"

    <Columns>
        <asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
            <ItemTemplate>
                <asp:DropDownList ID="RiskWorkDropDownList" runat="server">
                    <asp:ListItem Value="1">Pendiente</asp:ListItem>
                    <asp:ListItem>Atendido</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>                
    </Columns>

    <EmptyDataTemplate>No off-site links found.</EmptyDataTemplate>

</asp:GridView>

背后的代码:

public int SWMSTemplateId;
public DropDownList RiskWorkDropDownList;

protected void Page_Load(object sender, EventArgs e)
{
    SWMSTemplateId = int.Parse(Request.QueryString["templateid"]);

    DataTable templateFields = SWMSField.GetTemplateFields(SWMSTemplateId);

    RiskWorkDropDownList.DataSource = templateFields;
    RiskWorkDropDownList.DataBind();

}

错误:

System.NullReferenceException: Object reference not set to an instance of an object.

RiskWorkDropDownList 为空

RiskWorkDropDownList.DataSource = templateFields;

我正试图让它像处理这个问题/答案一样工作:

DropdownList DataSource

【问题讨论】:

  • 展示你目前对背后代码的尝试,这样回答的人就不必从头开始写了。
  • 哪一行给你错误?什么对象是空的?你知道 NullReferenceException 是什么吗?

标签: c# asp.net


【解决方案1】:

像这样简单的东西应该可以帮助你:

C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable theTable = new DataTable();
            theTable.Columns.Add("Names", typeof(string));
            theTable.Rows.Add("Name1");
            theTable.Rows.Add("Name2");
            theTable.Rows.Add("Name3");

            for (int i = 0; i < theTable.Rows.Count; i++ )
            {
                string theValue = theTable.Rows[i].ItemArray[0].ToString();
                DropDownList1.Items.Add(theValue);
            }

        }
    }
}

ASP

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

【讨论】:

  • 我不能只使用 DataBind() 之类的东西吗?
  • 不值得投反对票,因为你没有要求它。当您提供的信息很少或根本没有提供时,不要对帮助您的人投反对票。
  • 以防万一,我也没有。
【解决方案2】:

您可能希望在创建新行时绑定每个新的 DropDownList 实例。那么你可能想要 GridView.RowCreated 事件。

void gvTemplateFields_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        (DropDownList)riskWorkDropDownList = (DropDownList)e.Row.FindControl("RiskWorkDropDownList");

        riskWorkDropDownList.DataSource = dataTable; //Your data table
        riskWorkDropDownList.DataBind(); 
    }
}

【讨论】:

    【解决方案3】:

    我假设您正在使用 SqlExpress 服务器并尝试将数据从数据库表中获取到下拉列表中, 请试试这个。

    var Cn = new System.Data.SqlClient.SqlConnection();
    Cn.ConnectionString = "Server=.\\SqlExpress;Database=YourDatabasename;Trusted_Connection=True";
    Cn.Open();
    var Cm = Cn.CreateCommand();
    Cm.CommandText = string.Format(@"Select * From DataTablename");
    var Dr = Cm.ExecuteReader();
    
    //add all data from database to dropdownlist
    while (Dr.Read())
        {
           RiskWorkDropDownList.Items.Add(new ListItem(Dr.GetValue(1/*your table column*/).ToString()));
        }
    
    Cn.Close();
    

    【讨论】:

      【解决方案4】:

      只需将下拉菜单的数据源设置为数据表即可:

      theDropDownList.DataSource = dataTable; //Your data table
      theDropDownList.DataBind(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-27
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多