DataList controls use stored procedures to pagination implement code _ Practical tips

Source: Internet
Author: User
--------------Front Desk:-------------------
Copy Code code as follows:

<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>
&LT;TD class= "Style4" >
Product Name:</td>
&LT;TD class= "Style2" >
<asp:textbox id= "Txtproductname" runat= "Server"
Text= ' <%# Eval ("ProductName")%> ' ></asp:TextBox>
</td>
</tr>
<tr>
&LT;TD class= "Style4" >
Specification:</td>
&LT;TD class= "Style2" >
<asp:textbox id= "Txtproductstandard" runat= "Server"
Text= ' <%# Eval ("Productstandard")%> ' ></asp:TextBox>
</td>
</tr>
<tr>
&LT;TD class= "Style4" >
Packing rate:</td>
&LT;TD class= "Style2" >
<asp:textbox id= "Txtpackagingratio" runat= "Server"
Text= ' <%# Eval ("Packagingratio")%> ' ></asp:TextBox>
</td>
</tr>
<tr>
&LT;TD class= "Style4" >
Commodity Barcode:</td>
&LT;TD class= "Style2" >
<asp:textbox id= "Txtarticlenum" runat= "server" text= ' <%# Eval ("Articlenum")%> ' ></asp:TextBox>
</td>
</tr>
<tr>
&LT;TD class= "Style4" >
Price:</td>
&LT;TD class= "Style2" >
<asp:textbox id= "Txtprice" runat= "server" text= ' <%# Eval ("price")%> ' ></asp:TextBox>
</td>
</tr>
<tr>
&LT;TD class= "Style4" >
<asp:button id= "btnupdate" runat= "server" commandargument= ' <%# Eval ("PId")%> '
Commandname= "Update" height= "21px" text= "Updates"/>
</td>
&LT;TD class= "Style2" >
<asp:button id= "Btncancel" runat= "Server" commandname= "Cancel" text= "Cancel"/>
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
Product Name: <asp:label id= "Label1" runat= "server" text= ' <%# Eval ("ProductName")%> ' ></asp:Label>
<br/>
Spec.: <asp:label id= "Label2" runat= "server" text= ' <%# Eval ("Productstandard")%> ' ></asp:Label>
<br/>
Packing rate: <asp:label id= "Label3" runat= "server" text= ' <%# Eval ("Packagingratio")%> ' ></asp:Label>
<br/>
Product Barcode: <asp:label id= "Label4" runat= "server" text= ' <%# Eval ("Articlenum")%> ' ></asp:Label>
<br/>
Supermarket prices: <asp:label id= "Label5" runat= "server" text= ' <%# Eval ("price")%> ' ></asp:Label>
<br/>
<asp:button id= "Btnedit" runat= "Server" text= "edit" commandname= "editing"/>
<asp:button id= "Btndelete" runat= "server" text= "Delete"
Commandargument= ' <%# Eval ("PId")%> ' commandname= ' delete '/>
<br/>
<br/>
<asp:button id= "Button1" runat= "server" commandargument= ' <%# Eval ("PId")%> '
Commandname= "buy" text= "put in the shopping cart"/>
<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= must be an integer! "
Forecolor= "#FF3300" operator= "DataTypeCheck" type= "Integer" ></asp:CompareValidator>
<asp:rangevalidator id= "RangeValidator1" runat= "Server"
Controltovalidate= "Txtpagenumber" display= "Dynamic" errormessage= "input data is illegal!" "
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>

---------------------Background:---------------------------
Copy Code code as follows:

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 (this. Convert.ToInt32 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 ("You have entered more than the total number of pages, if necessary, please re-enter!") ");
}
}
protected void Datalist1_itemdatabound (object sender, DataListItemEventArgs e)
{
Label1.Text = "The first" + (Hiddenfield2.value). ToString () + "page, total" + HiddenField1.Value.ToString () + "page";
}

---------------------Stored Procedures-----------------------
Copy Code code as follows:

CREATE PROCEDURE [dbo]. [Sp_product_select_by_page_rownumber]
@pageSize int,--Number of records per page
@pageCount int output,--Total pages
@pageIndex INT--current page index number
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 the 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:-------------------
Copy Code code as follows:

<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 class:-------------------------------------
Copy Code code as follows:

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) br>{
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.