Asp. NET tip: Make a DataList data source _ Practical skills

Source: Internet
Author: User

This data source is divided into two parts, one is to pull out data from the data class, and then in this data source for paging parameters and page paging secondary control operation! There are three controls on the front page, one Drop-down list, and two linkbutton!

The following fill () method call is very simple, in the page to be bound to the place to write fill () on it, hehe! But be sure to write oh, such as the page after the action is performed!

The following is the DS method in the DB class

    public static DataTable ds (string que)
    {//Returns a data table loaded with SQL development messages,
         OleDbConnection con = Odb.con ();
        OleDbDataAdapter ODA = new OleDbDataAdapter ();
        ODA. Selectcommand=new OleDbCommand (Que,con);
        DataSet ds = new DataSet ();
        ODA. Fill (ds, "THC");
        return DS. tables["THC"];
        con. Close ();
   } The data source used by the method below is this
    private void Fill ()
    {//method , because there will be multiple bindings in the page
       //Here to set a hidden label, with and save the current page index
         int cup = Convert.ToInt32 (pagelbl. Text);

PagedDataSource PS = new PagedDataSource ();//new a paging data source
Ps. DataSource = Odb.ds ("SELECT * from guest ORDER by id DESC"). defaultview;//send a SQL statement to go in, determine the data source data source, a bit around it, hehe
Ps. AllowPaging = true;//Allow paging
Ps. PageSize = 2;//Set Number of pages
Ps. CurrentPageIndex = cup-1;
if (! IsPostBack)
{//To determine if the page is loaded for the first time
for (int i = 1; I <= PS. PageCount; i++)
{//Loop page number
Pageddl. Items.Add (i.ToString ());
}
}
The following is mainly to control whether the page up and down button is
PageUp. Enabled = true;
PageDown. Enabled = true;
if (Ps. Isfirstpage)
{//If is the front page, the button on the page is not available
PageUp. Enabled = false;
}
if (Ps. Islastpage)
{//If the last page, the Next button is not available
PageDown. Enabled = false;
}
Sets the currently selected value of the page number Drop-down menu
Pageddl. Selecteditem.text = Cup. ToString ();
Can finally bind to DataList.
Datalist1.datasource = PS;
Datalist1.datakeyfield = "id";
Datalist1.databind ();
}

And the following is the process of paging events

protected void Pageddl_selectedindexchanged (object sender, EventArgs e)
{//Page number Pull-down Menu event
Pagelbl. Text = Pageddl. SelectedItem.Text.ToString ();
Fill ();
}
protected void Pagedown_click (object sender, EventArgs e)
{//Next page Event
Pagelbl. Text = convert.tostring (Convert.ToInt32 (PAGELBL). Text) +1);
Fill ();
}
protected void Pageup_click (object sender, EventArgs e)
{//Up-page events
Pagelbl. Text = convert.tostring (Convert.ToInt32 (PAGELBL). Text)-1);
Fill ();
}

Related Article

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.