 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 28th, 2012, 11:40 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
Trying to increment a counte
Okay, I give up, well not really. But I have four data fields that are to be filled and the data is put into a nice array, to have calculations done on the numbers when the array is done. I set up a counter field to start the program, so onece you enter 'number of capacitors' then what is displayed are four text fields to fill out for capacitor 1. Then a 'Next' button is clicked to clear the text fields and to show capacitor 2. But when I fill out the data fields and click 'Next' I get show capacitor 2 again. The simple counter which is Dim at 1 increments to 2 each time Next is clicked, but never gets past 2.
I have setup:
Quote:
|
<%@ Page Title="" Language="VB" MasterPageFile="~/CoilCalcsMaster.master" AutoEventWireup="false" CodeFile="capacitor-calculations.aspx.vb" Inherits="capacitor_calculations" EnableViewState="true" ViewStateMode="Disabled"%>
|
And then I have enabled ViewStateMode on individual controls where I thought I could get the counter to increment but never does!
|
|

July 29th, 2012, 02:05 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
As a former member of this site used to say:
>> the error is online 11
> how do you know? I didn't post any code.
Exactly!
In other words, impossible to say without seeing your code.
Cheers,
Imar
|
|

July 29th, 2012, 07:32 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
Hi Imar, glad to see you here, and thank you. Yes, I should have posted the code. So here is the code for the click event of the Next button I have. Variable C I have dimension as DIM C as Integer = 1. I first ask the user to tell me how many different capacitor groups he has. This number is stored in C and then I have a submit button that sets up the four text boxes shown below in the code. So if the user put 5 in the textbox to submit then the below routine should run and increment C each time. But C gets incremented to 2 and stays at 2. Each time I press the Next button C goes back to 1!
Code:
Protected Sub btnNextCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextCap.Click
KR(C) = Val(txtKVAR.Text)
VR(C) = Val(txtVoltage.Text)
FR(C) = Val(txtFreq.Text)
Q(C) = Val(txtHowManyThese.Text)
'Clear the textbox values now.
txtKVAR.Text = ""
txtVoltage.Text = ""
txtFreq.Text = ""
txtHowManyThese.Text = ""
If C = intNoCap Then
btnNextCap.Enabled = False
CapCalc()
Else
End If
AR(C) = Q(C) * 1000 * KR(C) / VR(C)
XC(C) = VR(C) / AR(C)
MF(C) = 1000000.0! / (2 * 3.14159 * FR(C) * XC(C))
If C = Val(txtNoCapacitors.Text) Then
btnNextCap.Enabled = False
'Run Calcs.
CapCalc()
Else
End If
C = C + 1
lblCapNo.Text = "Capacitor number " & C.ToString
End Sub
|
|

July 29th, 2012, 07:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you post *all* relevant code? I don't see you declare C.
Also: a Page instance is created every time you request a page, also after a PostBack. So if C is a simple class based variable, it'll automatically be recreated (and thus set to its initial value) on each reqeust. Use ViewState, a hidden field or another state mechanism to maintain its value.
Cheers,
Imar
|
|

July 29th, 2012, 08:15 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
I just noticed that I had this bit of code in twice.
Code:
If C = intNoCap Then
btnNextCap.Enabled = False
CapCalc()
Else
End If
AR(C) = Q(C) * 1000 * KR(C) / VR(C)
XC(C) = VR(C) / AR(C)
MF(C) = 1000000.0! / (2 * 3.14159 * FR(C) * XC(C))
If C = Val(txtNoCapacitors.Text) Then
btnNextCap.Enabled = False
'Run Calcs.
CapCalc()
I changed this to but it didn't change anything with the C counter.
Code:
AR(C) = Q(C) * 1000 * KR(C) / VR(C)
XC(C) = VR(C) / AR(C)
MF(C) = 1000000.0! / (2 * 3.14159 * FR(C) * XC(C))
If C = intNoCap Then
btnNextCap.Enabled = False
CapCalc()
Else
End If
|
|

July 29th, 2012, 08:21 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
Here is all the code except for the CapCalc sub, which isn't finished anway!
Code:
Partial Class capacitor_calculations
Inherits System.Web.UI.Page
Dim C As Integer = 1
Dim VR(25) As Decimal
Dim FR(25) As Decimal
Dim Q(25) As Decimal
Dim AR(25) As Decimal
Dim KR(25) As Decimal
Dim MF(25) As Decimal
Dim XC(25) As Decimal
Dim TC As Decimal = 0
Dim intNoCap As Integer
Protected Sub btnNextCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextCap.Click
KR(C) = Val(txtKVAR.Text)
VR(C) = Val(txtVoltage.Text)
FR(C) = Val(txtFreq.Text)
Q(C) = Val(txtHowManyThese.Text)
'Clear the textbox values now.
txtKVAR.Text = ""
txtVoltage.Text = ""
txtFreq.Text = ""
txtHowManyThese.Text = ""
AR(C) = Q(C) * 1000 * KR(C) / VR(C)
XC(C) = VR(C) / AR(C)
MF(C) = 1000000.0! / (2 * 3.14159 * FR(C) * XC(C))
If C = intNoCap Then
btnNextCap.Enabled = False
CapCalc()
Else
End If
C = C + 1
lblCapNo.Text = "Capacitor number " & C.ToString
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
intNoCap = Val(txtNoCapacitors.Text)
btnSubmit.Enabled = False
txtNoCapacitors.Enabled = False
lblNoCapacitors.Text = "How many capacitors used?"
txtNoCapacitors.Visible = True
lblCapNo.Text = "Capacitor number " & C.ToString
' lblNoCapacitors.Enabled = False 'Once next is clicked, disable changing this value.
lblKVAR.Text = "Rated KVAR"
txtKVAR.Visible = True
lblVoltage.Text = "Rated voltage"
txtVoltage.Visible = True
lblFreq.Text = "Rated Frequency"
txtFreq.Visible = True
lblHowManyThese.Text = "How many of this capacitor type?"
txtHowManyThese.Visible = True
End Sub
|
|

July 29th, 2012, 08:31 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Code:
Partial Class capacitor_calculations Inherits System.Web.UI.Page Dim C As Integer = 1
See my previous reply; since you implemented C (great name!) as a class based variable, it'll be recreated and reset to 1 every time the page loads. You enabled View State on the controls, but since Integer is not a control, that doesn't help.
Imar
|
|

July 29th, 2012, 08:59 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
Quote:
Originally Posted by Imar
[CODE]since you implemented C (great name!) as a class based variable,
|
Yes, C is a great name, I considered changing to counter & prob. should here. The formulas are old time tested, but written long ago. I have over a thousand of these to work with, sometimes it's less confusing to paste the formulas in, I do change the names often, but the time allotted for this doesn't allow it.
I will have to figure how to do 'hidden state' not really sure how that works. But I will Google it! Thanks Imar! Hope you are enjoying the Olympics. I am hoping the Michael Phelps can make a comeback Monday and beat the world record in medals. He really put his heart into this the first two Olympics and he had setbacks and done some stupid things, but I think his determination to excel is a good role model! 
|
|

July 29th, 2012, 10:21 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I have over a thousand of these to work with, sometimes it's less confusing to paste the formulas in
|
Ah, that explains it. Makes sense.
Quote:
|
I will have to figure how to do 'hidden state' not really sure how that works. But I will Google it!
|
To help you figure it out, and save you some time, I wrote you a quick article explaining the basic concept. You can find the article here: http://imar.spaanjaars.com/570/imple...ate-properties
Quote:
|
Hope you are enjoying the Olympics.
|
Haven't seen much yet, but hopefully I find some time to see some of it.
Cheers,
Imar
|
|

July 29th, 2012, 10:48 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
|
|
Quote:
Originally Posted by Imar
|
Thank you Imar, I have printed this and will study it. The original program goes back to DOS days, so I had to modify how the user interacted. 
|
|
 |