0 Replies - 1052 Views - Last Post: 03 May 2013 - 11:51 AM Rate Topic: -----

#1 ASP-Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-May 13

How do I ad an if statement that refrences a product li an xml file...

Posted 03 May 2013 - 11:51 AM

To start off with... I have never coded in ASP. I am like a fish out of water when it comes to this type of code. I have, some how, been able to avoid it in my career, however I am now faced with a task that requires that I make a change to the functionality of a page. I need to make a check box defined as select-able control 4 other check boxes that are not select-able.

Basically, if you check one box it automatically selects 4 other boxes and if you un-select it, the other checks go away.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using COMPANYNAME.Pricing;

public partial class _assets_controls_PricingTable : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        tblPricing.Style.Add("margin", "0 auto");
        if (CheckBoxHash == null)
            CheckBoxHash = new Dictionary<object, Product>();
        BuildTable();
    } //end f()

    /// <summary>
    /// Recalculate the values based on the number of users.
    /// </summary>
    protected void btnGetEstimate_Click(System.Object sender, System.EventArgs e)
    {
        int number = 0;
        Int32.TryParse(txtNumberOfUsers.Text, out number);
        NumberOfUsers = number;

        //if "All available modules" is checked when we get the estimate, loop through everything and "Select" anything that is selectable.
        if (rbAllModules.Checked)
        {
            foreach (Package package in Packages)
            {
                foreach (Product product in package.Products)
                {
                    if (product.Selectable)
                        product.Selected = true;
                } //end foreach
            } //end foreach
        } //end if
        BuildTable();
    } //end f()

    /// <summary>
    /// Clear the screen.
    /// </summary>
	
    protected void btnClear_Click(System.Object sender, System.EventArgs e)
    {
        //Store the indexes of the expanded packages so expanded packages don't collapse after rebuilding the table.
        List<string> expancedPackages = new List<string>();
        for (int i = 0; i < Packages.Count; i++)
        {			
            if (Packages[i].Expanded)
                expancedPackages.Add(Packages[i].Name);
        } //end for		
        //Reset the packages information.
        Packages = PackageCollection.LoadFromXML(Server.MapPath("~\\App_Data\\Packages.xml"));
        txtNumberOfUsers.Text = String.Empty;
        NumberOfUsers = -1;
        rbCoreModule.Checked = true;
        //Before rebuilding the table, re-expand packages.
        foreach (string name in expancedPackages)
        {
            Package package = Packages[name];
            if (package != null)
            {
                package.Expanded = true;
            } //end if
        } //end foreach
        BuildTable();
    } //end f()
    /// <summary>
    /// Switch a package's "Expanded" property and update the link button's text, if need be.
    /// </summary>
	
    protected void LinkButton_Click(System.Object sender, System.EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        if (lb == null)
            return;
        Package package = Packages[lb.CommandArgument];
        if (package == null)
            return;
        package.Expanded = !package.Expanded;
        BuildTable();
    } //end f()

    /// <summary>
    /// Toggle a products "selected" value.
    /// </summary>
    protected void CheckBox_CheckChanged(System.Object sender, System.EventArgs e)
    {
        Product product = CheckBoxHash[sender];
        if (product != null)
        {
            product.Selected = !product.Selected;
            BuildTable();
        } //end if
    } //end f()

    /// <summary>
    /// Uses a Package to create controls for displaying package information and
    /// dropping it down to display product information.
    /// </summary>
    /// <param name="package">The package from which to build the controls.</param>
    protected void BuildPackageInformation(Package package)
    {
        //Build a link button that will "expand" a category node.
        string lbText = "[+] More";
        if (package.Expanded == true)
            lbText = "[-] Less";
        LinkButton lb = new LinkButton();
        lb.Text = lbText;
        lb.CssClass = "superscript";
        lb.CommandArgument = package.Name;
        lb.Click += new EventHandler(LinkButton_Click);
        lb.ID = package.Name + "LB";
       
        //Create a label with the Product's name.
        Label lbl = new Label();
        lbl.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + package.Name;
        if (!String.IsNullOrEmpty(package.Note))
            lbl.Text += " (" + package.Note + ")";
        lbl.Style.Add("font-weight", "bold");
        lbl.ID = package.Name + "LBL";

        //Create a text-box to display the total of all products selected.
        TextBox tb = new TextBox();
        tb.Enabled = false;
        tb.CssClass = "packagetextbox";
        if (NumberOfUsers > -1)
            tb.Text = package.TotalString(NumberOfUsers);
        tb.ID = package.Name + "TB";

        //Create a table cell for the link button.
        TableCell lbCell = new TableCell();
        //lbCell.Style.Add("padding-right", "25px");
        lbCell.Style.Add("width", "465px");
        lbCell.Style.Add("text-align", "left");
        lbCell.Controls.Add(lb);
        lbCell.Controls.Add(lbl);
        lbCell.ID = package.Name + "lbcell";

        //Create a table cell for the TextBox.
        TableCell tbCell = new TableCell();
        tbCell.Controls.Add(tb);
        tbCell.Style.Add("align", "center");
        tbCell.ColumnSpan = 2;
        tbCell.ID = package.Name + "tbcell";

        //Crerate a table row, add the cells, then add the row to the table.
        TableRow tr = new TableRow();
        tr.Width = 250;
        tr.Cells.AddRange(new TableCell[] { lbCell, tbCell });
        tr.ID = package.Name + "tr";
        tblPricing.Rows.Add(tr);

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = lb.ID;
        trigger.EventName = "Click";
        upPricingTable.Triggers.Add(trigger);
        //If the Package is expanded, draw the products.
        if (package.Expanded)
        {
            foreach (Product product in package.Products)
            {
                BuildProductInformation(product);
            } //end foreach
        } //end if
    } //end f()

    /// <summary>
    /// Build a table row representing the information on a product or service.
    /// </summary>
    /// <param name="product"></param>
    protected void BuildProductInformation(Product product)
    {
        //Create a hyperlink for the product
        HyperLink hlProd = new HyperLink();
        hlProd.ID = product.Name + "hl";
        hlProd.CssClass = "nolinkurl";
        hlProd.Text = product.Name;
        if (!String.IsNullOrEmpty(product.LinkURL))
        {
            hlProd.CssClass = "priceslink";
            hlProd.NavigateUrl = product.LinkURL;
        } //end if

        //Create the checkbox for selecting the product.
        CheckBox cbProd = new CheckBox();
        cbProd.Checked = product.Selected;
        cbProd.Enabled = product.Selectable;
        cbProd.ID = product.Name + "CB";
        cbProd.AutoPostBack = true;
        CheckBoxHash.Add(cbProd, product);
        cbProd.CheckedChanged += new EventHandler(CheckBox_CheckChanged);

        //Create the textbox for displaying the price.
        TextBox tbProd = new TextBox();
        tbProd.Enabled = false;
        tbProd.CssClass = "producttextbox";
        if (NumberOfUsers > -1)
            tbProd.Text = product.TotalString(NumberOfUsers);
        tbProd.ID = product.Name + "TB";
        
        //Create the cell for displaying the literal.
        TableCell ltlCell = new TableCell();
        ltlCell.Controls.Add(hlProd);
        if (product.HasLegend())
            ltlCell.Controls.Add(BuildLegendLiteral(product.Legend));
        ltlCell.ID = product.Name + "ltlCell";
        ltlCell.Style.Add("text-align", "left");

        //Create the cell for displaying the check box
        TableCell cbCell = new TableCell();
        cbCell.Controls.Add(cbProd);
        cbCell.ID = product.Name + "cbCell";

        //Create the cell for displaying the text box
        TableCell tbCell = new TableCell();
        tbCell.Controls.Add(tbProd);
        tbCell.ID = product.Name + "tbCell";

        //Crerate a table row, add the cells, then add the row to the table.
        TableRow tr = new TableRow();
        tr.CssClass = "productrow";
        tr.Cells.AddRange(new TableCell[] { ltlCell, cbCell, tbCell });
        tr.ID = product.Name + "tr";
        tblPricing.Rows.Add(tr);

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = cbProd.ID;
        trigger.EventName = "CheckedChanged";
        upPricingTable.Triggers.Add(trigger);

        //Build any products associated with this one.
        foreach (Product item in product.Products)
        {
            BuildProductInformation(item);
        } //end foreach
    } //end f()

    /// <summary>
    /// Build a asp:Literal whose text will display a legend with a tooltip when its hovered over.
    /// </summary>
    /// <param name="legend">The legend from which to build the literal.</param>
    /// <returns>A literal built to display a legend.</returns>
    protected Literal BuildLegendLiteral(Legend legend)
    {
        Literal ltlLegend = new Literal();
        ltlLegend.Text = "<a class=\"legend\" href=\"#\">" + legend.Display;
        ltlLegend.Text += "<span>" + legend.Description + "</span></a>";
        return ltlLegend;
    } //end f()

    /// <summary>
    /// Build a table row with the totals information.
    /// </summary>
    protected void BuildTotalInformation()
    {
        TableRow blankRow = new TableRow();
        TableCell blankCell = new TableCell();
        blankCell.ColumnSpan = 3;
        Literal blankLtl = new Literal();
        blankLtl.Text = "<hr />";
        blankCell.Controls.Add(blankLtl);
        blankRow.Cells.Add(blankCell);
        tblPricing.Rows.Add(blankRow);

        Label lblTotal = new Label();
        lblTotal.Text = "Estimated Monthly Total:";

        TextBox tbTotal = new TextBox();
        tbTotal.Enabled = false;
        tbTotal.CssClass = "packagetextbox";
        if (NumberOfUsers > -1)
        {
            int total = 0;
            foreach (Package item in Packages)
            {
                total += item.CalculateTotal(NumberOfUsers);
            } //end f()
            tbTotal.Text = String.Format("{0:C}", (double)total / (double)100);
        } //end if

        TableCell lblCell = new TableCell();
        lblCell.Controls.Add(lblTotal);

        TableCell tbCell = new TableCell();
        tbCell.Controls.Add(tbTotal);
        tbCell.ColumnSpan = 2;

        TableRow tr = new TableRow();
        tr.Cells.AddRange(new TableCell[] { lblCell, tbCell });
        tblPricing.Rows.Add(tr);
    } //end f()

    /// <summary>
    /// Build a series of rows listing each "legend" and it's description.
    /// </summary>
    protected void BuildLegendInformation()
    {
        List<Legend> legends = Packages.UniqueLegends();
        Legend legend = new Legend("*", "These prices are estimates and are based on a monthly basis");
        legends.Insert(0, legend);

        foreach (Legend item in legends)
        {
            Literal ltlDisplay = new Literal();
            ltlDisplay.Text = "<span class=\"superscript\">" + item.Display + "</span>";

            Label lblDescription = new Label();
            lblDescription.Text = item.Description;

            TableCell legendCell = new TableCell();
            legendCell.Controls.Add(ltlDisplay);
            legendCell.Controls.Add(lblDescription);
            legendCell.ColumnSpan = 3;

            TableRow tr = new TableRow();
            tr.CssClass = "productrow";
            tr.Cells.Add(legendCell);

            tblPricing.Rows.Add(tr);
        } //end foreach
    } //end f()

    /// <summary>
    /// Load up table rows and cells to display the information to the user.
    /// </summary>
    protected void BuildTable()
    {
        tblPricing.Rows.Clear();
        CheckBoxHash.Clear();
        PackageCollection packages = Packages;
        foreach (Package package in packages)
        {
            BuildPackageInformation(package);
        } //end foreach
        BuildTotalInformation();
        BuildLegendInformation();
    } //end f()

    /// <summary>
    /// The server path of the XML file that contains all the packages.
    /// </summary>
    public string BindingFilePath
    {
        get
        {
            if (ViewState["BindingFilePath"] == null)
                ViewState["BindingFilePath"] = Server.MapPath("~\\App_Data\\Packages.xml");
            return ViewState["BindingFilePath"].ToString();
        } //end get
        set
        {
            ViewState["BindingFilePath"] = value;
        } //end set
    } //end property

    /// <summary>
    /// The number of users we're calculating the totals for.
    /// </summary>
    public int NumberOfUsers
    {
        get
        {
            if (ViewState["NumberOfUsers"] == null)
                ViewState["NumberOfUsers"] = 0;
            return Convert.ToInt32(ViewState["NumberOfUsers"]);
        } //end get
        set
        {
            ViewState["NumberOfUsers"] = value;
        } //end set
    } //end property

    /// <summary>
    /// The list of packages used to build the table.
    /// </summary>
    public PackageCollection Packages
    {
        get
        {
            if (ViewState["Packages"] == null)
                ViewState["Packages"] = PackageCollection.LoadFromXML(BindingFilePath);
            return (PackageCollection)ViewState["Packages"];
        } //end get
        set
        {
            ViewState["Packages"] = value;
        } //end set
    } //end property 

    /// <summary>
    /// Provide a means to link a check box to a product.
    /// </summary>
    public Dictionary<Object, Product> CheckBoxHash
    {
        get;
        set;
    } //end property
} //end class



This was all done before I came into this position. All the packages pull from an XML document.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1