当前位置: 移动技术网 > IT编程>开发语言>.net > Datalist控件使用存储过程来分页实现代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
--------------前台:------------------- 复制代码 代码如下: <body> <form id="form1" runat
--------------前台:-------------------
复制代码 代码如下:

<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="datalist1" runat="server"
onitemcommand="datalist1_itemcommand"
oncancelcommand="datalist1_cancelcommand"
ondeletecommand="datalist1_deletecommand" oneditcommand="datalist1_editcommand"
onupdatecommand="datalist1_updatecommand"
onitemdatabound="datalist1_itemdatabound">
<edititemtemplate>
<table style="width: 100%; height: 180px;">
<tr>
<td class="style4">
商品名:</td>
<td class="style2">
<asp:textbox id="txtproductname" runat="server"
text='<%# eval("productname") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
规格:</td>
<td class="style2">
<asp:textbox id="txtproductstandard" runat="server"
text='<%# eval("productstandard") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
包装率:</td>
<td class="style2">
<asp:textbox id="txtpackagingratio" runat="server"
text='<%# eval("packagingratio") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
商品条码:</td>
<td class="style2">
<asp:textbox id="txtarticlenum" runat="server" text='<%# eval("articlenum") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
价格:</td>
<td class="style2">
<asp:textbox id="txtprice" runat="server" text='<%# eval("price") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
<asp:button id="btnupdate" runat="server" commandargument='<%# eval("pid") %>'
commandname="update" height="21px" text="更新" />
</td>
<td class="style2">
<asp:button id="btncancel" runat="server" commandname="cancel" text="取消" />
</td>
</tr>
</table>
</edititemtemplate>
<itemtemplate>
产品名:<asp:label id="label1" runat="server" text='<%# eval("productname") %>'></asp:label>
<br />
规格:<asp:label id="label2" runat="server" text='<%# eval("productstandard") %>'></asp:label>
<br />
包装率:<asp:label id="label3" runat="server" text='<%# eval("packagingratio") %>'></asp:label>
<br />
商品条码:<asp:label id="label4" runat="server" text='<%# eval("articlenum") %>'></asp:label>
<br />
超市价格:<asp:label id="label5" runat="server" text='<%# eval("price") %>'></asp:label>
<br />
<asp:button id="btnedit" runat="server" text="编辑" commandname="edit" />
 <asp:button id="btndelete" runat="server" text="删除"
commandargument='<%# eval("pid") %>' commandname="delete" />
<br />
<br />
<asp:button id="button1" runat="server" commandargument='<%# eval("pid") %>'
commandname="buy" text="放入购物车" />
<br />
</itemtemplate>
</asp:datalist>
<br />
<br />
<asp:button id="btnfirst" runat="server" onclick="btnfirst_click"
text="|<" />
 <asp:button id="btnprev" runat="server" onclick="btnprev_click" text="<"
style="height: 21px" />
 <asp:button id="btnnext" runat="server" onclick="btnnext_click" text=">" />
 <asp:button id="btnlast" runat="server" onclick="btnlast_click" text=">|" />
 <asp:label id="label1" runat="server"></asp:label>
<asp:textbox id="txtpagenumber" runat="server" height="26px" width="43px"></asp:textbox>
<asp:comparevalidator id="comparevalidator1" runat="server"
controltovalidate="txtpagenumber" display="dynamic" errormessage="必须为整数!"
forecolor="#ff3300" operator="datatypecheck" type="integer"></asp:comparevalidator>
<asp:rangevalidator id="rangevalidator1" runat="server"
controltovalidate="txtpagenumber" display="dynamic" errormessage="输入数据不合法!"
forecolor="red" maximumvalue="9" minimumvalue="1"></asp:rangevalidator>
<asp:button id="btngo" runat="server" onclick="btngo_click" text="go" />
<br />
<asp:hiddenfield id="hiddenfield1" runat="server" />
<asp:hiddenfield id="hiddenfield2" 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() + "页";
}

---------------------存储过程-----------------------
复制代码 代码如下:

create procedure [dbo].[sp_product_select_by_page_rownumber]
@pagesize int, --每页记录数量
@pagecount int output, --总页数
@pageindex int --当前页索引号
as
begin
declare @totalrecords int
select @totalrecords = count(pid) from product
if(@totalrecords % @pagesize = 0)
set @pagecount = @totalrecords / @pagesize;
else
set @pagecount = @totalrecords / @pagesize +1;
with temp as (select row_number() over (order by pid) as id,* from product)
select * from temp where id between (@pageindex -1)*@pagesize +1 and @pageindex * @pagesize
return @totalrecords
end
go

----------------web.config:-------------------
复制代码 代码如下:

<connectionstrings>
<add name="studentconnectionstring" connectionstring="data source=pc_think-think;initial catalog=student;persist security info=true;user id=sa;password=111111"
providername="system.data.sqlclient" />
</connectionstrings>

----------------------sqlhelper类:-------------------------------------
复制代码 代码如下:

public static string connstr = configurationmanager.connectionstrings["studentconnectionstring"].connectionstring;
public static int executenonquery(string sql, params sqlparameter[] pms)
{
using (sqlconnection con = new sqlconnection(connstr))
{
using (sqlcommand cmd = new sqlcommand(sql, con))
{
if (pms != null)
{
cmd.parameters.addrange(pms);
}
con.open();
return cmd.executenonquery();
}
}
}
public static datatable executedatatable(string sql, params sqlparameter[] pms)
{
datatable dt = new datatable();
sqldataadapter adapter = new sqldataadapter(sql,connstr);
if (pms != null)
{
adapter.selectcommand.parameters.addrange(pms);
}
adapter.fill(dt);
return dt;
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网