当我使用 Listview 控件插入或更新数据时,我想使用 sql 数据源重新填充下拉列表的数据。有没有一种方法可以做到这一点而无需手动刷新我的页面?
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" AutoPostBack="true" DataTextField="type" DataValueField="type" Width="66px">
</asp:DropDownList>
<asp:SqlDataSource runat="server" OnInserted="SqlDataSource1_Inserted" ID="SqlDataSource1" InsertCommand="INSERT INTO [permit] ([type]) VALUES (@type, @description, @Priceday, @Priceweek, @Pricemonth, @Pricesem, @Priceyear)" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [permit]" DeleteCommand="DELETE FROM [permit] WHERE [pid] = @pid" UpdateCommand="UPDATE [permit] SET [type] = @type, [description] = @description, [Priceday] = @Priceday, [Priceweek] = @Priceweek, [Pricemonth] = @Pricemonth, [Pricesem] = @Pricesem, [Priceyear] = @Priceyear WHERE [pid] = @pid">
<DeleteParameters>
<asp:Parameter Name="pid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="type" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="type" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:ListView ID="ListView1" runat="server" DataKeyNames="pid" DataSourceID="SqlDataSource2" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<span style="">pid:
<asp:Label ID="pidLabel" runat="server" Text='<%# Eval("pid") %>' />
<br />
type:
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("type") %>' />
<br />
description:
<asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
<br />
Priceday:
<asp:Label ID="PricedayLabel" runat="server" Text='<%# Eval("Priceday") %>' />
<br />
Priceweek:
<asp:Label ID="PriceweekLabel" runat="server" Text='<%# Eval("Priceweek") %>' />
<br />
Pricemonth:
<asp:Label ID="PricemonthLabel" runat="server" Text='<%# Eval("Pricemonth") %>' />
<br />
Pricesem:
<asp:Label ID="PricesemLabel" runat="server" Text='<%# Eval("Pricesem") %>' />
<br />
Priceyear:
<asp:Label ID="PriceyearLabel" runat="server" Text='<%# Eval("Priceyear") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<br />
<br />
</span>
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">pid:
<asp:Label ID="pidLabel1" runat="server" Text='<%# Eval("pid") %>' />
<br />
type:
<asp:TextBox ID="typeTextBox" runat="server" Text='<%# Bind("type") %>' />
<br />
description:
<asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' />
<br />
Priceday:
<asp:TextBox ID="PricedayTextBox" runat="server" Text='<%# Bind("Priceday") %>' />
<br />
Priceweek:
<asp:TextBox ID="PriceweekTextBox" runat="server" Text='<%# Bind("Priceweek") %>' />
<br />
Pricemonth:
<asp:TextBox ID="PricemonthTextBox" runat="server" Text='<%# Bind("Pricemonth") %>' />
<br />
Pricesem:
<asp:TextBox ID="PricesemTextBox" runat="server" Text='<%# Bind("Pricesem") %>' />
<br />
Priceyear:
<asp:TextBox ID="PriceyearTextBox" runat="server" Text='<%# Bind("Priceyear") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
<br />
<br />
</span>
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<span style="">type:
<asp:TextBox ID="typeTextBox" runat="server" Text='<%# Bind("type") %>' />
<br />
description:
<asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' />
<br />
Priceday:
<asp:TextBox ID="PricedayTextBox" runat="server" Text='<%# Bind("Priceday") %>' />
<br />
Priceweek:
<asp:TextBox ID="PriceweekTextBox" runat="server" Text='<%# Bind("Priceweek") %>' />
<br />
Pricemonth:
<asp:TextBox ID="PricemonthTextBox" runat="server" Text='<%# Bind("Pricemonth") %>' />
<br />
Pricesem:
<asp:TextBox ID="PricesemTextBox" runat="server" Text='<%# Bind("Pricesem") %>' />
<br />
Priceyear:
<asp:TextBox ID="PriceyearTextBox" runat="server" Text='<%# Bind("Priceyear") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br />
<br />
</span>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="InsertButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</InsertItemTemplate>
<ItemTemplate>
<span style="">pid:
<asp:Label ID="pidLabel" runat="server" Text='<%# Eval("pid") %>' />
<br />
type:
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("type") %>' />
<br />
description:
<asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
<br />
Priceday:
<asp:Label ID="PricedayLabel" runat="server" Text='<%# Eval("Priceday") %>' />
<br />
Priceweek:
<asp:Label ID="PriceweekLabel" runat="server" Text='<%# Eval("Priceweek") %>' />
<br />
Pricemonth:
<asp:Label ID="PricemonthLabel" runat="server" Text='<%# Eval("Pricemonth") %>' />
<br />
Pricesem:
<asp:Label ID="PricesemLabel" runat="server" Text='<%# Eval("Pricesem") %>' />
<br />
Priceyear:
<asp:Label ID="PriceyearLabel" runat="server" Text='<%# Eval("Priceyear") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">pid:
<asp:Label ID="pidLabel" runat="server" Text='<%# Eval("pid") %>' />
<br />
type:
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("type") %>' />
<br />
description:
<asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
<br />
Priceday:
<asp:Label ID="PricedayLabel" runat="server" Text='<%# Eval("Priceday") %>' />
<br />
Priceweek:
<asp:Label ID="PriceweekLabel" runat="server" Text='<%# Eval("Priceweek") %>' />
<br />
Pricemonth:
<asp:Label ID="PricemonthLabel" runat="server" Text='<%# Eval("Pricemonth") %>' />
<br />
Pricesem:
<asp:Label ID="PricesemLabel" runat="server" Text='<%# Eval("Pricesem") %>' />
<br />
Priceyear:
<asp:Label ID="PriceyearLabel" runat="server" Text='<%# Eval("Priceyear") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<br />
<br />
</span>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [permit] WHERE [pid] = @original_pid AND [type] = @original_type AND [description] = @original_description AND [Priceday] = @original_Priceday AND [Priceweek] = @original_Priceweek AND [Pricemonth] = @original_Pricemonth AND [Pricesem] = @original_Pricesem AND [Priceyear] = @original_Priceyear" InsertCommand="INSERT INTO [permit] ([type], [description], [Priceday], [Priceweek], [Pricemonth], [Pricesem], [Priceyear]) VALUES (@type, @description, @Priceday, @Priceweek, @Pricemonth, @Pricesem, @Priceyear)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [permit] WHERE ([type] = @type)" UpdateCommand="UPDATE [permit] SET [type] = @type, [description] = @description, [Priceday] = @Priceday, [Priceweek] = @Priceweek, [Pricemonth] = @Pricemonth, [Pricesem] = @Pricesem, [Priceyear] = @Priceyear WHERE [pid] = @original_pid AND [type] = @original_type AND [description] = @original_description AND [Priceday] = @original_Priceday AND [Priceweek] = @original_Priceweek AND [Pricemonth] = @original_Pricemonth AND [Pricesem] = @original_Pricesem AND [Priceyear] = @original_Priceyear">
<DeleteParameters>
<asp:Parameter Name="original_pid" Type="Int32" />
<asp:Parameter Name="original_type" Type="String" />
<asp:Parameter Name="original_description" Type="String" />
<asp:Parameter Name="original_Priceday" Type="Decimal" />
<asp:Parameter Name="original_Priceweek" Type="Decimal" />
<asp:Parameter Name="original_Pricemonth" Type="Decimal" />
<asp:Parameter Name="original_Pricesem" Type="Decimal" />
<asp:Parameter Name="original_Priceyear" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="Priceday" Type="Decimal" />
<asp:Parameter Name="Priceweek" Type="Decimal" />
<asp:Parameter Name="Pricemonth" Type="Decimal" />
<asp:Parameter Name="Pricesem" Type="Decimal" />
<asp:Parameter Name="Priceyear" Type="Decimal" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="type" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="Priceday" Type="Decimal" />
<asp:Parameter Name="Priceweek" Type="Decimal" />
<asp:Parameter Name="Pricemonth" Type="Decimal" />
<asp:Parameter Name="Pricesem" Type="Decimal" />
<asp:Parameter Name="Priceyear" Type="Decimal" />
<asp:Parameter Name="original_pid" Type="Int32" />
<asp:Parameter Name="original_type" Type="String" />
<asp:Parameter Name="original_description" Type="String" />
<asp:Parameter Name="original_Priceday" Type="Decimal" />
<asp:Parameter Name="original_Priceweek" Type="Decimal" />
<asp:Parameter Name="original_Pricemonth" Type="Decimal" />
<asp:Parameter Name="original_Pricesem" Type="Decimal" />
<asp:Parameter Name="original_Priceyear" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ListView1" />
代码隐藏
public partial class Default3 : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e) {
}
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
DropDownList1.Items.Clear();
//DropDownList1.Items.Dispose();
DropDownList1.DataSourceID = "SqlDataSource1";
DropDownList1.DataTextField = "type";
DropDownList1.DataValueField = "type";
DropDownList1.DataBind();
}
}