我访问了各个网站并找到了很多答案,但似乎没有一个能解决我的错误。 我想保持复选框的状态,以便在返回第1页之前检查复选框的数量在转到第2页之前保持不变。我的代码如下: Default.aspx文件是:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DatabaseAndDataSourceControls._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>ASP.NET DatabaseAndDataSourceControls Demo<br />
</h1>
</div>
<div class="row">
<div class="col-md-4">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringNorthwnd %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country], [Phone] FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [City], [Country], [Phone]) VALUES (@CustomerID, @CompanyName, @ContactName, @City, @Country, @Phone)" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [City] = @City, [Country] = @Country, [Phone] = @Phone WHERE [CustomerID] = @CustomerID">
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="CustomerID" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringNorthwnd %>" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)" SelectCommand="SELECT * FROM [Customers] WHERE ([CustomerID] = @CustomerID)" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [CustomerID] = @CustomerID">
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="ContactTitle" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="CustomerID" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="ContactTitle" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="CustomerID" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<h2>Manage Customers
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Save" runat="server" Font-Size="Medium" Height="25px" Text="Save" Width="80px" OnClick="Save_Click" />
<asp:Button ID="Check" runat="server" Font-Size="Medium" Height="25px" Text="Check" Width="80px" OnClick="Check_Click" />
</h2>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="CheckBox">
<ItemTemplate>
<asp:CheckBox ID="EmpSelector" runat="server" Text="EmpSelector"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:TemplateField HeaderText="View Details">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandArgument= '<%# Eval("CustomerID") %>' CommandName="ViewDetails" Text="View Details" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource2" ForeColor="Black" GridLines="Vertical" HeaderText="Customer Details" Height="50px" Width="125px">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#CCCC99" />
<FooterTemplate>
No customer is currently selected.<br />
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="New">New</asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
</asp:DetailsView>
<br />
</div>
</div>
</asp:Content>
CS文件是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace DatabaseAndDataSourceControls
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_ItemInserted(object sender,
DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
savechkdvls();
GridView1.PageIndex = e.NewPageIndex;
chkdvaluesp();
GridView1.DataBind();
}
private void chkdvaluesp()
{
ArrayList usercontent = (ArrayList)Session["chkditems"];
//String index = "";
if (usercontent != null && usercontent.Count > 0)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
String index = (GridView1.DataKeys[gvrow.RowIndex].Value).ToString();
if (usercontent.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("EmpSelector");
myCheckBox.Checked = true;
Label1.Text += index + " is checked </br></br>";
}
}
}
}
/////////////////////////
private void savechkdvls()
{
ArrayList usercontent = new ArrayList();
String index1 = "";
foreach (GridViewRow gvrow in GridView1.Rows)
{
index1 = (GridView1.DataKeys[gvrow.RowIndex].Value).ToString();
bool result = ((CheckBox)gvrow.FindControl("EmpSelector")).Checked;
// Check in the Session
if (Session["chkditems"] != null)
usercontent = (ArrayList)Session["chkditems"];
if (result)
{
if (!usercontent.Contains(index1))
usercontent.Add(index1);
}
else
usercontent.Remove(index1);
}
if (usercontent != null && usercontent.Count > 0)
Session["chkditems"] = usercontent;
}
protected void Save_Click(object sender, EventArgs e)
{
savechkdvls();
}
protected void Check_Click(object sender, EventArgs e)
{
chkdvaluesp();
}
}
}
我添加了两个按钮Save and Check。因此,当我将页面从2移动到1后单击它们。复选框会更新。但是没有Save_click和Check_Click事件,复选框不会更新。请帮忙。