This is a migrated thread and some comments may be shown as answers.

RadDropDownList OnSelectedIndexChanged doesn't hit event method

4 Answers 730 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 04 Apr 2016, 09:34 PM

I added a RadDropDownList to my user control and added a OnSelectedIndexChanged but when I run the project and change the dropdown index, it doesn't hit the OnSelectedIndexChanged method.

<telerik:RadDropDownList ID="RadDropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadDropDownList1_SelectedIndexChanged">
    <Items>
        <telerik:DropDownListItem Text="-- ALL --" Value="0" />
        <telerik:DropDownListItem Text="Test 1" Value="1"  />
        <telerik:DropDownListItem Text="Test 2" Value="2" />
    </Items>
</telerik:RadDropDownList>
protected void RadDropDownList1_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
{
    int x = -1;
    x = e.Index;
}

I put a breakpoint on the first line of the SelectedIndexChanged method and it never gets in there.

4 Answers, 1 is accepted

Sort by
0
Jeremy
Top achievements
Rank 1
answered on 05 Apr 2016, 12:18 AM

Clarification:  I realized the event isn't firing because the user control this DropDownList is in is dynamically created in code.  When I move the DropDownList out of that user control and into the one that's creating it, it works.  The problem is, I need the DropDownList to work in the dynamically created user control.

I create a RadTab and RadPageView for each party in my record set.  Then once the tab and page view are created, it inserts a user control with the party's information on it.  The DropDownList filter's the party's information.

ucHistoryTabs.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucHistoryTabs.ascx.cs" Inherits="ucHistoryTabs" %>
 
<div style="width: 100%; padding:0px 0px 10px 0px;">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label runat="server" ID="ErrorMessageLabel" Text="" Visible="false" style="font-size: x-large; color:red" />
            <telerik:RadTabStrip ID="PartyTabStrip" runat="server" MultiPageID="HistorysPages" Skin="" Width="100%" ScrollChildren="true" ScrollButtonsPosition="Right">
                <Tabs></Tabs>
            </telerik:RadTabStrip>
            <telerik:RadMultiPage ID="HistorysPages" runat="server" Width="95%"></telerik:RadMultiPage>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

ucHistoryTabs.ascx.cs

public void BuildTabs(List<PartyInfo> allPartyInfos)
{
    try
    {
        List<int> partyIDs = new List<int>();
        partyIDs = allPartyInfos.Select(i => i.PartyID).Distinct().ToList();
 
        foreach (int partyID in partyIDs)
        {
            List<PartyInfo> infoByParty = new List<PartyInfo>();
            infoByParty.Clear();
            infoByParty = allPartyInfos.Where(i => i.PartyID == partyID).ToList();
 
            string partyName = string.Empty;
            if (infoByParty.Count > 0)
                partyName = infoByParty[0].PartyName;
 
            //first check to see if the tab exists before creating it.
            if (PartyTabStrip.Tabs.Where(t => t.Text == partyName).Count() < 1)
            {
                #region Tab
                Telerik.Web.UI.RadTab tab = new Telerik.Web.UI.RadTab();
                tab.CssClass = "radtab-alltabs";
                tab.SelectedCssClass = "radtab-selectedtab";
                tab.Text = partyName;
                tab.Value = "p_" + partyID.ToString();
                tab.PageViewID = "p_" + partyID.ToString();
                PartyTabStrip.Tabs.Add(tab);
                #endregion
 
                #region Page
                Telerik.Web.UI.RadPageView page = new Telerik.Web.UI.RadPageView();
                page.ID = "p_" + partyID.ToString();
 
                if (infoByParty.Count > 0)
                {
                    ucHistoryDisplay ucWHD = (ucHistoryDisplay)LoadControl("~/ucHistoryDisplay.ascx");
                    ucWHD.FillData(infoByParty);
                    ucWHD.ID = "d_" + partyID.ToString();
                    page.Controls.Add(ucWHD);
                }
                else
                {
                    System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
                    lbl.ID = "d_" + partyID.ToString();
                    lbl.Text = "No records found";
                    page.Controls.Add(lbl);
                }
                #endregion
                HistorysPages.PageViews.Add(page);
            }
        }
        if (PartyTabStrip.Tabs.Count > 0)
            PartyTabStrip.Tabs[0].Selected = true;
        if (HistorysPages.PageViews.Count > 0)
            HistorysPages.PageViews[0].Selected = true;
    }
    catch (Exception ex) { ThrowError(ex, "BuildTabs"); }
}

ucHistoryDisplay.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucHistoryDisplay.ascx.cs" Inherits="uctHistoryDisplay" %>
 
<div style="padding-left:20px;">
    <asp:Table runat="server" ID="FilterControls" Width="100%">
        <asp:TableRow>
            <asp:TableCell>Status</asp:TableCell>
            <asp:TableCell>
                <telerik:RadDropDownList runat="server" ID="StatusDropDown" OnSelectedIndexChanged="StatusDropDown_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
                    <Items>
                        <telerik:DropDownListItem Text="-- ALL --" Selected="true" />
                    </Items>
                </telerik:RadDropDownList>
            </asp:TableCell>
            <asp:TableCell>Type</asp:TableCell>
            <asp:TableCell>
                <telerik:RadDropDownList runat="server" ID="TypeDropDown" OnSelectedIndexChanged="TypeDropDown_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
                    <Items>
                        <telerik:DropDownListItem Text="-- ALL --" Selected="true" />
                    </Items>
                </telerik:RadDropDownList>
            </asp:TableCell>
            <asp:TableCell>Number of Records</asp:TableCell>
            <asp:TableCell><asp:label runat="server" ID="Count" /></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</div>
 
<telerik:RadGrid ID="HistoryRadGV" runat="server" AutoGenerateColumns="false" ShowHeader="false"
                Skin="" style="padding-left: 0px; width: 100%" ItemStyle-BackColor="#FFFFFF"
                AlternatingItemStyle-BackColor="#F5F5F5" OnItemDataBound="HistoryRadGV_ItemDataBound">
</telerik:RadGrid>

ucHistoryDisplay.ascx.cs

        public void FillData(List<PartyInfo> selectedInfo)
        {
            try
            {
                List<string> myStatuses = new List<string>();
                myStatuses = selectedInfo.Select(w => w.Status).Distinct().OrderBy(q => q).ToList();
                StatusDropDown.DataSource = myStatuses;
                StatusDropDown.DataBind();
 
                List<string> myTypes = new List<string>();
                myTypes = selectedInfo.Select(w => w.Type).Distinct().OrderBy(q => q).ToList();
                TypeDropDown.DataSource = myTypes;
                TypeDropDown.DataBind();
 
                Count.Text = selectedInfo.Count.ToString();
 
                HistoryRadGV.DataSource = selectedInfo;
                HistoryRadGV.DataBind();
            }
            catch (Exception ex)
            {
                ThrowError(ex, "FillData");
            }
        }
 
        protected void StatusDropDown_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
//do stuff
}
 
        protected void WarrantTypeDropDown_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
//do stuff
}

Presumably, because I create this user control dynamically, the SelectedIndexChanged events never fire when I change the DropDownList.

 

Thoughts?

 

0
Ivan Danchev
Telerik team
answered on 06 Apr 2016, 01:54 PM
Hello Jeremy,

I replied to the identical thread you started in the Ticketing System. If you have any further questions on this subject you can ask them there. 

Regards,
Ivan Danchev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Panayiotis
Top achievements
Rank 1
answered on 04 Jul 2018, 07:51 AM

Hi,

 

I know that this is an old post but I have the same problem.

 

what was the solution??

0
Marin Bratanov
Telerik team
answered on 04 Jul 2018, 04:38 PM
Hi Panayiotis,

If Jeremy doesn't get a chance to respond and provide details, here is a short summary of the private thread (I cannot go into too much details as it is private) - the issue was coming from the dynamic control generation. The key point was that the controls had to be re-created with the correct IDs (the same with each postback) and to ensure that there is only one control with one ID and the .Clear() method of a Controls collection could be used to remove prior duplicates in case the control createion logic needs to run twice on the same page lifecycle.


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Jeremy
Top achievements
Rank 1
Ivan Danchev
Telerik team
Panayiotis
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or