Solved

code behind c#.

Posted on 2015-02-06
11
119 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
  • 6
  • 3
  • 2
11 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
 

Author Comment

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

Sorry above should be logical
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Do you have some code in code behind or it is blank?
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
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
 

Author Comment

by:ITsolutionWizard
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
we can remove autopostback = true. Instead use a submit button with autopostback = true to click when ready.
0
 
LVL 22

Expert Comment

by:Snarf0001
Comment Utility
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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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 22

Expert Comment

by:Snarf0001
Comment Utility
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 22

Expert Comment

by:Snarf0001
Comment Utility
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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now