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

wooderzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wooderzAuthor Commented:
Associating Email Address
0
deadlyDevCommented:
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 :)
0
GuitarRichCommented:
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

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

wooderzAuthor Commented:
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

0
GuitarRichCommented:
no - I wasn't saying that the event is private - just that it doesn't need to use any variables or other methods inside the BannerTools class - so it you make it a static method - the class will not need to be created for the method to be used.

I think what might be happening is that the class object is going out of scope when the ItemDataBound event fires - so the event can't call the method in the class. If you make it static - that doesn't matter.

public class BannerTools
{
    public static 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wooderzAuthor Commented:
Ah I understand, sorry, let me try that and get back to you
0
wooderzAuthor Commented:
Unfortunately not. That still doesn't fire on a static method :(
0
deadlyDevCommented:
Have you tried binding the event up in the markup?
0
wooderzAuthor Commented:
Can't do that because the event is fired by a helper class.
0
wooderzAuthor Commented:
All sorted. Had to bind the event in Page_Load not PreRender! Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.