Click here to Skip to main content
14,300,422 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello experts,
I have a dropdown in which i am adding checkboxlist at code behind.
when I checked any item from checkbox list, dropdown hide automatically. The same code is working properly without master page.
I need your urgent help.
Master Page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <asp:ContentPlaceHolder id="head" runat="server">
        
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    
    <div><h1>Hello World</h1></div>
    <div>
    
            <asp:ContentPlaceHolder id="MainContent" runat="server">
        
            </asp:ContentPlaceHolder>
        
    </div>
    </form>
</body>
</html>


Content Page.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<link href="bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                                <ContentTemplate>
                                    <div class="dropdown">
                                        <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
                                            Dropdown Example
                                <span class="caret"></span>
                                        </button>
                                        <asp:Panel class="dropdown-menu" ID="pnlList" runat="server" Style="overflow-x: hidden; overflow-y: scroll; height: 225px" OnInit="pnlList_Init"></asp:Panel>
                                    </div>
                                </ContentTemplate>
                            </asp:UpdatePanel>
</div>
</asp:Content>


Content Page.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Panel pnl;
    string[] lst_main = { "A", "B", "C", "D", "E"};
    string[] lst_child = { "1", "2", "3", "4", "5" };
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void pnlList_Init(object sender, EventArgs e)
    {
        #region Generate Checkbox
        //if (IsPostBack)
        //{
        for (int i = 0; i < lst_main.Count(); i++)
        {
            pnl = new Panel();
            pnl.CssClass = "pnl-mrgn";
            CheckBox chkmain = new CheckBox();
            chkmain.ID = lst_main[i].ToString();
            //chkmain.CheckedChanged += new System.EventHandler(chkmain_CheckedChanged);
            chkmain.Text = lst_main[i].ToString();
            chkmain.CssClass = "margin-right";
            chkmain.Enabled = false;
            //chkmain.AutoPostBack = true;
            //chkmain.CheckedChanged += (obj, args) =>
            //{
            //    if (chkmain.Checked.Equals(true))
            //    {
            //        txtvalue.Text += chkmain.Text;
            //    }
            //};
            pnl.Controls.Add(chkmain);
            CheckBoxList chkchild = new CheckBoxList();
            chkchild.ID = "chkList_" + lst_main[i].ToString();
            chkchild.RepeatDirection = RepeatDirection.Horizontal;
            chkchild.AutoPostBack = true;
            chkchild.SelectedIndexChanged += new System.EventHandler(chkchild_SelectedIndexChanged);
            //chkchild.SelectedIndexChanged += (obj, args) =>
            //{
            //    if (chkchild.SelectedItem != null)
            //        txtvalue.Text += chkchild.SelectedItem.Value;
            //};
            for (int j = 0; j < lst_child.Count(); j++)
            {

                System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem();
                li.Text = lst_child[j].ToString();
                chkchild.Items.Add(li);
                pnl.Controls.Add(chkchild);
            }

            pnlList.Controls.Add(pnl);
            //pnlList.ContentTemplateContainer.Controls.Add(pnl);
        }
        //}
        #endregion
    }
    protected void chkchild_SelectedIndexChanged(object sender, EventArgs e)
    {
        string result = Request.Form["__EVENTTARGET"];
        string[] checkedBox = result.Split('$');
        int index = int.Parse(checkedBox[checkedBox.Length - 1]);
        //int thisIndex = 0;
        CheckBoxList objCheckBoxList = sender as CheckBoxList;
        int thisIndex = objCheckBoxList.SelectedIndex;

        string lsSelectdVal = "";

        if (index >= 0 && index < 4 && objCheckBoxList.Items[index].Selected)
        {
            for (int i = index; i <= 4; i++)
            {
                objCheckBoxList.Items[i].Selected = true;
                lsSelectdVal += checkedBox[2].Split('_')[1] + ":" + (i + 1) + ",";
            }
            //CheckBox chk = FindControl(checkedBox[2].Split('_')[1]) as CheckBox;
            CheckBox chk = (CheckBox)this.FindControl("ctl00").FindControl("MainContent").FindControl(checkedBox[2].Split('_')[1]);
            chk.Checked = true;
        }
        if (index == 4 && objCheckBoxList.Items[index].Selected)
        {
            CheckBox chk = (CheckBox)this.FindControl("ctl00").FindControl("MainContent").FindControl(checkedBox[2].Split('_')[1]);
            chk.Checked = true;

            //else
            //{
            //    CheckBox chk = (CheckBox)this.FindControl("pnlList").FindControl(checkedBox[0].Split('_')[1]);
            //    chk.Checked = false;
            //}
        }

        if (thisIndex == -1)
        {
            CheckBox chk = (CheckBox)this.FindControl("ctl00").FindControl("MainContent").FindControl(checkedBox[2].Split('_')[1]);
            chk.Checked = false;
        }
        //((UpdatePanel)this.FindControl("ctl00").FindControl("MainContent").FindControl("UpdatePanel1")).Update();

        //objCheckBoxList.Items[0].Selected = true;
        //ListItem objListItem;
        //string selectedItems = string.Empty;
        //foreach (ListItem listitem in objCheckBoxList.Items)
        //{
        //    if (listitem.Selected)
        //    {
        //        thisIndex = objCheckBoxList.Items.IndexOf(listitem);               
        //    }
        //}

        //Label1.Text = lsSelectdVal;
    }
}


What I have tried:

I tried with above sample code which working properly without master page.
but actual code must be with master page.
Posted
Updated 5 days ago
Comments
Gerry Schmitz 9-Sep-19 15:08pm
   
I thought "content" was for the "form", not the master.
KManishS 10-Sep-19 1:46am
   
what to do now to achieve this?
F-ES Sitecore 10-Sep-19 6:38am
   
Step through the code and see how it behaves differently on a master page. ie is a line throwing an exception that it didn't before, is a call to FindControl returning null when it returned an object before and so on.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

just use the below for your master

@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
  
</head>
<body>
    <form id="form1" runat="server">
    
    <div><h1>Hello World</h1></div>
    <div>
 
        
    </div>
    </form>
</body>
</html>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100