如何使用代码隐藏的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;
我正试图让它像这个问题/答案一样工作:
答案 0 :(得分: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>
答案 1 :(得分:0)
您可能希望在创建新行时绑定每个新的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();
}
}
答案 2 :(得分:0)
我假设您正在使用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();
答案 3 :(得分:0)
这只需将下拉菜单的数据源设置为数据表即可:
theDropDownList.DataSource = dataTable; //Your data table
theDropDownList.DataBind();