Unable to get event to fire.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minus273
    New Member
    • Apr 2010
    • 3

    Unable to get event to fire.

    I have been unable to get the following code to work. The AddEventClicked event is always null. I have done stuff like this often in the past but for some reason I can't get it to work here. I get no errors. The only thing I can think of is that the problem has something to do with the event not being subscribed to on postback. What am I doing wrong? Thanks.

    Code:
        public partial class _Default : System.Web.UI.Page
        {
            CategoryList List = new CategoryList();
            protected void Page_Init(object sender, EventArgs e)
            {
                List.AddClickedEvent 
                   += new AddClickedEventHandler
                   (List_AddClickedEvent);
                FindControl("Form1").Controls.Add
                    (new CategoryList());
            }
            private void List_AddClickedEvent(int i){
                Response.Redirect(
                    "AddCategory.aspx?ID=" + i.ToString());
            }
        }
    
        public delegate void AddClickedEventHandler(int i);
        public class CategoryList : GridView
        {
            public event AddClickedEventHandler AddClickedEvent;
            public CategoryList()
            {
                SqlConnection Conn = new SqlConnection(ConnStr);
                SqlDataAdapter Adap = new SqlDataAdapter
                    ("select * from Categories 
                    order by Name", Conn);
                DataTable DT = new DataTable();
                Adap.Fill(DT);
                base.AutoGenerateColumns = false;
                base.DataSource = DT;
                ButtonField Add = new ButtonField();
                Add.CommandName = "AddCategory";
                Add.Text = "Add";
                base.Columns.Add(Add);
                BoundField Name = new BoundField();
                Name.DataField = "Name";
                Name.HeaderText = "Thing Type";
                base.Columns.Add(Name);
                base.DataKeyNames = new string[] { "ID" };
                base.DataBind();
                base.RowCommand += new 
                    GridViewCommandEventHandler(BaseClicked);
            }
    }
            private void BaseClicked(object Sender, 
                GridViewCommandEventArgs Args)
            {
                if (Args.CommandName == "AddCategory")
                {
                    int i = (int)((GridView)Sender)
                       .DataKeys[0].Value;
                    if (AddClickedEvent != null) 
                        AddClickedEvent(i);
                }
            }
        }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    On line 45 in your above posted code....
    Does that close brace refer to the class?

    If that's the case then the BaseClicked method is outside of the class that it belongs to?

    Comment

    • minus273
      New Member
      • Apr 2010
      • 3

      #3
      Good catch but no, that close brace on line 45 is not in the actual code. I don't know how it got there.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Your code looks correct as far as I can tell (except that curly brace that you said doesn't exist)...

        I'm sorry but I don't know what to suggest...try stepping through it?

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Where do you ever set AddClickedEvent ? When are you supposed to set it?

          How do you add something to it if it is never initialized?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            On line 54 in your posted code...aren't you supposed to pass "this" as the first parameter and "i" as the second when raising the event?

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              So your basic plan was to add a custom wrapper event to your object's click event?

              You might want to check the IsPostBack property in your form_load and not re-attach your event handler each time maybe?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by plater
                Plater:
                You might want to check the IsPostBack property in your form_load and not re-attach your event handler each time maybe?
                Just so you know the IsPostBack property is not available in the Page Init event. It has not been set yet because view state has not been loaded for the page yet...

                Also, you have to re-attach your event handlers every request.


                I'm pretty sure that the problem is that you aren't raising your event properly. When you raise an event you need to pass 2 arguments: the object that raised the event, and the EventArgs for the event. You may have to create a custom EventArgs for your purposes.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Originally posted by Frinavale
                  Just so you know the IsPostBack property is not available in the Page Init event. It has not been set yet because view state has not been loaded for the page yet...

                  Also, you have to re-attach your event handlers every request.


                  I'm pretty sure that the problem is that you aren't raising your event properly. When you raise an event you need to pass 2 arguments: the object that raised the event, and the EventArgs for the event. You may have to create a custom EventArgs for your purposes.
                  Well the passing of two arguments is only for standard-form events. A custom delegate taking only one parameter is created for that.

                  Have you verified that the base even is being called?

                  Comment

                  • minus273
                    New Member
                    • Apr 2010
                    • 3

                    #10
                    Originally posted by Plater
                    Well the passing of two arguments is only for standard-form events. A custom delegate taking only one parameter is created for that.

                    Have you verified that the base even is being called?
                    Yes, I have.

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      Ok, now does the base even fire when triggering it through your custom event? (ie: only the base event fires, but not your custom code)

                      Comment

                      Working...