开发者

Integrating ASP.NET repeater control into current page

开发者 https://www.devze.com 2023-02-08 21:36 出处:网络
So I currently have a aspx page that shows details for any single staff member, and the page is setup with mostly ASP labels.I have a subroutine called \"getstaffdata\" that populates the page on load

So I currently have a aspx page that shows details for any single staff member, and the page is setup with mostly ASP labels. I have a subroutine called "getstaffdata" that populates the page on load (via subroutine).

I would like to convert this to a page that shows details on one staff member, or many staff members, one after another. So I thought of using the repeater control, but I'm not sure how to convert the page over. The staff userids are 开发者_StackOverflow社区passed via query string, but I'm just confused on how to do the datasource for the repeater control.

Any input would be appreciated.


The basic outline:

1) Place a Repeater around your original markup:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        [your original markup]
    </ItemTemplate>
</asp:Repeater>

2) Instead of GetStaffData, you now need a method that returns a Collection of staff data items... an array, an ArrayList or List will all work.

3) Bind this collection to the Repeater datasource and databind. Simplified form:

myRepeater.DataSource = GetStaffData(Request.QueryString["staffids"]);
myRepeater.DataBind();

4) Bind the controls now inside ItemTemplate in the ItemDataBound event of the repeater. Two important things to remember: You must now get a handle to the control using FindControl, and your controls will not exist when the Header and Footer items are being bound.

myRepater_ItemDataBound(...)
{
     if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        Label myLabel = e.Item.FindControl("myLabel");
        myLabel.Text = //whatever
    }
}


I'm thinking that you'd have:

  • One repeater at the top to display a list of say names and ID of the staff. Selecting the row e.g. by means of a checkbox or some other control will populate:
  • Another repeater at the bottom. This will display details of the people selected in the first repeater.

E.g. Top Repeater

  ID| Name
[X]1  | John Doe
[ ]2  | Jane Plain
[X]3  | Mary Contrary
[ ]4  | James Brown

[X] incidentally, is a checked checkbox. takes me back to the days of ASCII art ;-)

Bottom Repeater

Name                DoB               Address                                                       Tel    
John Doe          24/12/1970    79a Dead Lane, Mort-on-Avon, Warwicks   01789 999 6669
Mary Contrary 5/3/1982         23 Contrary St, London                                 0207 1234 5678

You'll need two Sql queries or stored procedures. You'll need to amend the one that's currently driving your page and create or re-use another one.

The existing query will be something like:

SELECT * FROM Staff WHERE StaffID = x - x is what is probably being passed to the page via the Querystring.

You'll need to amend this to match multiple staff IDS. I would suggest the quick n dirty method of passing in a CSV list of Ids E.g. '1,4,8' and INNER JOINing your Staff table to a table representation of the CSV. SQL Server has the handy dbo.Split function. Similar things exist in most RDBMS.

The one you need to create will just return a list of IDs and Staff Names in alphabetical order for the listing.

E.g.

SELECT ID, Forename + ' ' + Surname FROM Staff ORDER BY Surname, Forename

You'll Bind your top Repeater to (in the first instance, later you can bind to a collection of objects) a DataTable populated by your query.

In this Repeater users will select the rows they want to view details on and click a 'View Details' type button.

When this button is clicked it fires an event which will loop through the Repeater's rows (RepeaterItems) and make a note (as CSV) of the IDs of the selected rows (you can store these in a control/element in the row).

It will then make the database call to the amended query and pass the CSV IDs along with it.

You then Bind the details repeater to this second data.

A bit of Googling around should get all the blanks filled ;-)

HTH


Datasource can be any kind of collection, array, datatable or similar. Just set reference (repeater.DataSource = someCollection;) and bind ( repeater.DataBind() ). So, get your collection from Linq2Sql, EF, DataSets/TableAdapter, DataReader ... just make sure it's in any collection object type. Repeater is smart enough to use objects from it.
You can access to properties (values) of every object with simple <%= Eval("Property or column") %> inside Repeater item template.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号