DataList的cancel事件具有以下代码
protected void Cancel_Command(Object sender, DataListCommandEventArgs e)
{
ItemsList.EditItemIndex = -1;
BindList();
}
字符串
执行后,列表从第一个项目开始显示。如何将列表的当前项目移动到编辑的最后一个项目?
谢谢
评论后编辑Sharham
这个方法可以工作--谢谢,但是它会把我用于这个DataList的分页器弄得一团糟。
protected void BindList()
{
// Set the data source and bind to the DataList control.
GetSource();
//ItemsList.DataSource = CartView;
//SetPager();
ItemsList.DataSource = pager;
ItemsList.DataBind();
}
protected void GetSource()
{
ProductService productService = new ProductService();
pager = new PagedDataSource();
CartView = productService.GetAllProducts();
SetPager();
//CartView.Sort = "ProductName";
}
private void SetPager()
{
pager.AllowPaging = true;
pager.PageSize = pageSize;
pager.DataSource = CartView;
pager.CurrentPageIndex = this.CurrentPage;
this.next.Enabled = !pager.IsLastPage;
this.prev.Enabled = !pager.IsFirstPage;
}
型
设置datalist.SelectedIndex=e.Item.Itemindex;
后如何正确处理寻呼机
谢谢
更新
这是最新的代码工作。
我仍然需要将它与前面的代码进行比较,以确定问题到底出在哪里。
这个解决方案展示了如何使用DataList进行分页和编辑。“更新”功能还没有添加,但应该很简单。
Employee.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="DataListPager.Employee" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<asp:DataList ID="DataList1" runat="server" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand">
<HeaderTemplate>
<h3>Employees List</h3>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<font color="Red"><b>Employee ID</b></font>
</td>
<td>
<font color="Red"><b>LastName</b></font>
</td>
<td>
<font color="Red"><b>Age</b></font>
</td>
<td>Action</td>
</tr>
<tr>
<td>
<font color="Green"><%# Eval("EmployeeID") %></font>
</td>
<td>
<font color="Green"><%#Eval("LastName") %></font>
</td>
<td>
<font color="Green"><%#Eval("age") %></font>
</td>
<td>
<asp:Button ID="ButtonEdit" runat="server" Text="EDIT" CommandName="Edit" CausesValidation="true" />
</td>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<font color="Red"><b>Employee ID</b></font>
</td>
<td>
<font color="Red"><b>LastName</b></font>
</td>
<td>
<font color="Red"><b>Age</b></font>
</td>
</tr>
<tr>
<td>
<font color="Green"><%# Eval("EmployeeID") %></font>
</td>
<td>
<font color="Green"><%#Eval("LastName") %></font>
</td>
<td>
<asp:TextBox ID="txtAge" runat="server" style="margin-bottom: 0px" Text='<%# Bind("Age") %>'></asp:TextBox>
</td>
<td>
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="CANCEL" />
</td>
</tr>
</EditItemTemplate>
</asp:DataList>
<tr>
<td colspan="3">
<%--<asp:Button ID="btnshow" runat="server" Height="32px" Text="Show"
Font-Bold="true" Font-Size="12" ForeColor="DarkRed"
Width="97px" onclick="btnshow_Click" />--%>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button ID="btnnext" runat="server" Height="32px" Text="Next"
Font-Bold="true" Font-Size="12" ForeColor="DarkRed"
Width="97px" onclick="btnnext_Click" />
<asp:Button ID="btnprevious" runat="server" Height="32px" Text="Previous"
Font-Bold="true" Font-Size="12" ForeColor="DarkRed"
Width="97px" onclick="btnprevious_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
型
Employee.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using DataListPager.App_Code;
namespace DataListPager
{
public partial class Employee : System.Web.UI.Page
{
int position;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["vs"] = 0;
DataBind();
}
position = (int)ViewState["vs"];
}
PagedDataSource pds;
DataSet dset;
//protected void btnshow_Click(object sender, EventArgs e)
//{
// DataBind();
// btnnext.Visible = true;
// btnprevious.Visible = true;
//}
public void DataBind()
{
dset = new DataSet();
EmployeeDS employeeDS = new EmployeeDS();
dset = employeeDS.GetAllEmployees();
pds = new PagedDataSource();
//added
pds.AllowPaging = true;
pds.CurrentPageIndex = position;
pds.PageSize = 2;
///
pds.DataSource = dset.Tables[0].DefaultView;
DataList1.DataSource = pds;
DataList1.DataBind();
btnnext.Visible = !pds.IsLastPage;
btnprevious.Visible = !pds.IsFirstPage;
}
protected void btnprevious_Click(object sender, EventArgs e)
{
position = (int)ViewState["vs"];
position--;
ViewState["vs"] = position;
DataBind();
}
protected void btnnext_Click(object sender, EventArgs e)
{
position = (int)ViewState["vs"];
position++;
ViewState["vs"] = position;
DataBind();
//btnnext.Visible = !pds.IsLastPage;
//btnprevious.Visible = !pds.IsFirstPage;
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
DataBind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataBind();
}
}
}
型
EmployeeDS.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
namespace DataListPager.App_Code
{
public class EmployeeDS
{
private OleDbConnection conn;
public EmployeeDS()
{
this.conn = new OleDbConnection(Connect.getConnectionString());
}
public DataSet GetAllEmployees()
{
OleDbCommand objCmd = new OleDbCommand("EmployeeAll", conn);
objCmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
try
{
this.conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(objCmd);
da.Fill(ds);
}
catch (Exception e)
{
throw e;
}
finally
{
this.conn.Close();
}
return ds;
}
}
}
型
Connect.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DataListPager
{
public class Connect
{
const string FILE_NAME = "Test.accdb";
public static string getConnectionString()
{
// string location = HttpContext.Current.Server.MapPath("@../../App_Data/" + FILE_NAME);
string location = HttpContext.Current.Server.MapPath("~/App_Data/" + FILE_NAME);
// string location = HttpContext.Current.Server.MapPath(FILE_NAME);
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; data source=" + location; ;
return ConnectionString;
}
}
}
型
Employee表
able: Employee Page: 1
Columns
Name Type Size
EmployeeID Long Integer 4
LastName Short Text 255
FirstName Short Text 255
Age Long Integer 4
型
取消全部查询
uery: EmployeeAll Page: 1
SQL
SELECT Employee.EmployeeID, Employee.LastName, Employee.FirstName,
Employee.Age
型
1条答案
按热度按时间5n0oy7gb1#
对你的原始代码做了一些修改:
SqlDataSource
,因为每个主机都支持SQL数据库,并且在Web服务器上使用MS-Access没有意义。Customers
只是因为它有更多的记录,这使得分页结果更有意义。DataBind
重命名为Bind
,以避免与page_databind发生冲突。SelectedItemTemplate
和SelectedItemStyle
以进行视觉反馈。Update
添加到datalist。数据列表:
字符串
导航按钮:
型
SqlDataSource:
型
后面的代码:
型