ASP.NET 中的 DataList 取消编辑 - 如何返回到已编辑的项目

问题描述 投票:0回答:1

DataList的取消事件有以下代码

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;

谢谢

asp.net datalist
1个回答
0
投票

这是最新的有效代码。

我仍然需要将它与之前的代码进行比较,以查明到底是什么问题。

此解决方案展示了如何使用 DataList 进行分页和编辑。 “更新”功能尚未添加,但应该很简单。

员工.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>

员工.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;
      }
   }
}

连接.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;
      }
   }
}

员工表

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
© www.soinside.com 2019 - 2024. All rights reserved.