A
A
Alexey Sh2016-01-21 12:54:06
ASP.NET
Alexey Sh, 2016-01-21 12:54:06

How to throw an event from a DropDownList nested into the UserControl?

Good day to all!
There is a Control with the following markup

<%@ Control Language="c#" CodeBehind="PickStatus.ascx.cs" AutoEventWireup="True" Inherits="BugNET.UserControls.PickStatus"  %>
<asp:DropDownList 
  id="dropStatus" 
  CssClass="form-control" 
  OnSelectedIndexChanged="dropStatus_OnSelectedIndexChanged" 
  Runat="Server">
  <asp:ListItem Value="0">-- Select Status --</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator 
   id="reqVal" 
   Visible="false" 
   ControlToValidate="dropStatus" 
   InitialValue="0" 
   Text="(required)" 
   ErrorMessage="Status is required"
   Runat="Server" 
   meta:resourcekey="reqVal" 
   CssClass="text-danger validation-error" />

This is the CodeBehind:
public partial class PickStatus : System.Web.UI.UserControl
  {
        /// <summary>
        /// Gets or sets a value indicating whether [display default].
        /// </summary>
        /// <value><c>true</c> if [display default]; otherwise, <c>false</c>.</value>
        public bool DisplayDefault { get; set; }

        /// <summary>
        /// Gets the selected text.
        /// </summary>
        /// <value>The selected text.</value>
        public string SelectedText
        {
            get { return dropStatus.SelectedItem.Text; }
        }

       public bool AutoPostBack
       {
          set { dropStatus.AutoPostBack = value; }
       }
        /// <summary>
        /// Gets or sets the selected value.
        /// </summary>
        /// <value>The selected value.</value>
    public int SelectedValue 
    {
      get {return Int32.Parse(dropStatus.SelectedValue); }
      set 
      {
        try 
        {
          dropStatus.SelectedValue = value.ToString();
        } 
        catch {}
      }
    }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PickStatus"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    public bool Enabled 
    {
      get { return dropStatus.Enabled; }
      set { dropStatus.Enabled = value; }
    }

        /// <summary>
        /// Gets or sets the data source.
        /// </summary>
        /// <value>The data source.</value>
        public List<Status> DataSource { get; set; }

        /// <summary>
        /// Binds a data source to the invoked server control and all its child controls.
        /// </summary>
    public override void DataBind() 
    {
      dropStatus.Items.Clear();
      dropStatus.DataSource = DataSource;
      dropStatus.DataTextField = "Name";
      dropStatus.DataValueField = "Id";
      dropStatus.DataBind();
      if (DisplayDefault)
                dropStatus.Items.Insert(0, new ListItem(GetLocalResourceObject("SelectStatus").ToString(), "0"));
    }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PickStatus"/> is required.
        /// </summary>
        /// <value><c>true</c> if required; otherwise, <c>false</c>.</value>
    public bool Required 
    {
      get { return reqVal.Visible; }
      set { reqVal.Visible = value; }
    }


       public void dropStatus_OnSelectedIndexChanged(object sender, EventArgs e)
       {
       }
  }
}

I connect it to the page like this:
<%@ Register TagPrefix="it" TagName="PickStatus" Src="~/UserControls/PickStatus.ascx" %>
<it:PickStatus ID="DropStatus" AutoPostBack="True" runat="Server"  DisplayDefault="false" />

The problem is that it does not have the usual DropDownList event - OnSelectedIndexChanged (at least IntelliSense does not show)
And I need it, for example, to check the index of the selected element inside it and do something on the page based on this.
How it is possible to inherit event from DropDownList, that which is inside UserControl'a?
I don’t understand events very well, I understand that when we write on the markup, for example
<asp:DropDownList runat="server" OnSelectedIndexChanged="SomeEvent" />
we thus call the virtual void OnSelectedIndexChanged() method, which has a Handler inside, into which the SomeEvent() method is pushed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mletov, 2016-01-21
@mletov

https://msdn.microsoft.com/en-us/library/fwd3bwed.aspx

H
heartdevil, 2016-01-21
@heartdevil

Hello.
You already have an event handler

public void dropStatus_OnSelectedIndexChanged(object sender, EventArgs e)
       {
       }

Now you need to get your object like this
public void dropStatus_OnSelectedIndexChanged(object sender, EventArgs e)
       {
          DropDownList ddl = (DropDownList)sender;
          //Индекс доступен через ddl.SelectedIndex
       }

For clarity, put a breakpoint on this line DropDownList ddl = (DropDownList)sender; and check if your event fires.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question