[研究]DropDownList下拉選單的恩怨情仇(二)使用料庫儲存選項清單
2022-3-19
環境:Visual Studio 2022 + ASP.NET + Web Application + WebForm + C#
續這篇,之前下拉選項寫死程式中,若項目多,或需要該下拉選單的地方多,可把清單放存放於資料庫中,免得增減項目要到處改程式。
[研究]DropDownList下拉選單的恩怨情仇(一)'DropDownList1' 擁有的 SelectedValue 無效,因為它不在項目清單中。
https://shaurong.blogspot.com/2022/03/dropdownlistdropdownlist1-selectedvalue.html
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" DeleteCommand="DELETE FROM [FruitRecord] WHERE [sn] = @sn" InsertCommand="INSERT INTO [FruitRecord] ([FruitBuy]) VALUES (@FruitBuy)" SelectCommand="SELECT * FROM [FruitRecord]" UpdateCommand="UPDATE [FruitRecord] SET [FruitBuy] = @FruitBuy WHERE [sn] = @sn"> <DeleteParameters> <asp:Parameter Name="sn" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="FruitBuy" DefaultValue="" ConvertEmptyStringToNull="false" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="FruitBuy" DefaultValue="" ConvertEmptyStringToNull="false" Type="String" /> <asp:Parameter Name="sn" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="sn" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:BoundField DataField="sn" HeaderText="sn" InsertVisible="False" ReadOnly="True" SortExpression="sn" /> <asp:TemplateField HeaderText="FruitBuy" SortExpression="FruitBuy"> <EditItemTemplate> <%--<asp:TextBox ID="TextBox1" runat="server" Text= <%# Bind("FruitBuy") %>'></asp:TextBox>--%> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT * FROM [FruitList]"></asp:SqlDataSource> <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("FruitBuy") %>' DataSourceID="SqlDataSource2" DataTextField="FruitName" DataValueField="FruitName"> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("FruitBuy") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html> |
按下「編輯」按鈕後出現
(完)
沒有留言:
張貼留言