[研究]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
[研究]DropDownList下拉選單的恩怨情仇(二)使用料庫儲存選項清單http://shaurong.blogspot.com/2022/03/dropdownlist.html
[研究]DropDownList下拉選單的恩怨情仇(三)已儲存項目不再提供於資料庫選項名單
http://shaurong.blogspot.com/2022/03/dropdownlist_19.html
[研究]DropDownList下拉選單的恩怨情仇(四)項目不存在的預選https://shaurong.blogspot.com/2022/03/dropdownlist_15.html
(下圖)新版分類選項
(下圖)舊版購買結果歷史紀錄 (芭樂、蘿蔔已經不再目前上面選項中)
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="WebApplication3.Default2" %> <!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> <asp:SqlDataSource ID="SqlDataSource_FruitVegeType" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT N'' AS FruitVegeType UNION SELECT DISTINCT FruitVegeType FROM [FruitVegeList]"></asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT * FROM [FruitVegeRecord] WHERE ([sn] = @sn)" UpdateCommand="UPDATE [FruitVegeRecord] SET [FVType] = @FVType, [FruitBuy] = @FruitBuy WHERE [sn] = @sn"> <SelectParameters> <asp:QueryStringParameter Name="sn" QueryStringField="sn" Type="Int32" DefaultValue="1" /> </SelectParameters> <UpdateParameters> <asp:Parameter Name="FVType" Type="String" /> <asp:Parameter Name="FruitBuy" Type="String" /> <asp:Parameter Name="sn" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <form id="form1" runat="server"> <div> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" DefaultMode="Edit" Caption="編輯" AutoGenerateRows="False" DataKeyNames="sn" DataSourceID="SqlDataSource1"> <Fields> <asp:BoundField DataField="sn" HeaderText="sn" InsertVisible="False" ReadOnly="True" SortExpression="sn" /> <asp:TemplateField HeaderText="FVType" SortExpression="FVType"> <EditItemTemplate> <asp:TextBox ID="TextBox_FVType" runat="server" Text='<%# Bind("FVType") %>' Visible="false"></asp:TextBox> <asp:DropDownList ID="DropDownList_FruitVegeType" runat="server" AutoPostBack="true" DataTextField="FruitVegeType" DataValueField="FruitVegeType" DataSourceID="SqlDataSource_FruitVegeType" OnSelectedIndexChanged="DropDownList_FruitVegeType_SelectedIndexChanged"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource_FruitVegeName" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT N'' AS FruitVegeName UNION (SELECT FruitVegeName FROM [FruitVegeList] WHERE ([FruitVegeType] = @FruitVegeType))"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList_FruitVegeType" Name="FruitVegeType" ConvertEmptyStringToNull="false" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FruitBuy" SortExpression="FruitBuy"> <EditItemTemplate> <asp:TextBox ID="TextBox_FruitBuy" runat="server" Text='<%# Bind("FruitBuy") %>' Visible="false"></asp:TextBox> <asp:DropDownList ID="DropDownList_FruitVegeName" runat="server" DataSourceID = "SqlDataSource_FruitVegeName" AutoPostBack="true" DataTextField="FruitVegeName" DataValueField="FruitVegeName" OnSelectedIndexChanged="DropDownList_FruitVegeName_SelectedIndexChanged"> <asp:ListItem></asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Fields> </asp:DetailsView> </div> </form> </body> </html> |
Default2.aspx.cs 中部分
using System; using System.Web.UI.WebControls; namespace WebApplication3 { public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DropDownList_FruitVegeType_SelectedIndexChanged(object sender, EventArgs e) { TextBox TextBox_FVType = (TextBox)DetailsView1.FindControl("TextBox_FVType"); DropDownList DropDownList_FruitVegeType = (DropDownList)DetailsView1.FindControl("DropDownList_FruitVegeType"); TextBox_FVType.Text = DropDownList_FruitVegeType.SelectedValue; } protected void DropDownList_FruitVegeName_SelectedIndexChanged(object sender, EventArgs e) { TextBox TextBox_FruitBuy = (TextBox)DetailsView1.FindControl("TextBox_FruitBuy"); DropDownList DropDownList_FruitVegeName = (DropDownList)DetailsView1.FindControl("DropDownList_FruitVegeName"); TextBox_FruitBuy.Text = DropDownList_FruitVegeName.SelectedValue; } } } |
按下「編輯」按鈕後出現
(完)
沒有留言:
張貼留言