Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

code behind c#.

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>
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

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?
Avatar of ITsolutionWizard

ASKER

listPrice01 = model01(array[2])
extnPrice01 = qty01 * UnitPrice01
unitPrice01 = listPrice01 * listPrice01 * priceType01

Sorry above should be logical
Do you have some code in code behind or it is blank?
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
we can remove autopostback = true. Instead use a submit button with autopostback = true to click when ready.
Avatar of Snarf0001
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?
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
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.
FYI, eghtebas, re-reading my last comment, could have come across as somewhat snotty.
If it did, wasn't intended that way at all ;)
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