2

I created a simple page in which I have two dropdownlists inside a UpdatePanel. When the first Dropdownlist changes selection, it's suppose to update the list for the second dropdownlist, but it doesn't work, and I'm not sure what I'm doing wrong.

I get the following error: A control with the ID "ctl00 $ MainContent $ FunctionCombo" could not be found for the trigger in the UpdatePanel "HeadUpdatePanel".

Even so when I step through it finds it and is not null. See this line:

DropDownList dd = (DropDownList)cph.FindControl("FunktionCombo");

But when it tries to apply the trigger than this error comes:

trigger.ControlID = cph.FindControl("FunktionCombo").UniqueID;

FYI: The masterpage has no content, just calling the TestPage!

Here is my Code for TestPage:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="Test.TestPage" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server" />

    <div class="container">
        <div>
            <asp:UpdatePanel ID="HeadUpdatePanel" UpdateMode="Always" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Wähle Funktion:" />&nbsp;
                    <asp:DropDownList ID="FunktionCombo" OnSelectedIndexChanged="FunktionCombo_SelectionChanged" AutoPostBack="true" runat="server" />&nbsp;&nbsp;&nbsp;
                    <asp:Label ID="Label2" runat="server" Text="Wähle Tabelle:" />&nbsp;
                    <asp:DropDownList ID="TabelleCombo" OnSelectedIndexChanged="TabelleCombo_SelectionChanged" AutoPostBack="true" runat="server" />&nbsp;&nbsp;&nbsp;
                    <asp:Label ID="CountLbl" runat="server" Text="Wähle Funktion:" />
                    <asp:Button ID="StartBn" runat="server" Text="Start" onclick="StartBtn_Click" />
                </ContentTemplate>               
            </asp:UpdatePanel>            
        </div>        
    </div>
</asp:Content>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class TestPage : System.Web.UI.Page
    {

        private List<ViewInfo> _ospList;
        private List<ViewInfo> _aeList;
        private List<ViewInfo> _icList;

        private bool _functionChanged = false;
        private string _currentFunctionDisplay = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpContext.Current.Session["IsAdmin"] = true;
                HttpContext.Current.Session["IsSubAdmin"] = true;

                CreateComboLists();

                ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");
                UpdatePanelControlTrigger trigger = new PostBackTrigger();
                DropDownList dd = (DropDownList)cph.FindControl("FunktionCombo");
                trigger.ControlID = cph.FindControl("FunktionCombo").UniqueID;

                HeadUpdatePanel.Triggers.Add(trigger);
            }
        }

        private void CreateComboLists()
        {
            if ((bool)HttpContext.Current.Session["IsAdmin"] == true)
            {
                FunktionCombo.Items.Add("OSP");
                FunktionCombo.Items.Add("AE");
                FunktionCombo.Items.Add("IC");
                FunktionCombo.DataBind();
                FunktionCombo.SelectedIndex = 0;

                _ospList = GetSprocList("OSP", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
                _aeList = GetSprocList("AE", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
                _icList = GetSprocList("IC", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);

                TabelleCombo.DataSource = _ospList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                TabelleCombo.SelectedIndex = 0;
                TabelleCombo.DataBind();
            }            
        }

        protected void FunktionCombo_SelectionChanged(object sender, System.EventArgs e)
        {
            DropDownList obj = (DropDownList)sender;

            TabelleCombo.DataSource = null;
            _functionChanged = true;

            if (FunktionCombo.SelectedValue.ToString() == "OSP")
            {
                TabelleCombo.DataSource = _ospList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "OSP";
            }
            else if (FunktionCombo.SelectedValue.ToString() == "AE")
            {
                TabelleCombo.DataSource = _aeList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "AE";
            }
            else if (FunktionCombo.SelectedValue.ToString() == "IC")
            {
                TabelleCombo.DataSource = _icList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "IC";
            }

            TabelleCombo.DataBind();
            TabelleCombo.SelectedIndex = 0;
            SetCountText();
        }

        protected void TabelleCombo_SelectionChanged(object sender, System.EventArgs e)
        {
            DropDownList obj = (DropDownList)sender;
        }

        private void SetCountText(string text = "")
        {
            CountLbl.Text = TabelleCombo.SelectedValue.ToString();
        }

        public static List<ViewInfo> GetSprocList(string function, bool isAdmin, bool isSubAdmin, bool infoPanel = false)
        {
            List<ViewInfo> sl = new List<ViewInfo>();

            switch (function)
            {
                case "OSP":
                    sl.Add(new ViewInfo("First Meetings Set", "sp_Activity_OSP_First"));
                    sl.Add(new ViewInfo("Contacts", "sp_Activity_OSP_Contact"));                   
                    return sl;

                case "AE":                    
                    sl.Add(new ViewInfo("Dials", "sp_Activity_AE_Dials"));
                    sl.Add(new ViewInfo("Dials Daily", "sp_Activity_AE_Dials_Daily"));
                    return sl;

                case "IC":
                    sl.Add(new ViewInfo("Monthly Activity Callstatus", "sp_Activity_IC_CallStatus"));
                    return sl;                

                default:
                    sl.Add(new ViewInfo("First Meetings Set", "sp_Activity_OSP_First"));
                    sl.Add(new ViewInfo("Contacts", "sp_Activity_OSP_Contact"));
                    return sl;
            }
        }
    }

    public class ViewInfo
    {
        public string ViewName { get; set; }
        public string SprocName { get; set; }
        public string ExtraName { get; set; }
        public List<ViewInfo> ViewList { get; set; }

        public ViewInfo()
        {
            this.ViewName = "";
            this.SprocName = "";
        }

        public ViewInfo(string name, string sproc, string extraName = "")
        {
            this.ViewName = name;
            this.SprocName = sproc;
            this.ExtraName = extraName;
        }
    }
}

I appreciate any help on this.

Developer
  • 81
  • 1
  • 7
  • Try and use breakpoints to check if the event for the dropdownlist selected index changes fires. – Renato Afonso Oct 11 '17 at 10:53
  • Please show the errors if any –  Oct 11 '17 at 11:00
  • I just added the error to the description – Developer Oct 11 '17 at 11:04
  • Possible duplicate of [A control with ID could not be found for the trigger in UpdatePanel](https://stackoverflow.com/questions/6667398/a-control-with-id-could-not-be-found-for-the-trigger-in-updatepanel) –  Oct 11 '17 at 11:14
  • So when I use: trigger.ControlID = dd.ID; it works and finds the control, but it doesn't update the Dropdown. I also added HeadUpdatePanel.Update(); to the bottom of FunktionCombo_SelectionChanged, but still does nothing??? – Developer Oct 11 '17 at 11:37
  • OK, I found the issue, and I'm so sorry, as it was a stupid. When I applied the DataSource to the DDL, the list was null, as I forgot it will be deleted by postback. So I'm just creating the lists at runtime, and now it works. Again, sos sorry for that, but thanks for all the help! – Developer Oct 11 '17 at 12:00

1 Answers1

0

OK, I found the issue, and I'm so sorry, as it was so stupid. First issue: I deleted the trigger as it is not needed, since the DDL which triggers it, is inside the Updatepanel, which solved that issue.

Second issue: When I applied the DataSource to the DDL inside of FunktionCombo_SelectionChanged, the list was null, as I forgot it will be deleted by postback.

So I'm just creating the lists at runtime, and now it works.

_ospList = SprocEvaluator.GetSprocList("OSP", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
TabelleCombo.DataSource = _ospList;

Again, so sorry for that, but thanks for all the help!

Developer
  • 81
  • 1
  • 7