2016-08-22 1 views
0

나는 격자에있는 드롭 다운을 바인딩하려고하지만 오류가 발생합니다.바인드 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 있습니다. 당신의 GridView 당신이 당신의 DropDownList에 대한 SelectedValue하지만 값을 설정하는에서

답변

0

은 DropDownList로에 속하는 ListItem 년대에서 찾을 수 없습니다.

이 줄에서 SelectedValue을 제거

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

을 당신이 SelectedValue, 왜 DataBind() 후하지 사용할 경우? 예 :

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

관련 문제