Datalist控件使用存储过程来分页实现代码

--------------前台:-------------------

复制代码 代码如下:


<body>
<form runat="server">
<div>
<asp:DataList runat="server"
onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand"
onitemdatabound="DataList1_ItemDataBound">
<EditItemTemplate>
<table>
<tr>
<td>
商品名:</td>
<td>
<asp:TextBox runat="server"
Text='<%# Eval("ProductName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
规格:</td>
<td>
<asp:TextBox runat="server"
Text='<%# Eval("ProductStandard") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
包装率:</td>
<td>
<asp:TextBox runat="server"
Text='<%# Eval("PackagingRatio") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
商品条码:</td>
<td>
<asp:TextBox runat="server" Text='<%# Eval("ArticleNum") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
价格:</td>
<td>
<asp:TextBox runat="server" Text='<%# Eval("Price") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="update" Text="更新" />
</td>
<td>
<asp:Button runat="server" CommandName="cancel" Text="取消" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
产品名:<asp:Label runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
<br />
规格:<asp:Label runat="server" Text='<%# Eval("ProductStandard") %>'></asp:Label>
<br />
包装率:<asp:Label runat="server" Text='<%# Eval("PackagingRatio") %>'></asp:Label>
<br />
商品条码:<asp:Label runat="server" Text='<%# Eval("ArticleNum") %>'></asp:Label>
<br />
超市价格:<asp:Label runat="server" Text='<%# Eval("Price") %>'></asp:Label>
<br />
<asp:Button runat="server" Text="编辑" CommandName="Edit" />
&nbsp;<asp:Button runat="server" Text="删除"
CommandArgument='<%# Eval("PId") %>' CommandName="delete" />
<br />
<br />
<asp:Button runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="Buy" Text="放入购物车" />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<br />
<asp:Button runat="server"
Text="|&lt;" />
&nbsp;<asp:Button runat="server" Text="&lt;"
style="height: 21px" />
&nbsp;<asp:Button runat="server" Text="&gt;" />
&nbsp;<asp:Button runat="server" Text="&gt;|" />
&nbsp;<asp:Label runat="server"></asp:Label>
<asp:TextBox runat="server"></asp:TextBox>
<asp:CompareValidator runat="server"
ControlToValidate="txtPageNumber" Display="Dynamic" ErrorMessage="必须为整数!"
ForeColor="#FF3300" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
<asp:RangeValidator runat="server"
ControlToValidate="txtPageNumber" Display="Dynamic" ErrorMessage="输入数据不合法!"
ForeColor="Red" MaximumValue="9" MinimumValue="1"></asp:RangeValidator>
<asp:Button runat="server" Text="Go" />
<br />
<asp:HiddenField runat="server" />
<asp:HiddenField runat="server" />
</div>
</form>
</body>


---------------------后台:---------------------------

复制代码 代码如下:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindProduct(1);
}
}
private void bindProduct(int pageIndex)
{
string constr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Product_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.Add("@pageCount", System.Data.DbType.Int32).Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@pageIndex", pageIndex);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
int pageCount = Convert.ToInt32(cmd.Parameters["@pageCount"].Value);
this.HiddenField1.Value = pageCount.ToString();
this.HiddenField2.Value = pageIndex.ToString();
}
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Buy")
{
Response.Write(e.CommandArgument.ToString());
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.bindProduct(1);
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string ProName = (e.Item.FindControl("txtProductName") as TextBox).Text;
string ProStandarde = (e.Item.FindControl("txtProductStandard") as TextBox).Text;
string ProPackaging = (e.Item.FindControl("txtPackagingRatio") as TextBox).Text;
string ProArtialeNum = (e.Item.FindControl("txtArticleNum") as TextBox).Text;
string ProPrice = (e.Item.FindControl("txtPrice") as TextBox).Text;
string sql = "update Product set ProductName=@ProductName,ProductStandard=@ProductStandard,PackagingRatio=@PackagingRatio,ArticleNum=@ArticleNum,Price=@Price where PId=@pid";
SqlParameter[] pms = new SqlParameter[]{
new SqlParameter("@ProductName",ProName),
new SqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
new SqlParameter("@ArticleNum",ProArtialeNum),
new SqlParameter("@Price",ProPrice),
new SqlParameter("@pid",e.CommandArgument)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
this.bindProduct(1);
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string sql = "delete from Product where PId=@pid";
SqlParameter pms = new SqlParameter("@pid", e.CommandArgument);
SQLHelper.ExecuteNonQuery(sql, pms);
this.bindProduct(1);
}
protected void btnFirst_Click(object sender, EventArgs e)
{
this.bindProduct(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
if (index > 1)
{
index--;
this.bindProduct(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
int pageCount = Convert.ToInt32(this.HiddenField1.Value);
if (index<pageCount)
{
index++;
this.bindProduct(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
this.bindProduct(Convert.ToInt32(this.HiddenField1.Value));
}
protected void btnGo_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(txtPageNumber.Text) <= Convert.ToInt32(HiddenField1.Value))
{
this.bindProduct(Convert.ToInt32(txtPageNumber.Text));
}
else
{
Response.Write("您输入的页数超出了总页数,如有需要请重新输入!");
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Label1.Text = "第" + (HiddenField2.Value).ToString() + "页,共" + HiddenField1.Value.ToString() + "页";
}


---------------------存储过程-----------------------

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdxzw.html