0
我想綁定一個網格中的下拉菜單,但出現錯誤。在asp.net中綁定行數據綁定上的項目模板中的DropDownList
<asp:GridView ID="grdddl" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="grdddl_RowDataBound" ShowFooter="true" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlcommtype" SelectedValue='<%#Eval("ComPlanRoleDescr") %>' AutoPostBack="true" OnSelectedIndexChanged="ddlcommtype_SelectedIndexChanged" runat="server"></asp:DropDownList>
<asp:HiddenField ID="hdnid" Value='<%#Eval("ID") %>' runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlcommtypefooter" AutoPostBack="true" OnSelectedIndexChanged="ddlcommtypefooter_SelectedIndexChanged" runat="server"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grdddl_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtype");
ddlcommtype.DataSource = listcommtype;
ddlcommtype.DataTextField = "ComPlanRoleDescr";
ddlcommtype.DataValueField = "ID";
ddlcommtype.DataBind();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtypefooter");
ddlcommtype.DataSource = listcommtype;
ddlcommtype.DataTextField = "ComPlanRoleDescr";
ddlcommtype.DataValueField = "ID";
ddlcommtype.DataBind();
}
}
本準則給錯誤: 「ddlcommtype」具有的SelectedValue,因爲它不在項目列表中存在哪些無效。
但我需要選擇一個值,它在列表中綁定到網格。 –
這個錯誤是第一次,當控制不進入數據行或頁腳行 –
查看更新的答案 – Delosdos