[研究]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
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="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"
DataTextField="FruitName" DataValueField="FruitName"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</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 中部分
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; } |
按下「編輯」按鈕後出現
芭樂雖然不再於下拉選單提供,但不會再出現下面錯誤。DropDownList1' 擁有的 SelectedValue 無效,因為它不在項目清單中。
別忘了GridView1_RowUpdating()要檢查按下「更新」按鈕後,新值是否屬於新名單中選項。
甚至按下「取消」觸發的GridView1_RowCancelingEdit()也進行檢查。
(完)
沒有留言:
張貼留言