2022年3月19日 星期六

[研究]DropDownList下拉選單的恩怨情仇(四)項目不存在的預選

 [研究]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

之前下拉選單一定只出現第一項,改進一下;

若已存資料庫中項目不再存在,希望「編輯」畫面下拉選單預選空白;

若已存資料庫中項目存在,希望「編輯」畫面下拉選單預選該項目。

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" 
                OnRowDataBound="GridView1_RowDataBound" 
                >
                <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="TextBox_FruitBuy" runat="server" ReadOnly="true"
                                Text='<%# Bind("FruitBuy") %>'></asp:TextBox>
                            <asp:SqlDataSource ID="SqlDataSource_FruitName" runat="server"
                                ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
                                SelectCommand="SELECT * FROM [FruitList]"></asp:SqlDataSource>
                            <asp:DropDownList ID="DropDownList_FruitBuy" runat="server"
                                AutoPostBack="true"
                                DataSourceID="SqlDataSource_FruitName"
                                 AppendDataBoundItems="true"
                                DataTextField="FruitName" DataValueField="FruitName" 
                                OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                <asp:ListItem></asp:ListItem>
                            </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>

Default.aspx.cs 中部分


using System;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var row = GridView1.Rows[GridView1.EditIndex];
            TextBox TextBox_FruitBuy = (TextBox)row.FindControl("TextBox_FruitBuy");
            DropDownList DropDownList_FruitBuy = (DropDownList)row.FindControl("DropDownList_FruitBuy");
            TextBox_FruitBuy.Text = DropDownList_FruitBuy.SelectedValue;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow 
                && e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))
            {
                if (e.Row.RowIndex == GridView1.EditIndex)
                {
                    TextBox TextBox_FruitBuy = (TextBox)e.Row.FindControl("TextBox_FruitBuy");
                    DropDownList DropDownList_FruitBuy = (DropDownList)e.Row.FindControl("DropDownList_FruitBuy");
                    if (DropDownList_FruitBuy.Items.Contains(new ListItem(TextBox_FruitBuy.Text)))
                    {
                        DropDownList_FruitBuy.SelectedValue = TextBox_FruitBuy.Text;
                    }
                    else
                    {
                        DropDownList_FruitBuy.SelectedValue = "";
                    }
                }
            }
        }
    }
}

按下「編輯」按鈕後出現

(下圖)芭樂不在下拉選單中,下拉選單預選空白


(下圖)香蕉在下拉選單中,下拉選單預選香蕉
這種情況下,可以設定 TextBox_FruitBuy 為Visible = "false" 隱藏掉 (因為有的需求方不想看到兩個控制項 (ASP.NET Control),或不想在該處看到 TextBox )

別忘了GridView1_RowUpdating()要檢查按下「更新」按鈕後,新值是否屬於新名單中選項。
甚至按下「取消」觸發的GridView1_RowCancelingEdit()也進行檢查。但也或許可以不用,看該欄位是否允許不選擇,也就是輸入空字串。

敝人遇過某種下拉選單,允許不選,選項有數千項,隨時增、減、改名,非敝人管理,也不會通知敝人,就合適這種情況。

(完)

沒有留言:

張貼留言