views:

1827

answers:

2

I have a Repeater which displays a list of items (in a grid-like/table view). When a user clicks an item, I display an UpdatePanel under this item with additional information relevant to the item (similar to Accordion control). I know how to populate the elements of the UpdatePanel in code-behind (I pass the ID of the selected element in the Repeater control as CommandArgument, get additional info for this ID, and set up the text fields of the active UpdatePanel controls). But I'm wondering if I could set up binding directly in the ASPX (instead of code-behind). When I used the <%= %> syntax to assign text fields of the UpdatePanel control the values of the page properties, e.g. <%= Comment %>, it sort of worked, but it changed the fields of all UpdatePanels in the repeater. Is there any way to bind the active UpdatePanel to the current values and leave already bound UpdatePanels unchanged?

A: 

Are you looking to display a container that displays additional information? Is there other activity in the "box" that requires it be an updatepanel?

<asp:repeater>
   <itemtemplate>
      <%# Eval("Name") %> <%# Eval("LastName") %><br />
      <span onclick="$get('<%# Eval("Id") %>')">View Age</span>
      <div id="<%# Eval("Id")%>" style="display:none;">
          Age: <%# Eval("Age") %>
      </div>
   <itemtemplate>
</asp:repeater>

Ithink that's right, some syntax may be off a bit (typing without intellisense). Would that work? I used ID as a unique identifier for the div id and the onclick command. You could also use jquery, asp:controls or whatever else you wanted.

Tony Basallo
I use UpdatePanel because additional info comes from a different DB, so getting it for each item in Repeater would take too long (and not necessary). I only want to get it when user expands an item (I also let user update it). In your sample you bind all fields at once, but this is not what I want.
Alek Davis
To clarify:You want to have an updatepanel within the repeater that when expanded it will go out and get some data from an available source?
Tony Basallo
Right. When I expand UpdatePanel in Repeater, it gets more data for the selected item. The user than can make changes and save them back to the database and it would only affect the active UpdatePanel.
Alek Davis
+1  A: 

Easiest way is to nest a FormView inside the update panel. Then the only thing you need to do in the code behind is get the additional info, assign it to the FormView.DataSource, and call FormView.DataBind(). Everything in the FormView will use the <%# Eval("SomeColumn") %> syntax. You'll probably need to use a FindControl() to get a reference to the FormView. I'd type up the code for you but I'll save you some headaches down the road and say DON'T DO THIS.

The update panel is about the most inefficient way to do any ajax stuff. The only way to get it all to wire up correctly with this repeater and server side code is to either have a gigantic viewstate or to rebind the repeater in your page load. You are turning a request that could be 300ms into something that will take over a second...or longer! Get familiar with a good ajax framework and don't be afraid to write real html. At the very least, use a webservice that loads a usercontrol with your markup.

I know the update panel is easy, and it's built in. It might even be adequate for what you are doing, but you must resist. You'll be glad you did.

Al W
Thanks Al. I'll check out FormView (didn't use it before). Regarding UpdatePanel, I see your point, but for my application the performance difference (1 or so second vs 500 or so milliseconds) is really negligible.
Alek Davis
BTW, I checked FormView and it seemed too bloated for the simple thing I wanted to do. I ended up populating UpdatePanel in code-behind, but if I were to do it in ASPX, I would do the following. (See next comment...)
Alek Davis
In code-behind: define a protected member of the data type used for binding UpdatePanel controls; populate properties of this object. In ASPX, use the <%= MemberName.PropName %> (not the <%# %> notation) to populate controls (may need to call bind first). I haven't tried it, but think it should work
Alek Davis