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
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.Text = lst_main[i].ToString();
chkmain.CssClass = "margin-right";
chkmain.Enabled = false;
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);
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);
}
#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]);
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 = (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;
}
if (thisIndex == -1)
{
CheckBox chk = (CheckBox)this.FindControl("ctl00").FindControl("MainContent").FindControl(checkedBox[2].Split('_')[1]);
chk.Checked = false;
}
}
}
What I have tried:
I tried with above sample code which working properly without master page.
but actual code must be with master page.