我有一個gridview。在這個我有一個DropDownList的Templatefield(在EditItemTemplate模式下的DDL,在ItemTemplate模式下的標籤)。 當我點擊detailsview的行的編輯時,我可以從DDL中選擇任何值(DDL從sqldatasource填充),但如果我嘗試執行更新,它會失敗,因爲它認爲我沒有提供數據...templatefield中的下拉列表 - 更新失敗(空值插入)
這裏是確切的錯誤(DB拒絕NULL數據):
無法將NULL值插入列 '狀態',表 'gyumolcs.dbo.orders';列不允許有空值。更新失敗。 聲明已被終止。
這裏是爲GridView的代碼:
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = @ID)"
UpdateCommand="UPDATE orders SET status = @status WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="orderadminSqlDataSource" DataKeyNames="ID" Width="608px"
AllowPaging="True" AllowSorting="True" PageSize="15">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="Felhasználónév"
SortExpression="UserName" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Mennyiség (kg)"
SortExpression="Quantity" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Termék"
SortExpression="Product" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Rendelés státusz" SortExpression="status">
<EditItemTemplate>
<!--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>-->
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="statustypeDDLSqlDataSource" DataTextField="name"
DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [status]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Date" HeaderText="Dátum" SortExpression="Date" />
</Columns>
<PagerSettings PageButtonCount="15" Mode="NumericFirstLast" />
</asp:GridView>
這裏是orderadminSqlDataSource的代碼(GridView的數據源)
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = @ID)"
UpdateCommand="UPDATE orders SET status = @status WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
請幫助我,我想不通的問題。 在此先感謝!
我想這不任何成功。我得到的錯誤是:「'DropDownList1'有一個SelectedValue,它是無效的,因爲它不存在於項目列表中 參數名稱:值」 – amman 2012-07-10 21:05:22
爲了擺脫這個錯誤,請執行:DropDownList1。SelectedIndex = -1;'或將此添加到標記:AppendDataBoundItems =「True」' – 2012-07-10 21:07:56
沒有「<%#Eval(」ID「)%>」和appendDataBoundItem我得到第一個錯誤(無法插入值NULL'列狀態'...),並與Eval && appenddatabounditems我得到第二個錯誤(DropDownList1'有一個SelectedValue這是無效的...) – amman 2012-07-10 21:18:02