locked
Problem with Update panel when using it in Content page of master page RRS feed

  • Question

  • User-1857878686 posted

    Hi friends,

                   Im having a Problem with Update panel when im using it in Content page of master page.I have two dropdown list(category ddl and subcategory ddl ).I have placed update panel containing subcategory ddl and label in contentplaceholder of my content page.But category ddl is outside updatepanel in contentplaceholder of my content page.I want subcategory ddl to get populated when user select category ddl and also subcategory selected value must be shown in label. The problem im facing is that subcategory ddl only gets populated 1st time when category is selected for 1st time but not other time and also subcategory ddl value is not shown in label. Below is my code.Please help me.

    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
        </asp:ScriptManagerProxy>
        <asp:DropDownList ID="ddlcategory" runat="server"
                      CssClass="input_text"  AutoPostBack="true"
                        onselectedindexchanged="ddlcategory_SelectedIndexChanged"   
                DataSourceID="SqlDataSource1" DataTextField="category"
                            DataValueField="category"
                ondatabound="ddlcategory_DataBound"  >
                      
                    </asp:DropDownList>

                       <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                            ConnectionString="<%$ ConnectionStrings:etalaashConnectionString %>"
                            SelectCommand="SELECT DISTINCT [category] FROM [tbl_category]">
                      </asp:SqlDataSource>
                      
                       <asp:UpdatePanel ID="upComponent" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlsubcategory" runat="server" CssClass="input_text"
                            ondatabound="ddlsubcategory_DataBound" AutoPostBack="True"
                            onselectedindexchanged="ddlsubcategory_SelectedIndexChanged1" >
                        </asp:DropDownList>

                       
                        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="ddlcategory" EventName="SelectedIndexChanged" />
                         <asp:AsyncPostBackTrigger ControlID="ddlsubcategory" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>
        
    </asp:Content>

    CS Code:-

      protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = "SELECT DISTINCT subcategory FROM tbl_subcategory WHERE category ='" + ddlcategory.SelectedItem.Text + "'";
            SqlDataReader rd = a.GetDatareader(str);
            ddlsubcategory.DataSource = rd;
            ddlsubcategory.DataValueField = "subcategory";
            ddlsubcategory.DataTextField = "subcategory";
            ddlsubcategory.DataBind();
            rd.Close();
          
        }

       protected void ddlsubcategory_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Label1.Text = ddlsubcategory.SelectedItem.Text;    

         }
        protected void ddlcategory_DataBound(object sender, EventArgs e)
        {
            ddlcategory.Items.Insert(0, "Select category");
        }
        protected void ddlsubcategory_DataBound(object sender, EventArgs e)
        {
            ddlsubcategory.Items.Insert(0, "Select Subcategory");
        }

    Friday, October 11, 2013 11:51 AM

Answers

  • User-417640953 posted

    Hi,

    Thanks for posting this issue to asp.net forum.

    According to your description and code provided, I see that you want to complete the two dropdownlist interaction using ajax control.

    The provided code looks fine to me. I made a demo based on your code.

    Master page:

    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            Master Page<br />
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> 
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    

    Aspx:

    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            Master Page<br />
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> 
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    
    Aspx:
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"></asp:ScriptManagerProxy>
                  <asp:DropDownList ID="ddlcategory" runat="server"   AutoPostBack="true" 
                         onselectedindexchanged="ddlcategory_SelectedIndexChanged" DataTextField="category"   
                         DataValueField="category" 
                         ondatabound="ddlcategory_DataBound"  > 
                  </asp:DropDownList>  
                  <asp:UpdatePanel ID="upComponent" runat="server" UpdateMode="Conditional">
                  <ContentTemplate>
                         <asp:DropDownList ID="ddlsubcategory" runat="server" CssClass="input_text" 
                             ondatabound="ddlsubcategory_DataBound" AutoPostBack="True" 
                             onselectedindexchanged="ddlsubcategory_SelectedIndexChanged1" >
                         </asp:DropDownList>
    
                        
                         <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    
                   </ContentTemplate>
                   <Triggers>
                         <asp:AsyncPostBackTrigger ControlID="ddlcategory" EventName="SelectedIndexChanged" />
                         <asp:AsyncPostBackTrigger ControlID="ddlsubcategory" EventName="SelectedIndexChanged" />
                   </Triggers>
                 </asp:UpdatePanel>
    </asp:Content>
    

    Code behind:

    static Dictionary<string, string> dic =null;
            static Dictionary<string, string> subdic = null;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
                    dic = new Dictionary<string, string>();
                    dic.Add("1","One");
                    dic.Add("2","Two");  
                    this.ddlcategory.DataSource = dic;
                    ddlcategory.DataValueField = "Key";
                    ddlcategory.DataTextField = "Value";
                    ddlcategory.DataBind();
                }
            }
            protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
            {
                subdic = new Dictionary<string, string>();
                if (this.ddlcategory.SelectedValue == "1")
                {
    
                    subdic.Add("1", "OneSub1");
                    subdic.Add("2", "OneSub2");
    
    
                }
                else {
    
                    subdic.Add("3", "TwoSub1");
                    subdic.Add("4", "TwoSub2");
                }
                ddlsubcategory.DataSource = subdic;
                ddlsubcategory.DataValueField = "key";
                ddlsubcategory.DataTextField = "value";
                ddlsubcategory.DataBind(); 
            }
    
            protected void ddlsubcategory_SelectedIndexChanged1(object sender, EventArgs e)
            {
                Label1.Text = ddlsubcategory.SelectedItem.Text;
    
            }
            protected void ddlcategory_DataBound(object sender, EventArgs e)
            {
                ddlcategory.Items.Insert(0, "Select category");
            }
            protected void ddlsubcategory_DataBound(object sender, EventArgs e)
            {
                ddlsubcategory.Items.Insert(0, "Select Subcategory");
            }
    

    It works fine in my workaround, please find something different. Thanks.

     

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 13, 2013 11:30 PM