Solved

code behind c#.

Posted on 2015-02-06
11
135 Views
Last Modified: 2015-03-12
I have below asp.net webform and I want to do below math, and I want to do it dynamic in code behind/c#?
Is it possible?



listPrice01 = model01(array[2])
extnPrice01 = qty * UnitPrice
unitPrice01 = listPrice01 * listPrice01 * priceType01

Get the total of each column:

totalListPrice
totalUnitPrice
totalExtnPrice


<table width="1024" cellpadding="3" cellspacing="3">
<tr><td>Action</td><td>Model</td><td>Qty</td><td>Price Type</td><td>List Price</td><td>Unit Price</td><td>Extn Price</td></tr>
<tr>
<td><asp:Button runat="server" ID="btnRemove"  Text="Remove" /></td>
<td><asp:DropDownList runat="server" ID="model01">
<asp:ListItem Text="ModelNo,ModelDescrption,1000" Value="ModelNo,ModelDescription,1000"></asp:ListItem>
<asp:ListItem Text="ModelNo,ModelDescrption,2000" Value="ModelNo,ModelDescription,2000"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:TextBox runat="server" ID="Qty01"></asp:TextBox></td>
<td><asp:DropDownList runat="server" ID="priceType01">
<asp:ListItem Text="0.08" Value="0.08"></asp:ListItem>
<asp:ListItem Text="0.10" Value="0.10"></asp:ListItem>
<asp:ListItem Text="0.12" Value="0.12"></asp:ListItem>
<asp:ListItem Text="0.13" Value="0.13"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:Label runat="server" ID="listPrice01" Text="0.00"></asp:Label></td>
<td><asp:TextBox runat="server" ID="unitPrice01"></asp:TextBox></td>
<td><asp:Label runat="server" ID="extnPrice01" Text="0.00"></asp:Label></td>
</tr>
<tr>
<td><asp:Button runat="server" ID="Button1"  Text="Remove" /></td>
<td><asp:DropDownList runat="server" ID="model02">
<asp:ListItem Text="ModelNo,ModelDescrption,1000" Value="ModelNo,ModelDescription,1000"></asp:ListItem>
<asp:ListItem Text="ModelNo,ModelDescrption,2000" Value="ModelNo,ModelDescription,2000"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:TextBox runat="server" ID="Qty02"></asp:TextBox></td>
<td><asp:DropDownList runat="server" ID="priceType02">
<asp:ListItem Text="0.08" Value="0.08"></asp:ListItem>
<asp:ListItem Text="0.10" Value="0.10"></asp:ListItem>
<asp:ListItem Text="0.12" Value="0.12"></asp:ListItem>
<asp:ListItem Text="0.13" Value="0.13"></asp:ListItem>
</asp:DropDownList></td>
<td><asp:Label runat="server" ID="listPrice02" Text="0.00"></asp:Label></td>
<td><asp:TextBox runat="server" ID="unitPrice02"></asp:TextBox></td>
<td><asp:Label runat="server" ID="extnPrice02" Text="0.00"></asp:Label></td>
</tr>
 
<tr bgcolor="silver">
<td colspan="2"><br /></td><td><asp:Label runat="server" ID="totalQty"></asp:Label></td>
<td><br /></td>
<td><asp:Label runat="server" ID="totalListPrice"></asp:Label></td>
<td><asp:Label runat="server" ID="totalUnitPrice"></asp:Label></td>
<td><asp:Label runat="server" ID="totalExtnPrice"></asp:Label></td>
</tr>
</table>
0
Comment
Question by:ITsolutionWizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40594936
In:
extnPrice01 = qty * UnitPrice
unitPrice01 = listPrice01 * listPrice01 * priceType01

Where is UnitPrice? Is it related to unitPrice01?

Has UnitPrice typo? I cannot find it in your .aspx source file.

Also, you have Qty01 but not qty; are they the same?
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40594955
listPrice01 = model01(array[2])
extnPrice01 = qty01 * UnitPrice01
unitPrice01 = listPrice01 * listPrice01 * priceType01

Sorry above should be logical
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40594961
Do you have some code in code behind or it is blank?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40595123
1. Locate the following line and add the portion in bold font:

<td><asp:DropDownList runat="server" ID="model01" bordercolor="yellow" AutoPostBack="true" OnSelectedIndexChanged="model01_SelectedIndexChanged">

2. Double click on model01 dropbox in design view. This gives you:

  protected void model01_SelectedIndexChanged(object sender, EventArgs e)
    {
        listPrice01.Text = model01.Text;    //<-- add this line
    }

Open in new window


3. run the page and select "ModelNo,ModelDescription,1000" option for example. This string will show up in ListPrice01 text box.

For now focusing on the first formula:

listPrice01 = model01

listPrice01 noe contains string "ModelNo,ModelDescription,1000". Because its content will be used later in the third formula, we will have problems with it. For listPrice01 we need to have just a number. Where this number is supposed to be coming?
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40595149
if we use autopostback = true, alot of data will be gone. Is it possible not to use it?
another problem is because I will have more than 2 row meaning more than 2 model drop down.
so I have to create selectedindexchange many more. is it possible to have something dynamic?

Thanks
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40595250
we can remove autopostback = true. Instead use a submit button with autopostback = true to click when ready.
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40596178
Just to throw my 2 cents in here, why do you want to do this in code-behind?
That's a horrid user experience.  Every time the user changes something the page would submit and refresh for them, causing needless postbacks to the server.
You could get around the page refresh with ajax, but you're still sending very pointless trips to the server to do a calculation that would be very simple to do on client side javascript.

Is there any specific reason you want to do it server-side?
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40596218
As far as I know, which is not much, having asp in your ,aspx file gets server involved one way or another. Now it becomes the choice of where to maintain the code you want to write. Some people like to combine it with their .aspx codes but you specifically have asked for code behind c#. It happens I also prefer to keep them, separately. What I have giving is code behind.

<td><asp:Button runat="server" ID="btnRemove"  Text="Remove" /></td>

Question: Where is your data source. Do you want these just for display (like a calculator) only? Or, you would want to save them in some database?

Mike
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40596242
Well yes and no.  Having the asp: prefix on the input items means the server is tracking it through postbacks and viewstate, but you still have full availability to access everything client side.

And I do realize you're answering the question asked, I'm just saying overall it's very much not the best way to approach it.
Any changes made to the input fields through js will still post back to the server with their current values, if and when the entire page is submitted, and can still save or do whatever the server process is.

But for simple addition / sum calculations like requested, the overhead on both the server and the client is very unnecessary.

A few lines of javascript executing on the client, vs an entire ajax (or full postback) on the client, and another entire request cycle on the server.
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40596245
FYI, eghtebas, re-reading my last comment, could have come across as somewhat snotty.
If it did, wasn't intended that way at all ;)
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40596276
We are at EE to learn. Nothing else really matters. As far as I am concern is to pay attention to the solution sought and help out as much as I can and then also learn in the process.

BTW, you didn't answer my question on what do you do with the data produced in this project. Or. whom this project serves and how it is intended to do that.

Mike
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question