如何使此DropDownList在GridView中工作
本文关键字:工作 GridView 何使此 DropDownList | 更新日期: 2023-09-27 18:26:09
我的数据库中有两个表,如下所示:
建议表:ID、标题、说明、状态ID。。。etc
建议状态表:ID,状态
我使用GridView来显示建议,在最后一列中,我放了一个DropDownList,用于选择每个建议的状态。现在,当我试图在DropDownList中显示状态时,我得到了以下错误,我不知道为什么:
非静态字段、方法或属性"System.Web.UI.WebControls.ListControl.Items.get"
我的ASP.NET代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="950px" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"
DataTextField="Status" DataValueField="ID" AutoPostBack="true"
OnDataBound="DropDownList_DataBound">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionShortcut
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
FilterExpression="[DivisionShortcut] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
<%--For the DropDownList--%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>
我的代码背后:
protected void Page_Load(object sender, EventArgs e)
{
}
int i = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = i.ToString();
i++;
}
}
protected void DropDownList_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList.Items.Insert(0, new ListItem("--Select--", ""));
}
}
那么我该如何解决这个问题呢
根据您似乎得到的异常,您可能必须将DropDownList_DataBound事件中的发件人强制转换为DropDownList,以便访问其Items集合:
protected void DropDownList_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", ""));
}
}
这一切都超出了我的想象,所以代码可能并不完美,但以下是您需要做的:
-
在RowDataBound中,从该行强制转换DropDownList的一个实例。
DropDownList drp = (DropDownList)e.Row.Cells[7].FindControl("DropDownList");
然后调用一个数据绑定函数,如下面的函数
-
绑定数据列表
protected void bindDrop(DropDownList drp) { if (!IsPostBack) { drp.Items.Insert(0, new ListItem("--Select--", "")); drp.DataBind() } }
这应该可以做到