locked
cascade dropdownlist not working in update panel RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I am using 2 cascading dropdownlist with a label to show a value (within a contentplaceholder i.e. using master page). All is working fine. On selecting ddlLocation it is binding ddlProjects and after selecting ddlProjects item it is showing related value in txtClient. ()

    But when I tried to put those in a update panel to get rid of the whole page post back, then those don't work. After selecting one item in first dropdown list it doesn't refresh the second one.

    Below is my aspx page.

      <asp:UpdatePanel ID="Updatepanel1" runat="server">
                                <ContentTemplate>                              
                                    
                                                <asp:DropDownList ID="ddlLocation" runat="server" Width="150px" DataTextField="Location" DataValueField="Location" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AppendDataBoundItems="True" AutoPostBack="True">
                                                    <asp:ListItem>-Select</asp:ListItem>
                                                </asp:DropDownList>
    
    
                                                <asp:DropDownList ID="ddlProjects" runat="server" Width="150px" OnSelectedIndexChanged="ddlProjects_SelectedIndexChanged" AppendDataBoundItems="True" DataTextField="Project_Name" DataValueField="Project_Id">
                                                    <asp:ListItem Value="0">-Select-</asp:ListItem>
    
                                                </asp:DropDownList>
                                                <asp:Label ID="txtClient" runat="server"></asp:Label>
    
    </ContentTemplate>  
     </asp:UpdatePanel

    Why it is not working.

    Please suggest.

    Saturday, April 14, 2018 10:36 AM

Answers

  • User475983607 posted

    Why it is not working.

    I can only guess that you have logical bugs in the code behind.  

    Please suggest.

    I suggest that you try basic troubleshooting.

    Start by reading the UpdatePanel reference documentation  Use the Visual Studio debugger to set a breakpoint and single step through the code behind logic to verify the code is functioning as expected.   

    Working Demo

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppDemo._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                    <asp:DropDownList ID="ColorType" runat="server" OnSelectedIndexChanged="ColorType_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem text="Select" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Primary Colors" Value="1"></asp:ListItem>
                        <asp:ListItem  Text="Seconday Colors" Value="2"></asp:ListItem>
                    </asp:DropDownList>
    
                <asp:DropDownList ID="Color" runat="server"></asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebAppDemo
    {
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void ColorType_SelectedIndexChanged(object sender, EventArgs e)
            {
                Color.DataSource = GetColorsById(ColorType.SelectedValue);
                Color.DataBind();
            }
    
            public List<ListItem> GetColorsById(string id)
            {
                if (id == "1")
                {
                    return new List<ListItem>()
                    {
                        new ListItem()
                        {
                            Text = "Select",
                            Value = "0",
                            Selected = true
                        },
                        new ListItem()
                        {
                            Text = "Red",
                            Value = "1"
                        },
                        new ListItem()
                        {
                            Text = "Yellow",
                            Value = "2"
                        },
                        new ListItem()
                        {
                            Text = "Blue",
                            Value = "3"
                        }
                    };
                }
    
                if (id == "2")
                {
                    return new List<ListItem>()
                    {
                        new ListItem()
                        {
                            Text = "Select",
                            Value = "0",
                            Selected = true
                        },
                        new ListItem()
                        {
                            Text = "Orange",
                            Value = "4"
                        },
                        new ListItem()
                        {
                            Text = "Purple",
                            Value = "5"
                        },
                        new ListItem()
                        {
                            Text = "Green",
                            Value = "6"
                        }
                    };
                }
    
                return new List<ListItem>();
            }
    
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 14, 2018 11:44 AM
  • User-1838255255 posted

    Hi demoninside9,

    According to your description and code, if you use the scriptmanager before updatepanel? I make a sample in my side, it works fine, please check:

    Sample Code: 

    Add the following code in the master page:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    Children Page: 

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="ddlLocation" runat="server" Width="150px" DataTextField="Location" DataValueField="Location" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AppendDataBoundItems="True" AutoPostBack="True">
                        <asp:ListItem>-Select</asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="ddlProjects" runat="server" Width="150px" OnSelectedIndexChanged="ddlProjects_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="True" DataTextField="Project_Name" DataValueField="Project_Id">
                        <asp:ListItem Value="0">-Select-</asp:ListItem>
                    </asp:DropDownList>
                    <asp:Label ID="txtClient" runat="server"></asp:Label>
    
    
                </ContentTemplate>
            </asp:UpdatePanel>
    </asp:Content>
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                    conn.Open();
                    string sql = "select  distinct Country from Data";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataReader sdr = command.ExecuteReader();
                    ddlLocation.DataSource = sdr;
                    ddlLocation.DataTextField = "Country";
                    ddlLocation.DataValueField = "Country";
                    ddlLocation.DataBind();
                    sdr.Close();
                    conn.Close();
                }
            }
    
            protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.ddlProjects.Items.Clear();
                string value = this.ddlLocation.SelectedValue;
                SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                conn.Open();
                SqlCommand command = new SqlCommand("select City from Data where Country= '" + value + "'", conn);
                SqlDataReader sdr = command.ExecuteReader();
                ddlProjects.DataSource = sdr;
                ddlProjects.DataTextField = "City";
                ddlProjects.DataValueField = "City";
                ddlProjects.DataBind();
                sdr.Close();
                conn.Close();
    
            }
    
            protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.txtClient.Text = ddlProjects.SelectedValue;
    
            }
    

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2018 10:01 AM

All replies

  • User475983607 posted

    Why it is not working.

    I can only guess that you have logical bugs in the code behind.  

    Please suggest.

    I suggest that you try basic troubleshooting.

    Start by reading the UpdatePanel reference documentation  Use the Visual Studio debugger to set a breakpoint and single step through the code behind logic to verify the code is functioning as expected.   

    Working Demo

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppDemo._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                    <asp:DropDownList ID="ColorType" runat="server" OnSelectedIndexChanged="ColorType_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem text="Select" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Primary Colors" Value="1"></asp:ListItem>
                        <asp:ListItem  Text="Seconday Colors" Value="2"></asp:ListItem>
                    </asp:DropDownList>
    
                <asp:DropDownList ID="Color" runat="server"></asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebAppDemo
    {
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void ColorType_SelectedIndexChanged(object sender, EventArgs e)
            {
                Color.DataSource = GetColorsById(ColorType.SelectedValue);
                Color.DataBind();
            }
    
            public List<ListItem> GetColorsById(string id)
            {
                if (id == "1")
                {
                    return new List<ListItem>()
                    {
                        new ListItem()
                        {
                            Text = "Select",
                            Value = "0",
                            Selected = true
                        },
                        new ListItem()
                        {
                            Text = "Red",
                            Value = "1"
                        },
                        new ListItem()
                        {
                            Text = "Yellow",
                            Value = "2"
                        },
                        new ListItem()
                        {
                            Text = "Blue",
                            Value = "3"
                        }
                    };
                }
    
                if (id == "2")
                {
                    return new List<ListItem>()
                    {
                        new ListItem()
                        {
                            Text = "Select",
                            Value = "0",
                            Selected = true
                        },
                        new ListItem()
                        {
                            Text = "Orange",
                            Value = "4"
                        },
                        new ListItem()
                        {
                            Text = "Purple",
                            Value = "5"
                        },
                        new ListItem()
                        {
                            Text = "Green",
                            Value = "6"
                        }
                    };
                }
    
                return new List<ListItem>();
            }
    
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 14, 2018 11:44 AM
  • User-1716253493 posted

    AFAIK You need updatepanel trigger, 

                <asp:DropDownList ID="ddlLocation" runat="server" Width="150px" DataTextField="Location" 
                    DataValueField="Location" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AppendDataBoundItems="True" AutoPostBack="True">
                    <asp:ListItem>-Select</asp:ListItem>
                </asp:DropDownList>
                 <asp:UpdatePanel ID="Updatepanel1" runat="server">
                     <ContentTemplate> 
                         <asp:DropDownList ID="ddlProjects" runat="server" Width="150px" OnSelectedIndexChanged="ddlProjects_SelectedIndexChanged" 
                             AppendDataBoundItems="True" DataTextField="Project_Name" DataValueField="Project_Id">
                             <asp:ListItem Value="0">-Select-</asp:ListItem>
                         </asp:DropDownList>
                         <asp:Label ID="txtClient" runat="server"></asp:Label>
                     </ContentTemplate>  
                     <Triggers>
                         <asp:AsyncPostBackTrigger ControlID="ddlLocation" />
                     </Triggers>
                 </asp:UpdatePanel>

    Saturday, April 14, 2018 11:46 AM
  • User-1838255255 posted

    Hi demoninside9,

    According to your description and code, if you use the scriptmanager before updatepanel? I make a sample in my side, it works fine, please check:

    Sample Code: 

    Add the following code in the master page:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    Children Page: 

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="ddlLocation" runat="server" Width="150px" DataTextField="Location" DataValueField="Location" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AppendDataBoundItems="True" AutoPostBack="True">
                        <asp:ListItem>-Select</asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="ddlProjects" runat="server" Width="150px" OnSelectedIndexChanged="ddlProjects_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="True" DataTextField="Project_Name" DataValueField="Project_Id">
                        <asp:ListItem Value="0">-Select-</asp:ListItem>
                    </asp:DropDownList>
                    <asp:Label ID="txtClient" runat="server"></asp:Label>
    
    
                </ContentTemplate>
            </asp:UpdatePanel>
    </asp:Content>
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                    conn.Open();
                    string sql = "select  distinct Country from Data";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataReader sdr = command.ExecuteReader();
                    ddlLocation.DataSource = sdr;
                    ddlLocation.DataTextField = "Country";
                    ddlLocation.DataValueField = "Country";
                    ddlLocation.DataBind();
                    sdr.Close();
                    conn.Close();
                }
            }
    
            protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.ddlProjects.Items.Clear();
                string value = this.ddlLocation.SelectedValue;
                SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                conn.Open();
                SqlCommand command = new SqlCommand("select City from Data where Country= '" + value + "'", conn);
                SqlDataReader sdr = command.ExecuteReader();
                ddlProjects.DataSource = sdr;
                ddlProjects.DataTextField = "City";
                ddlProjects.DataValueField = "City";
                ddlProjects.DataBind();
                sdr.Close();
                conn.Close();
    
            }
    
            protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.txtClient.Text = ddlProjects.SelectedValue;
    
            }
    

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2018 10:01 AM