Bind a repeater to an XML data source or file

Source: Internet
Author: User
1. namespace to be referenced
Using system;
Using system. Data;
Using system. Web;
Using system. Web. UI. webcontrols;

2. Bind the Repeater control to the xmldatasource data source without Paging
1) Foreground testrepeater. aspx file:

<Asp: repeater id = "xml1" runat = "server" performanceid = "xmlperformance1">
<Itemtemplate>
<Ul>
<Li style = "color: Blue;"> <% # server. htmlencode (XPath ("name"). tostring () %> </LI>
<Li> <% # server. htmlencode (XPath ("mark"). tostring () %> </LI>
<Li> <B> quantity: </B> <% # XPath ("quantity") %> <B> price: </B> <% # XPath ("price") %> <B> author: </B> <% # XPath ("author") %> </LI>
</Ul>
<HR/>
</Itemtemplate>
</ASP: repeater>
<Asp: xmldatasource runat = "server" datafile = "~ /Data/test. xml "> </ASP: xmldatasource>

2) Background testrepeater. aspx. CS File

Without special requirementsCodeTo display data.

3. The Repeater control binds an XML file directly through dataview, with pagination
1) Foreground testrepeater. aspx file:

<Asp: repeater id = "xml1" runat = "server">
<Itemtemplate>
<Ul>
<Li style = "color: Blue;"> <a style = "color: red;" href = '? Id = <% # eval ("ID") %> '> Delete </a> <% # server. htmlencode (eval ("name "). tostring () %> "</LI>
<Li> <B> quantity: </B> <% # eval ("quantity") %> <B> price: </B> <% # eval ("price") %> <B> author: </B> <% # eval ("author") %> </LI>
</Ul>
<HR/>
</Itemtemplate>
</ASP: repeater>
<Ul>
<Li>
<Asp: hyperlink id = "hyhome" runat = "server" text = "Homepage"> </ASP: hyperlink>
<Asp: hyperlink id = "hyprev" runat = "server" text = "Previous Page"> </ASP: hyperlink>
<Asp: hyperlink id = "hynext" runat = "server" text = "Next"> </ASP: hyperlink>
<Asp: hyperlink id = "hyend" runat = "server" text = "last page"> </ASP: hyperlink>
Total <asp: literal id = "rectotal" runat = "server"> </ASP: literal> items, <asp: literal id = "pagecount" runat = "server"> </ASP: literal> page, the current <asp: literal id = "pageindex" runat = "server"> </ASP: literal> page
</LI>
</Ul>

2) Background testrepeater. aspx. CS file:

protected void page_load (Object sender, eventargs e)
{< br> string file = server. mappath ("/data/test. XML ");
dataset DS = new dataset ();
DS. readxml (File);
datatable dt = Ds. tables [0];

// Perform the delete operation
Int id = convert. toint32 (request. querystring ["ID"]);
If (ID> 0)
{
DT. Rows [ID-1]. Delete ();
DT. acceptchanges ();
DS. writexml (File );
}
// Perform the delete operation

Dataview DV = DT. defaultview;
DV. Sort = "date DESC ";

Pageddatasource PPS = new pageddatasource ();
PPS. datasource = DV;
PPS. allowpaging = true;
PPS. pagesize = 10;

Int pageindex = convert. toint32 (request. querystring ["page"]);
If (pageindex <= 1) pageindex = 1;
PPS. currentpageindex = pageindex-1;

This. xml1.datasource = PDS;
This. xml1.databind ();

This. rectotal. Text = PPS. performancecount. tostring ();
This. pagecount. Text = PPS. pagecount. tostring ();
This. pageindex. Text = pageindex. tostring ();

If (pageindex = 1)
This. hyhome. navigateurl = string. empty;
Else
This. hyhome. navigateurl = "? Page = 1 ";

If (pageindex> 1)
This. hyprev. navigateurl = "? Page = "+ (pageindex-1) + "";
Else
This. hyprev. navigateurl = string. empty;
If (pageindex <PPS. pagecount)
This. hynext. navigateurl = "? Page = "+ (pageindex + 1) + "";
Else
This. hynext. navigateurl = string. empty;
If (pageindex> = maid. pagecount)
This. hyend. navigateurl = string. empty;
Else
This. hyend. navigateurl = "? Page = "+ (PPS. pagecount) + "";
}

4. xml file format:



1
Chinese hero
50
45.55
tiger

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.