Tuesday, 12 December 2017

Multilevel CheckBoxes



http://www.lidorsystems.com/support/articles/treeview-threestate.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function myPostBack() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem Text="a" Value="a" />
                        <asp:ListItem Text="b" Value="b" />
                        <asp:ListItem Text="c" Value="c" />
                        <asp:ListItem Text="d" Value="d" />
                        <asp:ListItem Text="e" Value="e" />
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Remove" OnClick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:TreeView ID="TreeView1" runat="server"  OnClick="myPostBack();" ShowCheckBoxes="All" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
                    </asp:TreeView>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
public partial class listbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TreeView1.Nodes.Add(new TreeNode("Fruits", "Fruits"));
                TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Mango", "Mango"));
                TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Apple", "Apple"));
                TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Pineapple", "Pineapple"));
                TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Orange", "Orange"));
                TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Grapes", "Grapes"));
                TreeView1.Nodes.Add(new TreeNode("Vegetables", "Vegetables"));
                TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Carrot", "Carrot"));
                TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Cauliflower", "Cauliflower"));
                TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Potato", "Potato"));
                TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Tomato", "Tomato"));
                TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Onion", "Onion"));
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox1.Items.Add(DropDownList1.SelectedValue);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            List<ListItem> deletedItems = new List<ListItem>();
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Selected)
                {
                    deletedItems.Add(item);
                }
            }
            if(deletedItems.Count>0)
            {
                foreach (ListItem item in deletedItems)
                {
                    ListBox1.Items.Remove(item);
                }
            }
            else
            {
                Response.Write("Please select an Item to delete");
            }
           
        }
        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            if (e != null && e.Node != null)
            {
                if (e.Node.ChildNodes.Count > 0)
                {
                    CheckTreeNodeRecursive(e.Node, e.Node.Checked);
                }
            }
        }
        private void CheckTreeNodeRecursive(TreeNode parent, bool fCheck)
        {
            foreach (TreeNode child in parent.ChildNodes)
            {
                if (child.Checked != fCheck)
                {
                    child.Checked = fCheck;
                }
                if (child.ChildNodes.Count > 0)
                {
                    CheckTreeNodeRecursive(child, fCheck);
                }
            }
        }
    }

Xml and webconfig file handling

<div dir="ltr" style="text-align: left;" trbidi="on">
<form id="form1" runat="server">
<script>
          
        </script>
    <br />
<div>
Text="&lt;% $Resources: MapRes, global_productName %&gt;"
        Text="&lt;%$  appsettings: gridPageSize %&gt;"
        <asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist>
    </div>
<br />
Text ='&lt;%# String.Format(HttpContext.GetGlobalResourceObject("MapRes", "parameterized_resourceKey").ToString(), "Every One") %&gt;'
  
        <asp:button id="Button1" onclick="Button1_Click" runat="server" text="Save">
        <asp:button id="SetSession" onclick="SetSession_Click" runat="server" text="SetSession">
        <asp:button id="EndSession" onclick="EndSession_Click" runat="server" text="EndSession">
    </asp:button></asp:button></asp:button></form>
public enum ConfigSettingName
    {
        mailConfig = 1,
        gridViewConfig
    }
    public enum ConfigAttribute
    {
        enableOutGoingMail=1,
        gridPageSize
    }
    public enum GlobalOption
    {
        paymentOption=1,
        statusOption,
        gridPageSizeOption
    }
    public enum AppSettingKey
    {
        enableMailSending=1,
        gridPageSize
    }


public partial class ResourcePlaying : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.DataBind();
                bind_ddl1();
                DateTime dt1 = new DateTime(1970, 01, 01);
                DateTime dt2 = DateTime.Now;
                Label3.Text = Convert.ToString((dt1 - dt2).TotalMilliseconds);
                Label3.Text = Label3.Text.Replace(".", "Hi");
            }
        }
        public void bind_ddl1()
        {
            //XmlDocument xdoc = new XmlDocument();
            //xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            //string PreferenceXML = xdoc.InnerXml;
            //XmlNode paymentNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList PaymentOption = paymentNode.SelectNodes("tab[@name='paymentOption']/option");
            //XmlNode statusNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList statusOption = statusNode.SelectNodes("tab[@name='statusOption']/option");
            XmlNodeList statusOption = GetOptionNodeList(nameof(GlobalOption.gridPageSizeOption));
            DropDownList1.Items.Clear();
            foreach (XmlNode xnode in statusOption)
            {
                string name = xnode.Attributes["name"].Value;
                string value = xnode.Attributes["value"].Value;
                ListItem i = null;
                i = new ListItem(name, value);
                DropDownList1.Items.Add(i);
            }
            //DropDownList1.SelectedValue = GetConfigValueFromXML(nameof(ConfigSettingName.gridViewConfig),
            //                                                    nameof(ConfigAttribute.gridPageSize));
            DropDownList1.SelectedValue = GetAppSettingValue(nameof(AppSettingKey.gridPageSize));
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. Updating value in xml file
                // SetValueInXMLConfiguration(ConfigSettingName.gridViewConfig.ToString(),
                //                ConfigAttribute.gridPageSize.ToString(),
                //                DropDownList1.SelectedValue);
                // 2. Updating value in webconfig appsetting
                SetAppSettingValue(nameof(AppSettingKey.gridPageSize), DropDownList1.SelectedValue);
                if (IsMailingEnable())
                    Response.Write("Mail feature is enabled");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString()); 
            }
        }
        private bool IsMailingEnable()
        {
            string val = string.Empty;
            //1. reading value from xml file
            val = GetConfigValueFromXML(nameof(ConfigSettingName.mailConfig), nameof(ConfigAttribute.enableOutGoingMail));
            //2. reading value from web.config file
            val = GetAppSettingValue(nameof(AppSettingKey.enableMailSending));
            bool staus = val == "1" ? true : false;
            return staus;
        }
        private void SetValueInXMLConfiguration(string settingName,string attributeName, string value, string fileType = "XML")
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("xml") + "\\default.xml");
                string PreferenceXML = xdoc.InnerXml;
                //XmlNode mailConfig = xdoc.SelectSingleNode("globalOption/tab[@name='mailConfig']/setting");
                XmlElement xmlElement = (XmlElement)xdoc.SelectSingleNode("globalOption/tab[@name='configuration']/setting[@name='"+ settingName + "']");
                //XmlNode a = mailConfig.SelectSingleNode("setting/@id='1']");           
                if (xmlElement != null)
                {
                    //formData.SetAttribute("allowOutGoingMail", "1"); // Set to new value.
                    //formData.Attributes["enableOutGoingMail"].Value = value;
                    xmlElement.Attributes[attributeName].Value = value;
                    //Response.Write(xmlElement.Attributes[attributeName].Value);
                }
                xdoc.Save(Server.MapPath("xml") + "\\default.xml");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        private string GetConfigValueFromXML(string settingName, string attributeName)
        {
            string attributeValue = string.Empty;
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            string PreferenceXML = xdoc.InnerXml;
            XmlElement xmlElement = (XmlElement)xdoc.SelectSingleNode("globalOption/tab[@name='configuration']/setting[@name='" + settingName + "']");
            if (xmlElement != null)
                attributeValue = xmlElement.Attributes[attributeName].Value;
            return attributeValue;
        }
       
        public string GetAppSettingValue(string key)
        {
            string value = string.Empty;
            value= ConfigurationManager.AppSettings[key];
            return value;
        }
        public void SetAppSettingValue(string key, string newValue, string fileType="WebConfig")
        {
           
            //Helps to open the Root level web.config file.
            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            //Modifying the AppKey from AppValue to AppValue1
            webConfigApp.AppSettings.Settings[key].Value = newValue;
            //Save the Modified settings of AppSettings.
            webConfigApp.Save();
            //set value in aspx page directly
            //&lt;%$  appsettings: *KeyName * %&gt;
            //="&lt;%$ ConfigurationSettings.AppSettings["ImagePath"] %&gt;"
        }
        private XmlNodeList GetOptionNodeList(string optionField)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            string PreferenceXML = xdoc.InnerXml;
            XmlNode node = xdoc.SelectSingleNode("globalOption");
            XmlNodeList nodelist = node.SelectNodes("tab[@name='" + optionField + "']/option");
            //XmlNode statusNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList statusOption = statusNode.SelectNodes("tab[@name='statusOption']/option");
            return nodelist;
        }
        protected void SetSession_Click(object sender, EventArgs e)
        {
            Session["IsUserValid"] = "Avi";
            ErrorReporting.Log.Info("Session Started: Logged form login page", "aspx page", "SetSession_Click");
        }
        protected void EndSession_Click(object sender, EventArgs e)
        {
            ErrorReporting.Log.Info("Logged form asax Session_End", "aspx page", "EndSession_Click");
            Session.Remove("IsUserValid");
            Session.RemoveAll();
            Response.Redirect("listbox.aspx");
        }
    }

default.xml
<globaloption>
  <tab id="1" name="paymentOption">
    <option name="Cash" value="Cash">
    <option name="Online" value="Online">
  </option></option></tab>
  <tab id="2" name="configuration">
    <setting enableoutgoingmail="1" name="mailConfig">
    <setting gridpagesize="150" name="gridViewConfig">
  </setting></setting></tab>
  <tab id="3" name="statusOption">
    <option name="Enable" value="1">
    <option name="Disable" value="0">
  </option></option></tab>
  <tab id="4" name="gridPageSizeOption">
    <option name="10" value="10">
    <option name="20" value="20">
    <option name="30" value="30">
    <option name="40" value="40">
    <option name="50" value="50">
    <option name="100" value="100">
    <option name="150" value="150">
    <option name="200" value="200">
  </option></option></option></option></option></option></option></option></tab>
</globaloption>

webconfig
<configuration>
 <appsettings>
    <add key="enableMailSending" value="1">
    <add key="gridPageSize" value="20">
  </add></add></appsettings>
</configuration></div>