用来自CodeBehind的数据表填充下拉列表
本文关键字:数据表 填充 下拉列表 CodeBehind | 更新日期: 2023-09-27 18:17:45
我如何设置这个下拉列表与数据表从代码后面?
<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
为null
RiskWorkDropDownList.DataSource = templateFields;
我正试着让它像这个问题/答案一样工作:
DropdownList数据源
像这样简单的事情应该可以帮助你:
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>
您可能希望在创建新行时绑定每个新的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();
}
}
我假设你正在使用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();
只需将下拉菜单的数据源设置为数据表:
theDropDownList.DataSource = dataTable; //Your data table
theDropDownList.DataBind();