views:

2388

answers:

5

Hi,

I have a Question class:

class Question {
    public int QuestionNumber { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

Now I make an ICollection of these available through an ObjectDataSource, and display them using a Repeater bound to the DataSource. I use <%#Eval("Question")%> to display the Question, and I use a TextBox and <%#Bind("Answer")%> to accept an answer.

If my ObjectDataSource returns three Question objects, then my Repeater displays the three questions with a TextBox following each question for the user to provide an answer.

So far it works great.

Now I want to take the user's response and put it back into the relevant Question classes, which I will then persist.

Surely the framework should take care of all of this for me? I've used the Bind method, I've specified a DataSourceID, I've specified an Update method in my ObjectDataSource class, but there seems no way to actually kickstart the whole thing.

I tried adding a Command button and in the code behind calling MyDataSource.Update(), but it attempts to call my Update method with no parameters, rather than the Question parameter it expects.

Surely there's an easy way to achieve all of this with little or no codebehind?

It seems like all the bits are there, but there's some glue missing to stick them all together.

Help!

Anthony

+1  A: 

You have to handle the postback event (button click or whatever) then enumerate the repeater items like this:

foreach(RepeaterItem item in rptQuestions.Items)
{
   //pull out question
   var question = (Question)item.DataItem;
   question.Answer = ((TextBox)item.FindControl("txtAnswer")).Text;

   question.Save() ?  <--- not sure what you want to do with it
}
Ben Scheirman
DataItem is always null, so this isn't valid.
paulwhit
how are you binding the repeater? DataItem should not be null.
Ben Scheirman
A: 

Then what's the point in the Bind method (as opposed to the Eval method) if I have to bind everything back up manually on postback?

littlecharva
+1  A: 

The bind method really isn't for the repeater, it's more for the formview or gridview, where you are editing just one item in the list not every item in the list.

On both you click a edit button which then gives you the bound controls (bound using bind) and then hit the save link which auto saves the item back into your datasource without any code behind.

Solmead
A: 

Seems like a missed trick if you ask me, but thanks for your response.

littlecharva
A: 

Ben: Having tried it, item.DataItem is always null, and according to the following post, it's not designed to be used that way:

http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic4049.aspx

So how on earth do I manually bind it back?

littlecharva