DCNYAM
DCNYAM

Reputation: 12126

Repeat ASP.NET Controls without Databinding

I have an ASP.NET page that requires the ability to have users enter multiple records. What I would like is to display a collection of textboxes with an add button below. Each time a user clicks the add button, a new, identical collection of textboxes will be added to the page. Then, when the user clicks submit, each collection of textboxes can be examined, the data extracted, and saved to the database.

Note that the users will only be inserting data to the database. Not editing or updating of records is allowed.

The problem is that each asp.net control I've looked at (Repeater, Formview, etc) requires the control to be databound. However, I would rather not have this. I just want a control to collect the information so I can read through it at the end and do my own inserting into the database. Any ideas on how to easily accomplish this? If necessary, I'll go with databinding to a table, but I figured I'd ask first.

Upvotes: 2

Views: 1837

Answers (5)

HectorMac
HectorMac

Reputation: 6143

Take advantage of the convenience of databinding provided by controls like the Repeater, GridView and ListView.

Use the binding to populate the controls.

Use any method you like to retrieve and update the database from the Repeater etc.

To make 2 way databinding work, you would have to enable it anyway through code.

The Row/Item collections provided by these controls will make your custom code easier to write as well.

Upvotes: 1

Reputation:

Use jQuery to add textbox from client-side and you will have to collection at server-side.

Upvotes: 0

cjk
cjk

Reputation: 46425

You can dynamically add any controls you want to a page, allowing to iterate over your own array and add whateveer it is you need without any databinding.

Upvotes: 0

Julian de Wit
Julian de Wit

Reputation: 3094

You can do a for loop in de aspx

    <% for (i=0;i<list.count;i++)
       {
          string name = "textbox" + i.ToString();
          string value = list[i].property;

%>
        <input type="text" name="<%= name %>" value="<%= value %>">
       <%}
    %>

In the codebehind you can just ask the Request.Form["textbox2"] variable for its value after the post.

Upvotes: 0

teedyay
teedyay

Reputation: 23521

I'd use a repeater: you can databind it to a collection such as a List<> - it doesn't have to bind to something in the database.

Upvotes: 1

Related Questions