Link to home
Start Free TrialLog in
Avatar of wooderz
wooderz

asked on

Using helper class methods to bind repeaters

Morning Experts,

I have a query regarding databinding repeaters. Can the OnItemDataBound method be executed from a helper class? See code snippet for an example? I am doing as attached but the method does not get fired?

Help is appreciated
BindingTools BT = new BindingTools();
 
rptRepeater.ItemDataBound += new RepeaterItemEventHandler(BT.rptRepeater_ItemDataBound);

Open in new window

Avatar of wooderz
wooderz

ASKER

Associating Email Address
Your code is quite correct, and should not fail because it is referencing a method in another class. Can you include a more complete code sample, as I cannot see anything wrong with the provided one :)
The only thing that might throw up a problem with that is how the BindingTools class is created. If it is local to a private method - it might be out of scope when the ItemDataBound method is called.

Does the BindingTools class have to be created for the BT.rptRepeater_ItemDataBound method to work? You could declare the method as static and change the code adding the event to:




rptRepeater.ItemDataBound += new RepeaterItemEventHandler(BindingTools.rptRepeater_ItemDataBound);

Open in new window

Avatar of wooderz

ASKER

I don't think it is private to a method, I have attached all in the code snippet.

Please note that in the real code that I have now posted, BindingTools becomes BannerTools

Regards
public partial class UploadBanners : AppPages.ProtectedPages.PlayerProtectedPage
    {
        Credential oCred;
        public bool EditMode;
        public BannerTools BT = new BannerTools();
 
        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["EditMode"] = EditMode;
 
            // Add JQuery to make Target Selector Work
            if (!ClientScript.IsClientScriptBlockRegistered("TargetSelector"))
                ClientScript.RegisterClientScriptBlock(this.GetType(), "TargetSelector", BT.JavaScript(txtBannerTarget.ClientID), true);
 
            rptBlogCategories.ItemDataBound += new RepeaterItemEventHandler(BT.rptBlogCategories_ItemDataBound);
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            //get current players details
            if (Session["Credential"] != null)
            {
                oCred = (Credential)Session["Credential"];
            }
 
            if (!IsPostBack)
            {
                BT.BindRepeaters(rptPromotions, rptBlogCategories, rptPokerRooms);
                EditMode = String.IsNullOrEmpty(Request.QueryString["Edit"]) ? false : true;
                if (EditMode)
                {
                    validatorBannerImage.Enabled = false;
                    btnDelete.Visible = true;
                    BindFields();
                }
                else
                {
                    validatorBannerImage.Enabled = true;
                    btnDelete.Visible = false;
                }
            }
            else
            {
                EditMode = Convert.ToBoolean(ViewState["EditMode"]);
            }
        }
    }
 
 
 
public class BannerTools
{
    public void rptBlogCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            PostsTableAdapter Posts = new PostsTableAdapter();
            Blog.PostsDataTable PostsTable;
 
            try
            {
                if (e.Item.ItemType.Equals(ListItemType.Item) | e.Item.ItemType.Equals(ListItemType.AlternatingItem))
                {
                    // Get CategoryID from Parent Repeater
                    HiddenField hidCategoryID = (HiddenField)e.Item.FindControl("hidCategoryID");
                    long CategoryID = Convert.ToInt64(hidCategoryID.Value);
 
                    // Bind child repeater with Posts for this Blog/Category Combo
                    Repeater rptPosts = (Repeater)e.Item.FindControl("rptPosts");
                    PostsTable = Posts.GetDataByBlogCategory(BlogTheRake.Config.BlogID, CategoryID);
 
                    if (PostsTable.Rows.Count.Equals(0))
                        PostsTable = Posts.GetDataByCategory(CategoryID);
 
                    rptPosts.DataSource = PostsTable;
                    rptPosts.DataBind();
                }
            }
            finally
            {
                Posts = null;
                PostsTable = null;
            }
        }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GuitarRich
GuitarRich
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wooderz

ASKER

Ah I understand, sorry, let me try that and get back to you
Avatar of wooderz

ASKER

Unfortunately not. That still doesn't fire on a static method :(
Have you tried binding the event up in the markup?
Avatar of wooderz

ASKER

Can't do that because the event is fired by a helper class.
Avatar of wooderz

ASKER

All sorted. Had to bind the event in Page_Load not PreRender! Thank you!