Showing posts with label repeater. Show all posts
Showing posts with label repeater. Show all posts

Monday, March 26, 2012

Updating an UpdatePanel through a LinkButton command generated by a repeater

Hi everybody,

I would like to refresh an update panel when I click on a Linkbutton generated by a repeater that looks like this:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="FilterByPrize" CommandName="filter" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "NameID")%>'>
<%# DataBinder.Eval(Container.DataItem, "Name")%> </asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
The command called is the following:   
 protected void FilterByPrize(object sender, CommandEventArgs E){string connString ="connstring"; SqlConnection IVR =new SqlConnection(connString);string finalquery ="sqlquery " + E.CommandArgument.ToString() +" order by TimeStamp desc"; SqlDataSource2.SelectCommand = finalquery; SqlDataSource2.DataBind(); userData.DataBind();} 
The problem with this is that it reloads the whole page instead of only the update panel. Is there a way to refresh the updatepanel from a function in the c# code called from a linkbutton?
Thanks

MrClash,

The update panel has an Update() method that will do this. But I'm afraid that you are going to have register the linkbutton with the update panel. Take a look at this thread.

http://forums.asp.net/thread/1703456.aspx

Good luck! And feel free to provide some more details if this doesn't work.
Regards.

Updating Nested Repeaters

I'm working on a page with nested repeaters. The child repeater is for comments that can be added by users. My problem is updating the child repeater when a comment is added. I have something like:

<UpdatePanel id="parentUpdatePanel">

<repeater id="parent">

<item stuff>

<UpdatePanel id="commentUpdatePanel">

<repeater id="comments">

<comment stuff>

</repeater>

<a href="http://links.10026.com/?link=javascript:AddComment(itemID)">Add Comment</a>

</UpdatePanel>

</repeater>

</UpdatePanel>

The AddComment function brings up a div-type-dialog to enter the comment. It's doing an AJAX postback to add the comment, but I can't find a way to get back to my comment repeater to update it with the added comment. The comments are also in an asp:panel, so that I can hide them until the user clicks on a "View Comments" link under each item. I could possibly just collapse the panel, and when they re-expand it, it will reload just fine, but that won't look very nice.

I assume that when the parent repeater is databound you are also binding the child repeater for each item in the parent repeater, am I correct? If this is the case if you were to call databind again for your parent repeater shouldn't this rebind the child comment repeater within?


I've found a way around my issue. I tacked on a hidden field in my parent repeater with the item's ID value so I can do a FindControl on the hidden field and match the item ID up. Then, if I know which item I'm dealing with in the parent I can also do a FindControl on my child repeater and update it. Less elegant than I'd like, but it works quite well.