我的数据库中有两个表如下:
建议表:ID,标题,描述,StatusID等等
SuggestionsStatus表:ID,状态
我正在使用GridView来显示建议,在最后一栏中我放了一个DropDownList来选择每个Suggestion的状态。现在,当我试图在DropDownList中显示状态时,我收到以下错误,我不知道原因:
非静态字段,方法或者需要对象引用 property'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--", ""));
}
}
那我该如何解决这个问题呢?
答案 0 :(得分:3)
基于您似乎得到的异常,您可能必须将DropDownList_DataBound事件中的发送者强制转换为DropDownList才能访问其Items集合:
protected void DropDownList_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", ""));
}
}
答案 1 :(得分:2)
这一切都在我的头脑中,所以代码可能不完美,但这就是你需要做的事情:
在RowDataBound中,从行中强制转换DropDownList的实例。
DropDownList drp = (DropDownList)e.Row.Cells[7].FindControl("DropDownList");
然后调用类似于下面的数据绑定函数
绑定您的datalist
protected void bindDrop(DropDownList drp)
{
if (!IsPostBack)
{
drp.Items.Insert(0, new ListItem("--Select--", ""));
drp.DataBind()
}
}
应该这样做