Repeater Item Data Bound not firing on callback script

Khoa Nguyen asked on July 23, 2018 05:32

I try to render a repeater control when client click (followed this Implementation )

When first time page load, ItemDataBound event work perfectly.

For some reason, when call script from client click, repeater renders correct, but ItemDataBound event isn't firing. I can't work out why.

Heres what I have; (Removed unnecessary stuff)

public void RaiseCallbackEvent(string eventArgument)
{
    var pages = DocumentHelper.GetDocuments(ClassNames)
                                         .Path(Path)
                                         .OnSite(CurrentSite.SiteName)
                                         .Culture(CurrentDocument.DocumentCulture)
                                         .OrderBy(order)
                                         .Published()
                                         .Where(where)
                                         .Columns(Columns);

    repItems.DataSource = pages.Result;
    repItems.DataBind();
    repItems.ItemDataBound += repItems_ItemDataBound;
}

protected void repItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //action here
}

I have consider using the ItemCreated event instead. But it has the same.

any solution ?

Correct Answer

Trevor Fayas answered on July 31, 2018 14:08

Yes, you only really do one update panel, whatever contains all the elements that you want to update together when one posts back. So make 1 zone with update panel checked, then make sure repeater and pager are in there and they are normal, and make sure the pager is using postback instead of url mode.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Development Support answered on July 23, 2018 15:40 (last edited on July 23, 2018 15:42)

(Removed, wring login)

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 23, 2018 15:42

have you tried adding the event before you actually run the action? DataBind will rebind the items, so most likely your "ItemDataBound" is run before your event is actually set. Try flipping repItems.DataBind() and repItems.ItemDataBound += repItems_ItemDataBound around and tell me if that works.

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 24, 2018 03:24

Hi Trevor Fayas, i have tried flipping around, but didn't work

//both flipping not working
repItems.DataSource = Nodes;
repItems.ItemDataBound += repItems_ItemDataBound;        
repItems.DataBind();

repItems.ItemDataBound += repItems_ItemDataBound;        
repItems.DataSource = Nodes;
repItems.DataBind();
0 votesVote for this answer Mark as a Correct answer

Development Support answered on July 24, 2018 03:47

Okay it may be a lifecycle issue. Can you try adding the action onInit vs on the post back and see if it runs then? It's possible that this area is triggering after that event is triggerable. Or maybe give a little more context/code?

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 24, 2018 06:36

Here is almost what i am customize repeater:

public partial class CMSWebParts_Products : CMSAbstractWebPart, ICallbackEventHandler
{
public string content = "";

#region "Kentico properties"
//i'm touch nothing
#endregion


#region "Methods"
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    // calling Call back event registration.
    CallBackEvent();
}

// i'm start coding here to render repeater

/// <summary>
/// Registering the Call back event.
/// </summary>
private void CallBackEvent()
{
    var cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "$.ReceiveProduct", "context");
    var callbackScript = "function ListProduct(arg, context)" + "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ListProduct", callbackScript, true);
}
/// <summary>
/// Get Call back result.
/// </summary>
/// <returns> Returns the Call back result </returns>
public string GetCallbackResult()
{
    //throw new NotImplementedException();
    return content;
}

/// <summary>
/// Invoke Call back event
/// </summary>
/// <param name="eventArgument"> Event Argument </param>
public void RaiseCallbackEvent(string eventArgument)
{
    var pages = DocumentHelper.GetDocuments(ClassNames)
                                 .Path(Path)
                                 .OnSite(CurrentSite.SiteName)
                                 .Culture(CurrentDocument.DocumentCulture)
                                 .OrderBy(OrderBy)
                                 .Published()
                                 .Where(where)
                                 .Columns(Columns);

    repItems.DataSource = pages.Result;
    repItems.DataBind();
    repItems.ItemDataBound += repItems_ItemDataBound;

    // Render control
    StringBuilder stringBuilder = new StringBuilder();
    using (StringWriter textWriter = new StringWriter(stringBuilder))
    {
        using (HtmlTextWriter writer = new HtmlTextWriter(textWriter))
        {
            RenderChildren(writer);
        }
    }
    content = stringBuilder.ToString().Replace("~/", "/");
}

}

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 24, 2018 14:54

Thanks for the code, next thing i need is an explanation of what you are trying to accomplish. Is it that this is a single repeater (of products) that when someone clicks on the item, it posts back and updates the repeater to show a different list (say of sub products)?

or is "repItems" a secondary repeater (or maybe just a control not present on the page?) that you are using to try to render the results of that post back call?

A little more info may help me understand, and there may be a simpler way of doing things.

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 25, 2018 03:22 (last edited on July 25, 2018 03:22)

Hi Trevor Fayas,

It is a single repeater with checkbox option on top (filter), when user click on checkbox, i want to rebind repeater with NO postback (reload).

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 27, 2018 03:23

any one ?

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 27, 2018 06:08

You can use an ajax update panel to post back without reloading the page, not sure how you are going to do it any other way as you can't run code without a post back of some sorts, update panel can do that through an ajax call. You can check on the web part to use an ajax update panel then do a normal post back with the checkbox and do your stuff.

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 30, 2018 03:19

Hi Trevor Fayas,

The reason i cannot use update panel, because i would like to update url also (update panel did not do that)

After receive callback:

window.history.replaceState(null, null, url + paramUrl)
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 30, 2018 14:08

You can use update panels and execute your JavaScript, Google "run JavaScript after asp.net update panel postback"

There's a manager object that you can bind before and after requests and perform your logic. I would go that route.

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on July 31, 2018 11:27

Hi Trevor Fayas,

I had tried your solutions, but not get lucky, did i make something wrong ?

I had zone with repeater on it, make sure both zone and repeater checked "Update Panel"

Another zone with universal pager on it, and both zone and pager checked "Update Panel". Pager using mode "Query String" and connect to repeater.

After post back, click on next page, update panel not working, it still reload.

0 votesVote for this answer Mark as a Correct answer

Khoa Nguyen answered on August 3, 2018 09:12 (last edited on August 3, 2018 10:41)

Thank for your help, it working now...

But i face to another problem, when i using update panel and update url, go to next page, reload it. How can i pass pager correct.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.