listview中的下拉列表是否将null项传递给数据库?

时间:2016-06-23 04:56:28

标签: asp.net

当我在listview中应用下拉列表时,他们只添加空值为什么?

目前我有一个下拉列表,其中包含自己的sqldatasource,它正在填充下拉列表。 ddl位于listviews中,它也位于sqldatasource上,插入项模板。但是,当我们单击insert时,传递给dbase的值为null。

    <asp:DropDownList ID="DropDownList1" SelectedValue= '<%# Bind("NationalityId") %>' CssClass="form-control" runat="server" 
DataSourceID="SqlDataSource1" DataTextField="NationalityName" DataValueField="NationalityId">
     </asp:DropDownList>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:CRMSConnectionString %>' SelectCommand="SELECT * FROM [Nationality]">
        </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource3" DataKeyNames="AreaId" InsertItemPosition="LastItem">
<InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton"  CssClass="btn-warning" Width="50px" Height="25px" />
                        <asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton"  CssClass="btn-warning" Width="50px" Height="25px" />
                    </td>
                    <td>
                        <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                             <ProgressTemplate>
                                 <img alt="as" src="Images/loader1.gif" style="width: 30px; height: 30px" />
                             </ProgressTemplate>
                         </asp:UpdateProgress>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("ProvinceId") %>' CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataSourceID="SqlDataSource4" DataTextField="ProvinceName" DataValueField="ProvinceId"></asp:DropDownList>
                        <asp:SqlDataSource runat="server" ID="SqlDataSource4" ConnectionString='<%$ ConnectionStrings:CRMSConnectionString %>' SelectCommand="SELECT * FROM [Province]"></asp:SqlDataSource>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList4" runat="server" SelectedValue='<%# Bind("CityId") %>' CssClass="form-control" DataSourceID="SqlDataSource5" DataTextField="CityName" DataValueField="CityId"></asp:DropDownList>
                        <asp:SqlDataSource runat="server" ID="SqlDataSource5" ConnectionString='<%$ ConnectionStrings:CRMSConnectionString %>' SelectCommand="SELECT City.CityId, City.CityName FROM City INNER JOIN Province ON City.ProvinceId = Province.ProvinceId WHERE (City.ProvinceId = @ProvinceId)">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="ProvinceId"></asp:ControlParameter>
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                    <td>
                        <asp:TextBox Text='<%# Bind("AreaName") %>' runat="server" ID="AreaNameTextBox" CssClass="form-control" /></td>
                </tr>
            </InsertItemTemplate>
</asp:ListView>

<asp:SqlDataSource runat="server" ID="SqlDataSource3" ConnectionString='<%$ ConnectionStrings:CRMSConnectionString %>' DeleteCommand="DELETE FROM [Area] WHERE [AreaId] = @AreaId" InsertCommand="INSERT INTO [Area] ([AreaName], [CityId], [ProvienceId]) VALUES (@AreaName, @CityId, @ProvienceId)" SelectCommand="SELECT * FROM [Area]" UpdateCommand="UPDATE [Area] SET [AreaName] = @AreaName, [CityId] = @CityId, [ProvienceId] = @ProvienceId WHERE [AreaId] = @AreaId">
            <DeleteParameters>
                <asp:Parameter Name="AreaId" Type="Int32"></asp:Parameter>
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="AreaName" Type="String"></asp:Parameter>
                <asp:Parameter Name="CityId" Type="Int32"></asp:Parameter>
                <asp:Parameter Name="ProvienceId" Type="Int32"></asp:Parameter>
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="AreaName" Type="String"></asp:Parameter>
                <asp:Parameter Name="CityId" Type="Int32"></asp:Parameter>
                <asp:Parameter Name="ProvienceId" Type="Int32"></asp:Parameter>
                <asp:Parameter Name="AreaId" Type="Int32"></asp:Parameter>
            </UpdateParameters>
        </asp:SqlDataSource>