Sunday, May 1, 2011

UpdatePanel with ASP.NET Repeater and Checkbox Aync Postback issue

I have a rather annoying issue here

I can't get my CheckBox CheckedChange event to fire, or catch or whatever it is that fails:

ASPX Code

<asp:UpdatePanel runat="server" ID="udp_Lists" UpdateMode="Always">
<ContentTemplate>
    <asp:Repeater ID="rep_showings" runat="server">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <div class="div_assignment">
                <div class="div_assignment_text">
                    <asp:LinkButton runat="server" ID="lnk_show_task" OnClick="lnk_show_task_Click" CommandArgument='<%# Eval("Id") %>' Text='<%# Eval("TaskTitle") %>'></asp:LinkButton>
                </div>
                <div class="div_assignment_checkbox">
                    <asp:CheckBox runat="server" ID="chk_handle" AutoPostBack="true" OnCheckedChanged="chk_handle_Changed" ToolTip='<%# Eval("Id") %>' />
                </div>
            </div>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:Repeater>
</ContentTemplate>
<Triggers>
</Triggers>

The Code behind function "chk_handle_Changed" is never reached. The Linkbutten works perfectly.

From stackoverflow
  • I took a look at your problem. I used the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.rep_showings.DataSource = new object[] { new { Title = "title", ID = "id" } };
            this.rep_showings.DataBind();
        }
    }
    
    protected void chk_handle_Changed(object source, EventArgs e)
    {
        Trace.Write("here");
    }
    
    protected void lnk_show_task_Click(object source, EventArgs e)
    {
        Trace.Write("here 2");
    }
    
    protected void rep_showings_ItemCommand(object source, RepeaterCommandEventArgs e)
    { }
    

    The above code works. I think you are probably re-binding your repeater on every postback - I tested this by removing the "if (!IsPostBack)" statement in Page_Load(), and I was able to reproduce the problematic behaviour you describe.

    Rebinding a control on every postback should be avoided if possible. Once a control is populated, it's data is taken care of by ViewState, so unless the data is changing, you should probably not be rebinding it all the time.

    edosoft : Yep, you should only databind in page_load when !isPostback
    The real napster : Solved the issue, thanks for going the extra mile to solve this.

0 comments:

Post a Comment