Member Avatar

postonoh

Sample webpage

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

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
     <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
  
         <table style="width:100%;">
              <tr>
                  <th colspan="2" align="left" scope="row">My goal is to: 
    			<p> <asp:CheckBoxList ID="goalbxlist" runat="server"  
                         onselectedindexchanged="goalbxlist_SelectedIndexChanged" AutoPostBack="True">
          <asp:ListItem Value="10">continue in my business (10)</asp:ListItem>
          <asp:ListItem Value="20">move my business from my home to an office setting (20) </asp:ListItem>
          <asp:ListItem Value="30">expand my business using the lastest technological developments (30)</asp:ListItem>
     </asp:CheckBoxList>
    				</p></th></tr>
              <tr>
                   <td>
                        &nbsp;</td>
                   <td>
                        &nbsp;</td>
                   <td>
                   <asp:TextBox runat="server" type="text" name="total" id="total" readonly="true" Visible="false" /></td>
              </tr>

         </table>
    
 </asp:Content>

sample behind code

protected void goalbxlist_SelectedIndexChanged(object sender, EventArgs e)
    {
         string values = "";
         int sum = 0;
         double subsum = 0;
         for (int i = 0; i < goalbxlist.Items.Count; i++)
         {
              if (goalbxlist.Items[i].Selected)
              {
                   values += goalbxlist.Items[i].Value;
              }
              double.TryParse(values, out subsum);

         }

         sum += Convert.ToInt32(subsum);
         total.Text = sum.ToString();
         }
    }

If user check a box or multiple boxes I need for them add.

if listitem 10 selected insert 10 in the textbox

if listitem 10 and 20 selected insert 30 in the textbox

if listitem 10,20 and 30 selected insert 60 in the textbox

any combination insert total check into textbox.

Thanks ahead of time.

values is a string, try replacing it on line 10 by subsum and delete line 12

Also I don't think you need to convert to an integer. If you are only expecting relatively small integers in the list box why not use integers the whole way through rather than doubles.

Member Avatar

postonoh

values is a string, try replacing it on line 10 by subsum and delete line 12

Also I don't think you need to convert to an integer. If you are only expecting relatively small integers in the list box why not use integers the whole way through rather than doubles.

Thanks,

I figure it bydoing this.

protected void btnNext_Click(object sender, EventArgs e)
    {

         double _val;
         {
              _val = goalbxlist.Items.Cast<ListItem>().Where(i => i.Selected == true).Select(i => double.Parse(i.Value)).Aggregate((items, item) => items + item);

              total.Text = _val.ToString();
         }

    }