locked
Create Event In User Control. RRS feed

  • Question

  • User-1215342584 posted

    Hello Friends,

    I have telrik combo box , I m using this control as my Custom Control. Telrik combobox has an event named “ItemsRequested”.

    I hv made an user control in which I have made a property suppose :

        public string Query

        {

            get { return _Query; }        set { _Query = value; }

        }

      

    Now I want to create an event for this control in my UserControl.ascx.cs file using delegate according to “Query “ property’s value that has been set for this control by user.

      protected void Page_Load(object sender, EventArgs e)

      {

             if (_Query == "anything")

            {

                     RadComboBox1.ItemsRequested += new   Telerik.Web.UI.RadComboBoxItemsRequestedEventHandler(KeyPressOnCombo);

             }

    }

      

    Please tell me that in which event I can create this event because if I do this in Page_load of the control then I am getting my Control’s property always is blank while I have set the property with a string value.

     

    I hope I have described my query nicely.

     

     

    Thanks!!

    Friday, November 13, 2009 1:45 AM

All replies

  • User2130758966 posted

    Hey,

    Looking at this page:

    one of the events between init and page_load should do the trick?

    init is where all controls are created so you cant reference them in events before then.

    Friday, November 13, 2009 7:34 AM
  • User-1215342584 posted

    thanks for your reply,

     

              so you mean there is no way...I will have to pas event to my parent page is the only way?

     

     

    Friday, November 13, 2009 7:59 AM
  • User-2106054853 posted

    Hi,

    I'm unable to reproduce this issue. My test code is like below. Could you please test it to see if you can get _Query in Page_Load?

    ascx.cs:

     public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // I can get the value of _Query here
            }
            public string Query
            {

                get { return _Query; }
                set { _Query = value; }

            }
            string _Query;

        }

     aspx:

    <uc1:WebUserControl1 Query="anything" ID="WebUserControl11" runat="server" />

    Monday, November 16, 2009 2:35 AM
  • User-1215342584 posted

    Hello Friends,

     

       I have used event handling to get the UserControl Events in Parent Page.

     

      I am doing like this in my ASCX.CS Page:

    public event EventHandler SelectedText;
    
        void SelectTextFunction()
        {
            if (SelectedText != null)
            {
                SelectedText(this, EventArgs.Empty);
            }
        }
        protected void RadComboBox1_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SelectTextFunction();
        }


    and in ASPX.CS page i am doing like this:

     if (txt_TotalItems.Text.Trim() != "")
            {
                BindingGrid();  //here I am binding my grids.
                for (int i = 0; i < GV_MasterEntry.Rows.Count; i++)
                {
                    CmbTelrikNormal Cmb = ((CmbTelrikNormal)(GV_MasterEntry.Rows[i].FindControl("CmbTelrikNormal_IndNo")));
                    Cmb.SelectedText += new EventHandler(MyEvent);
                }
            }


     but in SelectedText is coming with NULL value

            if (SelectedText != null)
            {
                SelectedText(this, EventArgs.Empty);
            }
    so my event is not being called.

     

    But if i use normal ASP:Button control in place of Telrik ComboBox then it is running fine without making a single change. Please brief me what is the issue.

    Thanks !!

    Wednesday, November 18, 2009 4:45 AM
  • User-1078840699 posted

    Hello dpkbahuguna,

    First, you need to declare the event within your class (alongside your methods and constructors):

    public event EventHandler LabelsTextChanged;

    Then you need to create a method to handle the individual labels' TextChanged events.

    private void HandleLabelTextChanged(object sender, EventArgs e)
    {
        // we'll explain this in a minute
        this.OnLabelsTextChanged(EventArgs.Empty);
    }

    Somewhere, probably in your control's constructor, you need to subscribe to the label's TextChanged events.

    myLabel1.TextChanged += this.HandleLabelTextChanged;
    myLabel2.TextChanged += this.HandleLabelTextChanged;
    myLabel3.TextChanged += this.HandleLabelTextChanged;

    Now for the HandleLabelsTextChanged method. We could raise LabelsTextChanged directly; however, the .NET framework design guidelines say that is it a best practice to create an OnEventName protected virtual method to raise the event for us. That way, inheriting classes can "handle" the event by overriding the OnEventName method, which turns out to have a little better performance than subscribing to the event. Even if you think you will never override the OnEventName method, it is a good idea to get in the habit of doing it anyway, as it simplifies the event raising process.

    Here's our OnLabelsTextChanged:

    protected virtual void OnLabelsTextChanged(EventArgs e)
    {
        EventHandler handler = this.LabelsTextChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    We have to check for null because an event without subscribers is null. If we attempted to raise a null event, we would get a NullReferenceException. Note that we copy the event's EventHandler to a local variable before checking it for null and raising the event. If we had instead done it like this:

    if (this.LabelsTextChanged != null)
    {
        this.LabelsTextChanged(this, e);
    }

    We would have a race condition between the nullity check and the event raising. If it just so happened that the subscribers to the event unsubscribed themselves just before we raised the event but after we checked for null, an exception would be thrown. You won't normally encounter this issue, but it is best to get in the habit of writing it the safe way.

    Edit: Here is how the public event EventHandler LabelsTextChanged; line should be placed:

    namespace YourNamespace
    {
        class MyUserControl : UserControl
        {
            // it needs to be here:
            public event EventHandler LabelsTextChanged;
    
            ...
        }
    }

    Here is another example below:

    public event EventHandler ButtonClick;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        //bubble the event up to the parent
        if (this.ButtonClick!= null)
            this.ButtonClick(this, e);
    }

    Form:

    UserControl1.OnButtonClick += new EventHandler(UserControl_ButtonClick);
    
    protected void UserControl_ButtonClick(object sender, EventArgs e)
    {
        //handle the event 
    }
    Monday, March 23, 2015 5:54 PM
  • User-1078840699 posted

    Hello dpkbahuguna,

    Any update about that?

    Monday, April 13, 2015 3:32 PM