Answered by:
How to modify the source codes of a FormView control in Insert mode to display DDL controls?

Question
-
User-830563764 posted
In a FormView data control of a VS2010 ASP.net 4 webpage, set as Insert mode,
the empty Textboxes are displayed for entering the new data entries.Is it possible I can modify the source codes of the FormView control, so that,
instead of the empty textboxes, the DDL(dropdownlist) controls are displayed
for users to select the new data entries, then to insert the new data into the
SqlDataSource data table of the FormView control.Wednesday, April 27, 2016 8:06 PM
Answers
-
User1559292362 posted
Hi wonjartran,
an error popped up.
Cannot insert the value NULL into column 'ProductName', table 'Northwind.bdo.Products';...
According to your description, I create a demo and reproduce your on my side, as oned_gk said, I add selectedvalue property for the dropdownlist control, it works fine. and code as below for your reference.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormViewInsertTemplate.aspx.cs" Inherits="WingtipToys.FormView.FormViewInsertTemplate" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:formview id="EmployeeFormView" datasourceid="SqlDataSource1" allowpaging="True" datakeynames="ProductID" emptydatatext="No products found." runat="server"> <itemtemplate> ProductID: <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /> <br /> ProductName: <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' /> <br /> Description: <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>' /> <br /> ImagePath: <asp:Label ID="ImagePathLabel" runat="server" Text='<%# Bind("ImagePath") %>' /> <br /> UnitPrice: <asp:Label ID="UnitPriceLabel" runat="server" Text='<%# Bind("UnitPrice") %>' /> <br /> CategoryID: <asp:Label ID="CategoryIDLabel" runat="server" Text='<%# Bind("CategoryID") %>' /> <br /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </itemtemplate> <insertitemtemplate> ProductName: <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>' /> <br /> Description: <asp:DropDownList ID="DropDownList1" ClientIDMode="Static" SelectedValue='<%# Bind("Description")%>' runat="server" DataSourceID="SqlDataSource2" DataTextField="CategoryID" DataValueField="CategoryName" ></asp:DropDownList> <br /> ImagePath: <asp:TextBox ID="ImagePathTextBox" runat="server" Text='<%# Bind("ImagePath") %>' /> <br /> UnitPrice: <asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%# Bind("UnitPrice") %>' /> <br /> CategoryID: <asp:TextBox ID="CategoryIDTextBox" runat="server" Text='<%# Bind("CategoryID") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </insertitemtemplate> </asp:formview> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WINGTIPTOYS_61448e25c2a7489599c7bb76fb9aaadfConnectionString %>" InsertCommand="INSERT INTO [Products] ([ProductName], [Description], [ImagePath], [UnitPrice], [CategoryID]) VALUES (@ProductName, @Description, @ImagePath, @UnitPrice, @CategoryID)" SelectCommand="SELECT * FROM [Products]"> <InsertParameters> <asp:Parameter Name="ProductName" Type="String" /> <%-- <asp:ControlParameter ControlID="DropDownList1" DbType="String" Name="Description" />--%> <asp:Parameter Name="Description" Type="String" /> <asp:Parameter Name="ImagePath" Type="String" /> <asp:Parameter Name="UnitPrice" Type="Double" /> <asp:Parameter Name="CategoryID" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:WINGTIPTOYS_61448e25c2a7489599c7bb76fb9aaadfConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource> </div> </form> </body> </html>
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 9:24 AM
All replies
-
User-1716253493 posted
To save textboxt text value, the binding code is like this
Text = '<%# Bind("FieldName") %>'
If you want replace the textbox with dropdownlist, you can simply bind selectedvalue or selectedtext property of the dropdown
SelectedValue = '<%# Bind("FieldName") %>'
Thursday, April 28, 2016 12:29 AM -
User-830563764 posted
I think my problem is how to code to display a DDL inside
the source codes of the FormView control, so the user
can click the DDL arrow and select the desired value
from the DDL to insert into the SqlDataSource database table.Thursday, April 28, 2016 1:45 AM -
User-830563764 posted
In stead of <asp:Textbox ID=..>, I inserted <asp:DropDownList ID=...DataTextField=...> into the <InsertItemTemplate>ProductName:.
The Code worked fine, I can click 'New', then clicked to select DDL value. But after I clicked 'Insert', an error popped up.
Cannot insert the value NULL into column 'ProductName', table 'Northwind.bdo.Products';...
Apparently, the DDL SelectedValue='<%# Bind("ProductName") %> was not treated as a value @ProductName.
I inserted,
Thursday, April 28, 2016 8:45 PM -
User-830563764 posted
Is it possible that I need to change the <InsertParameters> of the SqlDataSource.
Change <asp:Parameter Name="ProductName" Type="String" to ?Thursday, April 28, 2016 9:30 PM -
User1559292362 posted
Hi wonjartran,
an error popped up.
Cannot insert the value NULL into column 'ProductName', table 'Northwind.bdo.Products';...
According to your description, I create a demo and reproduce your on my side, as oned_gk said, I add selectedvalue property for the dropdownlist control, it works fine. and code as below for your reference.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormViewInsertTemplate.aspx.cs" Inherits="WingtipToys.FormView.FormViewInsertTemplate" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:formview id="EmployeeFormView" datasourceid="SqlDataSource1" allowpaging="True" datakeynames="ProductID" emptydatatext="No products found." runat="server"> <itemtemplate> ProductID: <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /> <br /> ProductName: <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' /> <br /> Description: <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>' /> <br /> ImagePath: <asp:Label ID="ImagePathLabel" runat="server" Text='<%# Bind("ImagePath") %>' /> <br /> UnitPrice: <asp:Label ID="UnitPriceLabel" runat="server" Text='<%# Bind("UnitPrice") %>' /> <br /> CategoryID: <asp:Label ID="CategoryIDLabel" runat="server" Text='<%# Bind("CategoryID") %>' /> <br /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </itemtemplate> <insertitemtemplate> ProductName: <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>' /> <br /> Description: <asp:DropDownList ID="DropDownList1" ClientIDMode="Static" SelectedValue='<%# Bind("Description")%>' runat="server" DataSourceID="SqlDataSource2" DataTextField="CategoryID" DataValueField="CategoryName" ></asp:DropDownList> <br /> ImagePath: <asp:TextBox ID="ImagePathTextBox" runat="server" Text='<%# Bind("ImagePath") %>' /> <br /> UnitPrice: <asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%# Bind("UnitPrice") %>' /> <br /> CategoryID: <asp:TextBox ID="CategoryIDTextBox" runat="server" Text='<%# Bind("CategoryID") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </insertitemtemplate> </asp:formview> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WINGTIPTOYS_61448e25c2a7489599c7bb76fb9aaadfConnectionString %>" InsertCommand="INSERT INTO [Products] ([ProductName], [Description], [ImagePath], [UnitPrice], [CategoryID]) VALUES (@ProductName, @Description, @ImagePath, @UnitPrice, @CategoryID)" SelectCommand="SELECT * FROM [Products]"> <InsertParameters> <asp:Parameter Name="ProductName" Type="String" /> <%-- <asp:ControlParameter ControlID="DropDownList1" DbType="String" Name="Description" />--%> <asp:Parameter Name="Description" Type="String" /> <asp:Parameter Name="ImagePath" Type="String" /> <asp:Parameter Name="UnitPrice" Type="Double" /> <asp:Parameter Name="CategoryID" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:WINGTIPTOYS_61448e25c2a7489599c7bb76fb9aaadfConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource> </div> </form> </body> </html>
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 9:24 AM