Content

ASP .NET 1.1 Repeater - rock star hack

Page is part of Articles in which you can submit an article

written by owen on 2005-Sep-26.

This article assumes that you are either good at some language and new at ASP.NET. Or, you are good at ASP.NET and never written anything or whatever. Anyway I refer to this piece of that I am going to tell you about as a "hack" because I am not very good at ASP.NET and I could not get expert advice on the subject. So after weeks of mental juggling, reading useless/redundant articles and playing around with Web Matrix, I finally got the Repeater object to work.

Basically the code uses the repeater object to create an array of input fields with which I can dynamically add, update or delete rows while still using ASP.NET web controls which are tied to a 2 dimensional data-source. In other web development languages you could simply create an array of input fields but I found some issues with doing that in ASP.NET.

The Code

  • The first step is to create a repeater onto a blank page.

  • Edit the repeater code in source view and add a item tag. Within the item tag your add web controls etc that you want to be repeated. Note that the IDs of my web controls are the same as the columns of my data-source. I use the IDs when I am updating the data-source. My data-source is a "DataTable" object which is stored in a session variable but it could be a "Arraylist" or something else that is "Bindable".

  • Set the web controls to values from your data-source. Note that Text='value' uses single quotes. If you use double quotes then you are going to get a parse error from aspx.

  • Add event functions to your web controls. These events need to be monitored so that the appropriate actions can be taken when they are fired. They work exactly as did before but figuring out which row the event is being fired on is the tricky part. You will notice that all my text boxes fire the same change event - this is optional.

  • Write the event code. You will need a function to delete a row and to update your data-source with changes from the web-controls. You will also need a function to find out the index of the row on which the event is being fired on. Once you have the index you can use that to update or delete the corresponding index in your data-source.

  • That is basically the gist of it. Only thing left to do is to save your DataTable to somewhere permanent. In this example I loop through and print the values in my data-source when you click the save button. You might (and should) be using something more sophisticated.

Download

repeater.zip - zip file

Other Info

  • Eventually you might end up working on a datasource which is "live". Inorder to keep track of the records which are live and ones that are just added to the form aka "new". You might need to have some sort of flag on each row.

  • if you need to refer to controls in a row using javascript use this code;

    <%# Container.uniqueid %>:control_name

permanent link. Find similar posts in Articles.

comments

    Comment list is empty. You should totally be the first to Post your comments on this article.


comment