Search code examples
c#asp.netasprepeater

asp:repeater items not clearing when new dataSource set


I have a form in which I put one repeater control and bind it on pageLoad,it bind as expected.

I have a dropDownList, when I select a value from it I want to clear the repeater dataSource and rebind it in dropDown_SelectedIndexChanged event handler on codebehind.

I set the repeater datasource as null then rebind it with new datasource it wont reflect the changes, it display the first binded value.

Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
        return;

   //Populate the drop down
   PopulateLocationDropdown();

   if (Request.QueryString["locationID"] != null)
   {
       int locationID=Request.QueryString["locationID"];

       //Clear repeater 
       rpt_Displaytheater.DataSource = null;
       rpt_Displaytheater.DataBind();

       //Rebind repeater
       rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
       rpt_Displaytheater.DataBind();
   }
}

When dropDown selectionChanged :

protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
   int locationID=ddlLocation.SelectedValue.ToInt();

   //Clear repeater 
   rpt_Displaytheater.DataSource = null;
   rpt_Displaytheater.DataBind();

   //Rebind repeater
   rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
   rpt_Displaytheater.DataBind();
}

Edit

here is my dropdown :

<asp:DropDownList ID="ddlLocation" DataTextField="LocationName" DataValueField="Record_Id" runat="server" CssClass="textbox_230"
                                            OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AutoPostBack="True">
                                        </asp:DropDownList>

Here is my repeater :

 <asp:Repeater ID="rpt_Displaytheater" runat="server"
            OnItemCommand="rpt_Displaytheater_ItemCommand">
            <ItemTemplate>
                <div class="TheaterListing" style="background: #FFF;">
                    <div class="TheaterName">
                        <div class="Theaterhead">
                            <asp:Label ID="lblTheaterID" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Record_Id") %>'></asp:Label>
                            <asp:LinkButton ID="lnkbtnTheaterName" runat="server" Style="color: #000; font-weight: bold" Text='<%# DataBinder.Eval(Container.DataItem, "Theatre_Name") %>'></asp:LinkButton>
                        </div>
                    </div>
                    <div class="showlist">
                        <%-- show time repeater-Movies Tab --%>
                        <asp:Repeater ID="rpt_showtime" runat="server">
                            <ItemTemplate>
                                <ul style="padding-left: 10px; margin: -18px 0 0 163px;">
                                    <li class="fl" style="font-weight: bold; padding-left: 8px;">
                                        <asp:LinkButton ID="lnk_showtime" CommandName="Showtime" runat="server" CssClass="txtstyle1"
                                            Text='<%# DataBinder.Eval(Container.DataItem, "Time_From") %>'></asp:LinkButton>
                                    </li>
                                </ul>
                            </ItemTemplate>
                        </asp:Repeater>
                        <%-- show time repeater-Movies Tab --%>
                        <%--<asp:LinkButton ID="LinkButton1" runat="server" style="color: #000;font-weight:bold" Text="10.00 AM"></asp:LinkButton>--%>
                    </div>
                </div>

                <br clear="all" />
            </ItemTemplate>
        </asp:Repeater>

My problem is when I reset repeater control's dataSource to new dataSource on dropDown selection changed but the changes is not reflected on the repeater it just show the first bounded values on page load whats wrong with my code


Solution

  • You need to refresh your asp:updatepanel.

    This can be achieved by calling yourPanel.Update() or set the UpdateMode of your asp:updatepanel to Always.