GridView 使用 DropDownList 出现绑定空值的异常处理

<asp:TemplateField HeaderText="父节点">
                    <EditItemTemplate>
                         <asp:DropDownList ID="DropDownList1"  runat="server"
                            DataSourceID="SqlDataSource_ZipNode" DataTextField="DistrictDesc"
                            DataValueField="DistrictId"  SelectedValue='<%# Bind("ParentId") %>'
                             AppendDataBoundItems="True" ondatabinding="DropDownList1_DataBinding"
                             ondatabound="DropDownList1_DataBound"
                             onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                            
                             <asp:ListItem Enabled="false"></asp:ListItem>
                            
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource_ZipNode" runat="server"
                            ConnectionString="<%$ ConnectionStrings:connStr %>" 
                            SelectCommand="prd_SelectDistrictNodes" SelectCommandType="StoredProcedure">
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ParentId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

 

要点已经标出

 

 

//上面这种方法有局限性,还有一种通用的方法

 <EditItemTemplate> 模版里的DropDownList 不用SelectedValue绑定

在GridView事件 _RowUpdating

 DropDownList objtemp_ZipId=GV_SheBeiDelete.Rows[e.RowIndex].FindControl("DropDownList3") as DropDownList;
        DropDownList objtemp_DvsTypeId = GV_SheBeiDelete.Rows[e.RowIndex].FindControl("DropDownList4") as DropDownList;
        e.NewValues["ZipId"] = objtemp_ZipId.Text;
        e.NewValues["DvsTypeId"] = objtemp_DvsTypeId.Text;

 

当然对于编辑模版里的 DropDownList 要写事件  DropDownList4_DataBound
 DropDownList obj = sender as DropDownList;
        try
        {
            obj.Text = DvsTypeId_GV_SheBeiDelete; ; //变量,其取值在GridView的RowEditing事件里

        }
        catch
        {

        }

你可能感兴趣的:(server,asp)