War
War

Reputation: 8628

Handling repeater events aynchronously

I have a custom search control on my page (asp.net) which contains a textbox and a repeater for displaying results.

The repeater is populated with a callback as the user types ... nice and simple callback.

...

When a search result is selected the repeater fires off a postback and the itemcommand event is raised (as expected) ... and this event adds a child repeater to itself and binds a child list to the current item.

My problem is that I don't want my parent repeater to fire off a full postback because the page is quite time consuming to render. I tried putting the control / just the outer repeater in to an ajax update panel control but it appears to still fire a ful postback.

Can anyone shed any light on how I might tell a repeater to fire its item command event in a callback instead of a postback?

I'm guessing this involves a load of manual wiring for my repeater item controls but i'm hoping theres a control somewhere that handles all that for me :)

EDIT : Sample of my situation ....

<asp:UpdatePanel ... >
  <asp:Repeater ...>
    <itemTemplate> <asp:LinkButton ... CommandArg='<%= Eval("ID") %>' CommandName="select" /> </itemTemplate>
  </asp:Repeater>
</asp:UpdatePanel>

So my question is ...

How do i tell the repeater "fire this link buttons onclick as a callback instead of a postback"

the process of wrapping up the repeater in an update panel doesn't help because the ID of the link button is dynamic and therefore I cannot (not inline anyway) add a trigger for the link button.

If i manually add a trigger to the panel in the repeaters onitembound event i get an exception from .Net sayingt he callback reference is invalid ... i guess this is because im trying to attach a callback trigger to a control that is already handling a postback event or something setup by the repeater ...

EDIT 2 : Sample of the scenario faced here

essentially because this control X number of times on the page virtually everything has to be dynamic. The control implements ICallbackHandler and the search bx code (not included below) fires off an ajax call onkeyup when the user types in company names (so it works a bit like google).

I was hoping that when a user clicked on a company name from the list it would ajax call back / partial postback to recover the sub list of branches thus preventing the full page flicker you get with a full postback.

Then a user would select a branch and it would do a full postback which would result in several server actions taking place.

This works fine as is ... its just not the cleanest user experience.

<div id='<%= this.UniqueID + "Results" %>' class="results">
    <asp:Repeater ID="ui_lstCompanies" runat="server" onitemcommand="ui_lstCompanies_ItemCommand">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Panel ID="item" runat="server">
            <li>
                <asp:LinkButton ID="ui_btnSelectCompany" runat="server" CommandName="Select" Text='<%# Eval("Name") %>' />
            </li>
            </asp:Panel>
            <asp:Panel ID="selectedItem" runat="server" Visible="false">
            <li>
                <hr /><h4><%# Eval("Name") %></h4> 
                <asp:Repeater ID="ui_lstBranches" runat="server" onitemcommand="ui_lstBranches_ItemCommand" >
                    <HeaderTemplate>
                        <table style="border-collapse:collapse;">
                            <tr><th>&nbsp;</th><th>Branch Name</th><th>Branch Address</th><th>Tel</th><th>Fax</th><th>Email</th></tr>
                    </HeaderTemplate>
                    <ItemTemplate>   
                            <tr>
                                <td>&nbsp;&nbsp;&nbsp;</td>
                                <td><asp:LinkButton ID="ui_btnSelectBranch1" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Select" Text='<%# Eval("Name") %>' /></td>
                                <td><asp:LinkButton ID="ui_btnSelectBranch2" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Select" Text='<%# Eval("Address") %>' /></td></td>
                                <td><asp:LinkButton ID="ui_btnSelectBranch3" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Select" Text='<%# Eval("Telephone1") %>' /></td></td>
                                <td><asp:LinkButton ID="ui_btnSelectBranch4" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Select" Text='<%# Eval("Fax") %>' /></td></td>
                                <td><asp:LinkButton ID="ui_btnSelectBranch5" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Select" Text='<%# Eval("Email") %>' /></td></td>
                             </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
                <hr />
            </li>
            </asp:Panel>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>
</div>

Upvotes: 0

Views: 3907

Answers (2)

RobRazor
RobRazor

Reputation: 21

I had a similar problem as you did. If you replace the linkbuttons with regular asp:button and continue to use the repeater's itemcommand event as you were, it will work. Why? I don't know. However, it works. It may not look good with your design, but it triggers the asynch postback that you desired.

Upvotes: 2

TBohnen.jnr
TBohnen.jnr

Reputation: 5129

 <asp:Repeater runat="server" ID="rpt1">
    </asp:Repeater>


<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
<asp:AsyncPostBackTrigger  ControlID="rpt1"/>
</Triggers>
<ContentTemplate>
     <asp:Repeater runat="server" ID="rpt2">
    </asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>

This should then perform an async call to all commands from rpt1. Just Replace these repeater control's with yours

EDIT:

I've basically created a mockup of your code with different fields etc. I assume the code below is what you tried and it was not working? If so then I've got no idea why it's not working on your side as it is on myne, there must be some slight difference somewhere that we're not picking up.

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <ContentTemplate>
        <div id='<%= this.UniqueID + "Results" %>' class="results">
            <asp:Repeater ID="ui_lstCompanies" runat="server" OnItemCommand="ui_lstCompanies_ItemCommand">
                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Panel ID="item" runat="server">
                        <li>
                            <asp:LinkButton ID="ui_btnSelectCompany" runat="server" CommandName="Select" Text='<%# Eval("Name") %>' />
                        </li>
                    </asp:Panel>
                    <asp:Panel ID="selectedItem" runat="server" Visible="false">
                        <li>
                            <hr />
                            <h4>
                                <%# Eval("Name") %></h4>
                            <asp:Repeater ID="ui_lstBranches" runat="server" OnItemCommand="ui_lstBranches_ItemCommand">
                                <HeaderTemplate>
                                    <table style="border-collapse: collapse;">
                                        <tr>
                                            <th>
                                                &nbsp;
                                            </th>
                                            <th>
                                                Branch Name
                                            </th>
                                            <th>
                                                Branch Address
                                            </th>
                                            <th>
                                                Tel
                                            </th>
                                            <th>
                                                Fax
                                            </th>
                                            <th>
                                                Email
                                            </th>
                                        </tr>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr>
                                        <td>
                                            &nbsp;&nbsp;&nbsp;
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="ui_btnSelectBranch1" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                CommandName="Select" Text='<%# Eval("Name") %>' />
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="ui_btnSelectBranch2" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                CommandName="Select" Text='<%# Eval("Address") %>' />
                                        </td>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="ui_btnSelectBranch3" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                CommandName="Select" Text='<%# Eval("Telephone1") %>' />
                                        </td>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="ui_btnSelectBranch4" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                CommandName="Select" Text='<%# Eval("Fax") %>' />
                                        </td>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="ui_btnSelectBranch5" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                CommandName="Select" Text='<%# Eval("Email") %>' />
                                        </td>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </table>
                                </FooterTemplate>
                            </asp:Repeater>
                            <hr />
                        </li>
                    </asp:Panel>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </div>
</ContentTemplate>
</asp:UpdatePanel>

Upvotes: 1

Related Questions