Answer the question
In order to leave comments, you need to log in
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" />
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)
{
}
}
}
<%@ Register TagPrefix="it" TagName="PickStatus" Src="~/UserControls/PickStatus.ascx" %>
<it:PickStatus ID="DropStatus" AutoPostBack="True" runat="Server" DisplayDefault="false" />
<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
Hello.
You already have an event handler
public void dropStatus_OnSelectedIndexChanged(object sender, EventArgs e)
{
}
public void dropStatus_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
//Индекс доступен через ddl.SelectedIndex
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question