Gaurav
Gaurav

Reputation: 330

State of repeater is not persisted after postback when inside a panel

I have following code structure in markup :

        <asp:Panel ID="Panel1" runat="server" Title="TestPanel" Visible="true">
          <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="true">        
          </asp:GridView>
          <myControl:RepeaterUserControl ID="rpt"></myControl:RepeaterUserControl>
        </asp:Panel>

The panel is used to control visibilty (currently set true for all time). The control 'RepeaterUserControl' is a user control which contains a asp repeater with two buttons named 'Ok' and 'Cancel' (all initially set display:none). All this content of user control is shown on page as modal popup (using modal popup extender) on click of a 'Invoke' button which is also a part of the user control.

<div id="div1" runat="server">
    <ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="divDialog"
        TargetControlID="btnDummyInvoker" CancelControlID="imbClose" PopupDragHandleControlID="divDialogTitle"
        BackgroundCssClass="modalBackground">
    </ajax:ModalPopupExtender>
    <div style="height: 4px;">
    </div>
    <asp:Button ID="btnInvoke" runat="server" OnClick="btnInvoke_Click" Text="Invoke"
        Width="90px" />
    <div style="display: none; position: absolute;">
        <asp:Button ID="Button2" runat="server" />
    </div>
    <div id="div2" runat="server" style="display: none;">
        <asp:Repeater ID="rptList" runat="server">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("DisplayText").ToString() %>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" />
        <asp:Button ID="btnCancel" runat="server" />
    </div>
</div>

So below the grid, user sees a button (Invoke) on page, on click of which the repeater control and the Ok/Cancel buttons are shown in the modal popup. The repeater is assigned a datasource which contains the list of items, user makes changes and on the ok click of the popup, from code behind (ok OnClick event) I access repeater as rptList.Items.Count to do further operations.

Now the problem. If the user control is outside the panel it perfectly returns me the correct count of items, but if the userControl in inside the panel (as shown in code) the count returned on click of ok button is '0' after postback. why is the repeater losing its state in this case ?

Update : Adding code snippet from code-behind, if it helps solving the issue

//This is from the user control code
 protected void btnInvoke_Click(object sender, EventArgs e)
        {   
          rptList.DataSource = listToBeBoundToRepeater;
          rptList.DataBind();
          modalPopupExtender.Show();             
        }

//This is from the user control code
 protected void btnOk_Click(object sender, EventArgs e)
        {

            for (int itemIndex = 0; itemIndex < rptList.Items.Count; itemIndex++)
            {
               // business logiv
            }

        }

rptList.Items.Count is 0 for me after postback for the case I described.

update2 : Here is the weird solution I got, I enclosed the grid and repeater with a tag and set the runat property to server to make it a server control. Now its persisting the values of the repeater after postback properly. Strange, but working now.

Upvotes: 1

Views: 3014

Answers (3)

Gaurav
Gaurav

Reputation: 330

Here is the weird solution I got, I enclosed the grid and repeater with a tag and set the runat property to server to make it a server control. Now its persisting the values of the repeater after postback properly. Strange, but working now.

Upvotes: 1

Kristof
Kristof

Reputation: 3315

Edit: Removed previous answer.
I tried simulating your problem as much as possible in using a main panel containing a usercontrol which contains a repeater that is shown with the ajaxcontroltoolkit.
Alas my repeater item count is always correct though.
Container page :

<%@ Register TagPrefix="a" TagName="test" Src="cc.ascx" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server" />   
    <asp:Panel ID="Panel1" runat="server" Title="TestPanel" Visible="true">
        <a:test runat="server" ID="test"></a:test>
    </asp:Panel>
</asp:Content>

user control cc aspx :

<%@ Register TagPrefix="ajax" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit, Version=4.1.60501.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" %>

<div id="div1" runat="server">
    <ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="divDialog"
        TargetControlID="btnDummyInvoker"
        BackgroundCssClass="modalBackground">
    </ajax:ModalPopupExtender>
    <asp:Button runat="server" style="display:none" ID="btnDummyInvoker"/>
    <div style="height: 4px;">
    </div>
    <asp:Button ID="btnInvoke" runat="server" OnClick="btnInvoke_Click" Text="Invoke" Width="90px" />
    <div style="display: none; position: absolute;">
        <asp:Button ID="Button2" runat="server" />
    </div>
    <div id="divDialog" runat="server" style="display: none;">
        <asp:Repeater ID="rptList" runat="server">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("DisplayText").ToString() %>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" />
        <asp:Button ID="btnCancel" runat="server" />
    </div>
    <asp:TextBox runat="server" ID="result"></asp:TextBox>
</div>

user control code behind :

public partial class cc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnInvoke_Click(object sender, EventArgs e)
        {
            var list = new List<Data>();
            list.Add(new Data { DisplayText = "one" });
            list.Add(new Data { DisplayText = "two" });
            rptList.DataSource = list;
            rptList.DataBind();
            ModalPopupExtender1.Show();
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            var count = rptList.Items.Count;
            result.Text = count.ToString();
        }
    }

Upvotes: 0

rt2800
rt2800

Reputation: 3045

for <asp:UpdatePanel ID="UpdatePanel1" runat="server">, define AsyncPostbackTrigger as btnInvoke

Upvotes: 0

Related Questions