2016-08-22 90 views
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,因爲它不在項目列表中存在哪些無效。

回答

0

在你GridView要設置一個SelectedValueDropDownList,但 值不能在屬於DropDownList中的ListItem的發現。

從該行中刪除SelectedValue

<asp:DropDownList ID="ddlcommtype" SelectedValue='<%#Eval("ComPlanRoleDescr") %>' AutoPostBack="true" OnSelectedIndexChanged="ddlcommtype_SelectedIndexChanged" runat="server"></asp:DropDownList> 

如果你想使用SelectedValueDataBind()後,爲什麼不去做呢?例如:

DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtype"); 
ddlcommtype.DataSource = listcommtype; 
ddlcommtype.DataTextField = "ComPlanRoleDescr"; 
ddlcommtype.DataValueField = "ID"; 
ddlcommtype.DataBind(); 

// Now set the default value: 
ddlcommtype.SelectedValue = "InsertValueHere"; 
+0

但我需要選擇一個值,它在列表中綁定到網格。 –

+0

這個錯誤是第一次,當控制不進入數據行或頁腳行 –

+0

查看更新的答案 – Delosdos

相關問題