0
votes

I can't do post back of the UpdatePanel inside the repeater.

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:Repeater ID="Posts" runat="server" OnItemCreated="Posts_OnItemCreated">
    <ItemTemplate>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
            <ContentTemplate>
                <asp:Label ID="lblComentarios" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click"/>
            </Triggers>
        </asp:UpdatePanel>

        <asp:TextBox ID="txtComentario" runat="server" CssClass='<%# Eval("PostId") %>'></asp:TextBox>

        <asp:Button ID="Button1" ClientIDMode="Static" runat="server" OnCommand="btnComentar_OnCommand" CommandName='<%# Eval("PostId") %>' class='<%# Eval("PostId") %>' />

    </ItemTemplate>
</asp:Repeater>

Code for repeater OnItemCreated():

protected void Posts_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
    var control = e.Item.FindControl("Button1");
    ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}

Then in my codebehind OnCommand ..

protected void btnComentar_OnCommand(object sender, CommandEventArgs e)
{
    MyCode();
    foreach (RepeaterItem item in Posts.Items)
    {
        var panel = (UpdatePanel)item.FindControl("UpdatePanel1");
        panel.Update();
    }
}            

everything is doing what it should except panel.update().

1
hmm.. so btnComentar_OnCommand is not firing? - Brett Caswell
It is doing all the code behind I have. but not refreshing the content template - Mário Tomé

1 Answers

0
votes

I have done below code and its updating test values to label

ASPX:

<%@ Page Language="C#" AutoEventWireup="true"  EnableEventValidation="false" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

            <asp:Repeater ID="Posts" runat="server" OnItemCreated="Posts_OnItemCreated">
                <ItemTemplate>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:Label ID="lblComentarios" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
                              <asp:Label ID="Label1" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>

                    <asp:TextBox ID="txtComentario" runat="server" CssClass='<%# Eval("PostId") %>'></asp:TextBox>

                    <asp:Button ID="Button1" ClientIDMode="Static" runat="server" OnCommand="btnComentar_OnCommand" CommandName='<%# Eval("PostId") %>' class='<%# Eval("PostId") %>' />

                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

Code Behind:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("LabelComentarios");
                dt.Columns.Add("PostId");
                dt.Rows.Add("esresrer");
                dt.Rows.Add("esresrer");
                dt.Rows.Add("esresrer");


                Posts.DataSource = dt;
                Posts.DataBind();
            }
        }
        protected void Posts_OnItemCreated(object sender, RepeaterItemEventArgs e)
        {
            var control = e.Item.FindControl("Button1");
            ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
        }
        protected void btnComentar_OnCommand(object sender, CommandEventArgs e)
        {
            //MyCode();
            foreach (RepeaterItem item in Posts.Items)
            {


                var lbl = (Label)item.FindControl("Label1");
                lbl.Text = "testest";
                var panel = (UpdatePanel)item.FindControl("UpdatePanel1");
                panel.Update();
            }
        }
    }
}

Also check if !IsPostback proper or not