如何在没有 Javascript 的情况下在单个 Repeater 中有多个 OnClick 事件?

How to have multiple OnClick events in a single Repeater without Javascript?

以下是我创建的存储过程:

CREATE PROCEDURE AdminViewProducts           
@PageIndex INTEGER = 1,  
@PageSize INTEGER = 10,  
@RecordCount INTEGER OUTPUT    
AS  
BEGIN  
SET NOCOUNT ON;  
SELECT ROW_NUMBER() 
OVER  
(  
 ORDER BY Product_Category_Name  
)AS RowNumber,  
    Product_No,
    Product_Image,
    Product_Name,
    Barcode_No,
    Product_Category_Name,
    Product_Price,
    Product_Quantity,
    Grocery_Branch_Name   
    INTO #Results 
    FROM Product,Product_Category,Grocery_Branch
    WHERE Product_Category.Product_Category_No = Product.Product_Category_No
    AND Grocery_Branch.Grocery_Branch_No = Product.Grocery_Branch_No

    SELECT * FROM #Results  
    WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 
    AND
    (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1  

    SELECT @RecordCount = COUNT(*) FROM #Results  

    DROP TABLE #Results  
END


CREATE PROCEDURE AdminSearchProduct
@ProductName VARCHAR(50),           
@PageIndex INTEGER = 1,  
@PageSize INTEGER = 10,  
@RecordCount INTEGER OUTPUT    
AS  
BEGIN  
SET NOCOUNT ON;  
SELECT ROW_NUMBER() OVER
(
 ORDER BY Product_Name
)
AS RowNumber,  
    Product_No,
    Product_Image,
    Product_Name,
    Barcode_No,
    Product_Category_Name,
    Product_Price,
    Product_Quantity,
    Grocery_Branch_Name   
    INTO #Results 
    FROM Product,Product_Category,Grocery_Branch
    WHERE Product_Category.Product_Category_No = Product.Product_Category_No
    AND Grocery_Branch.Grocery_Branch_No = Product.Grocery_Branch_No
    AND Product_Name LIKE '%' + @ProductName + '%'

    SELECT * FROM #Results  
    WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 
    AND
    (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1  

    SELECT @RecordCount = COUNT(*) FROM #Results  

    DROP TABLE #Results  
END

下面是我在 ASPX 中的代码

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="CSS/searchtext.css" rel="stylesheet" />
<link href="CSS/searchproducts.css" rel="stylesheet" />

<form id="form1" runat="server">

<main>
<div class="content-wrap">
<h4 class="searchtext">
<strong>Search by Product Name</strong>
</h4>

<div class="searchproducts">
<asp:Table ID="Table1" runat="server" align="center">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell HorizontalAlign="Right">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell HorizontalAlign="Left">
<asp:Button ID="Button8" runat="server" OnClick="Search_Product" ValidationGroup="SearchByName" Text="Search Product" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="SearchByName" ErrorMessage=" Please enter a product name" Font-Bold="True"></asp:RequiredFieldValidator>
<asp:Label ID="Label3" runat="server" Font-Bold="True"></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>

<br />
<br />

<div style="margin-left: 265px;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" HeaderStyle-BackColor="#00a400" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="Product_No" HeaderText="Product No" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="100px" Width="150px"
                                            ImageUrl='<%#"data:Image/png/jpg/jpeg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Product_Image")) %>' />
</ItemTemplate>
                                </asp:TemplateField>
<asp:BoundField DataField="Product_Name" HeaderText="Product" />
<asp:BoundField DataField="Barcode_No" HeaderText="Barcode" />
<asp:BoundField DataField="Product_Category_Name" HeaderText="Category" />
<asp:BoundField DataField="Product_Price" HeaderText="Price" DataFormatString="{0:0.00} AUD" />
<asp:BoundField DataField="Product_Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Grocery_Branch_Name" HeaderText="Branch" />
</Columns>
</asp:GridView>

<br />
<table style="align-content:center; width:800px;">
<tr>
<td>
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>' Enabled='<%# Eval("Enabled") %>' OnClick="lnkbtn_PageIndexChanged" ForeColor="#00a400"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>

</div>

</div>
</main>

</body>
</html>

下面是我的 C# 代码

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Grocery_Demo
{
 public partial class AdminViewProducts : System.Web.UI.Page
 {
  int PageSize = 10;

  protected void Page_Load(object sender, EventArgs e)
 {

  if (Session["UsernameAdmin"] != null && Session["PasswordAdmin"] != null && Session["BranchAdmin"] != null)
 {
  string username = Session["UsernameAdmin"].ToString();
  string password = Session["PasswordAdmin"].ToString();
  string branch = Session["BranchAdmin"].ToString();
  string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;
  SqlConnection con = new SqlConnection(CS);
  SqlCommand cmd = new SqlCommand("AdminValidation", con);
  cmd.CommandType = System.Data.CommandType.StoredProcedure;

  cmd.Parameters.AddWithValue("@AdministratorUsername", username);
  cmd.Parameters.AddWithValue("@AdministratorPassword", password);
  cmd.Parameters.AddWithValue("@GroceryBranchName", branch);
  con.Open();
  SqlDataReader read = cmd.ExecuteReader();
  read.Read();
  if (read.HasRows == false)
  {
   Response.Redirect("SignIn.aspx");
  }
    con.Close();
   }

   else
  {
   Response.Redirect("SignIn.aspx");
  }

   Label1.Text = Session["BranchAdmin"].ToString();
   Label2.Text = Session["UsernameAdmin"].ToString();
  {
    if (!Page.IsPostBack)
    DisplayProducts(1, PageSize);
   }

  }

 SqlDataAdapter da;
 DataSet ds = new DataSet();
 private void DisplayProducts(int PageIndex, int PageSize)
 {
  string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;          

 using (SqlConnection con = new SqlConnection(CS))
{
  using (SqlCommand cmd = new SqlCommand("AdminViewProducts", con))
 {   
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
   cmd.Parameters.AddWithValue("@PageSize", PageSize);
   cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
   cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
   da = new SqlDataAdapter(cmd);
   da.Fill(ds);
   con.Open();
   cmd.ExecuteNonQuery();

   if (ds.Tables[0].Rows.Count > 0)
   {
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
   }
  int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
                    this.PopulatePager(recordCount, PageIndex);
   }
   }


   }


private void PopulatePager(int recordCount, int currentPage)
{
 double dblPageCount = (double)((decimal)recordCount / (PageSize));
 int pageCount = (int)Math.Ceiling(dblPageCount);
 List<ListItem> pages = new List<ListItem>();
 if (pageCount > 0)
{
 pages.Add(new ListItem("FIRST >> ", "1", currentPage > 1));
 for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
                }
pages.Add(new ListItem(" << LAST", pageCount.ToString(), currentPage < pageCount));
}
rptPager.DataSource = pages;
rptPager.DataBind();
}

protected void lnkbtn_PageIndexChanged(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
DisplayProducts(pageIndex, PageSize);
}

protected void Search_Product(object sender, EventArgs e)
{
int PageIndex = 1;
string CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString;

using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("AdminSearchProduct", con))
{

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text + "%");
cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();

if (ds.Tables[0].Rows.Count > 0)
{
 GridView1.DataSource = ds.Tables[0];
 GridView1.DataBind();
}

else
{
 Label3.Text = " Couldn't find your product";
}

int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, PageIndex);
}
}
}

}
}

所有SQL存储过程、C#文件和ASPX文件编译成功。 此外,DisplayProducts() 方法能够成功调用存储过程 "AdminViewProducts" 并且我得到了我想要的输出,如下所示:

FIRST >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 << LAST

因此,DisplayProducts() 方法没有问题。

另一方面,Search_Product () 方法调用存储过程 "AdminSearchProduct" 成功,我得到了我想要的结果,如下所示:

FIRST >> 1 2 3 4 5 6 7 8 9 << LAST

但是,Search_Product() 方法中存在一个小问题。 问题是,如果我单击任何随机页码(link 按钮),例如 2 OR 6 OR 7 ,我将得到一个基于 DisplayProducts() 方法的显示,如下所示:

FIRST >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 << LAST

但是,我想在单击任何随机页码(link 按钮)后获得此显示,例如 2 或 6 或 7,如下所示:

FIRST >> 1 2 3 4 5 6 7 8 9 << LAST

这就是我面临的问题。 换句话说,SearchProduct() 方法中存在需要修复的小问题。

虽然我确实意识到了这个问题。 如果我回到我的 aspx 代码,Repeater 中只有一个 OnClickEvent。 如果我返回我的 C# 代码,protected void lnkbtn_PageIndexChanged 仅调用 DisplayProducts()。 但是,我尝试在 protected void lnkbtn_PageIndexChanged 下添加另一行来调用 SearchProduct(),但我仍然无法解决问题。

我想做的是在单个 Repeater 中有 2 个 OnClickEvent,以便在不使用任何 Javascript.

的情况下解决这个问题

如果能识别出我在 C# 代码和 ASPX 代码中的错误并相应地提供适当的解决方案,那将非常有帮助。

如果我对你的问题理解正确,你需要创建一个分页逻辑来呈现结果。

那么您在控制器中的方法应该将页码作为参数,您可以使用它从数据库中查询结果。

Fx 如果您点击 2,您的查询应该跳过 (2-1) * 查询大小并采用查询大小。