Wrox Programmer Forums
|
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
 
Old July 29th, 2012, 01:43 PM
Authorized User
 
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
Default

Thanks Imar, worked great. I will have to do the same for four arrays on the page, but I think I can do it now. Then the Calcs page will be able to access the four arrays. A bit of data to send back from the server.

When I get this program done, I can post both the vb and aspx pages.
__________________
Bob
bdtcomp.com
http://lettersfromasoldier.com
Follow me on Twitter
“Success is not final, failure is not fatal: it is the courage to continue that counts.”~Winston Churchill
 
Old July 30th, 2012, 03:22 PM
Authorized User
 
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
Default

Okay I am posting the aspx and vb pages here. They are associated with a Master page, so you will have to change that. But I will include the code to both.

I was able to get the half dozen variables that needed to persist. I copied Imar's example he sent and did two of my variables that way which I think is 'session state'. The rest were done with 'viewstate' and I used the PageLoad event, to refresh, so check out those lines. I also used the Page Prerendering event. BUT, that event doesn't always fire, and I learned that if your controls aren't visible it won't. Not sure what is going on here, so I have code to update the 'viewstate' in more than one procedure, not really what I wanted, but I don't understand the Prerendering Event and why this happens. I would like to be able to rely on this 100%.

This is still beta, but it should work, for the most part. It may error out, but that is part of the work, it's testing.

capacitor-calculations.aspx
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/CoilCalcsMaster.master" AutoEventWireup="false" CodeFile="capacitor-calculations.aspx.vb" Inherits="capacitor_calculations" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  <style type="text/css">
    .style5
    {
      height: 26px;
    }
    .style6
    {
      height: 21px;
    }
    .style7
    {
      width: 100%;
    }
  </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<p style="text-align: center">
      <span class="style2">Capacitor Calculations</span>
      <br class="style2" />
       Property of Interpower</p>
      <br />
      <table width="60%" border="0">
  <tr>
    <td colspan="2" >
      <asp:Label ID="lblTitle" runat="server" 
        Text="This program will calculate the total capacitance for several capacitors of different values connected in paralle. It will then ask for an operating frequency and a voltage range and show KVAR, Voltage, Current, Frequency and Reactance."></asp:Label>
    </td>
  </tr>
  <tr>
    <td >
      &nbsp;</td>
  <td> 
    &nbsp;</td>
  </tr>
  <tr>
    <td >
  <asp:Label ID="lblNoCapacitors" runat="server" Text="How many capacitors used?" 
        ViewStateMode="Enabled"></asp:Label></td>
  <td> 
  <asp:TextBox ID="txtNoCapacitors"
    runat="server" CssClass="LeftValueColumn"></asp:TextBox>
    &nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </td>
  </tr>
  <tr>
    <td>
      <asp:Label ID="lblCapNo" runat="server" ViewStateMode="Enabled"></asp:Label>
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
      <asp:Label ID="lblKVAR" runat="server" ViewStateMode="Enabled"></asp:Label>
    </td>
    <td>
      <asp:TextBox ID="txtKVAR" runat="server" CssClass="LeftValueColumn" 
        Visible="False"></asp:TextBox>
    </td>
  </tr>
  <tr>
    <td>
      <asp:Label ID="lblVoltage" runat="server" ViewStateMode="Enabled"></asp:Label>
    </td>
    <td>
      <asp:TextBox ID="txtVoltage" runat="server" CssClass="LeftValueColumn" 
        Visible="False"></asp:TextBox>
    </td>
  </tr>
  <tr>
    <td class="style5">
      <asp:Label ID="lblFreq" runat="server" ViewStateMode="Enabled"></asp:Label>
    </td>
    <td class="style5">
      <asp:TextBox ID="txtFreq" runat="server" CssClass="LeftValueColumn" 
        Visible="False"></asp:TextBox>
    </td>
  </tr>

  <tr>
    <td class="style5">
      <asp:Label ID="lblHowManyThese" runat="server" ViewStateMode="Enabled"></asp:Label>
    </td>
    <td class="style5">
      <asp:TextBox ID="txtHowManyThese" runat="server" CssClass="LeftValueColumn" 
        Visible="False"></asp:TextBox>
    </td>
  </tr>


  <tr>
    <td>&nbsp;</td>
    <td style="text-align: right">
      <asp:Button ID="btnNextCap" runat="server" Text="Next Capacitor" 
        Height="26px" />
    </td>
  </tr>
</table>
  &nbsp;&nbsp; 
  <br />
  <br />

  <table width="100%" border="0">
  <tr>
    <td>
      <asp:Label ID="lblCapNoTitle" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblQuantity" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblKVARTitle" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblVoltageTitle" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblFreqTitle" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblMicroFarads" runat="server"></asp:Label>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
      <asp:Label ID="lblCapNoResult" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblQuantityResult" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblKVARResult" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblVoltageResult" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblFreqResult" runat="server"></asp:Label>
    </td>
    <td>
      <asp:Label ID="lblMicroFaradsResult" runat="server"></asp:Label>
    </td>
  </tr>
  <tr>
    <td class="style6" colspan="6">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="6">
      <asp:Label ID="lblTotalCap" runat="server"></asp:Label>
    </td>
  </tr>
  </table>

  <br /> 
     

  <table class="style7">
    <tr>
      <td class="style5">
        <asp:Label ID="lblFreqCalcParam" runat="server"></asp:Label>
      </td>
      <td class="style5">
        <asp:TextBox ID="txtFreqCalcParam" runat="server" CssClass="LeftValueColumn" 
          Visible="False"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td>
        <asp:Label ID="lblCalcParamVolt" runat="server"></asp:Label>
      </td>
      <td>
        &nbsp;</td>
    </tr>
    <tr>
      <td>
        <asp:Label ID="lblHighVolt" runat="server"></asp:Label>
      </td>
      <td>
        <asp:TextBox ID="txtHighVolt" runat="server" CssClass="LeftValueColumn" 
          Visible="False"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td>
        <asp:Label ID="lblLowVolt" runat="server"></asp:Label>
      </td>
      <td>
        <asp:TextBox ID="txtLowVolt" runat="server" CssClass="LeftValueColumn" 
          Visible="False"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td>
        <asp:Label ID="lblVoltIncr" runat="server"></asp:Label>
      </td>
      <td>
        <asp:TextBox ID="txtVoltIncr" runat="server" CssClass="LeftValueColumn" 
          Visible="False"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td>
        &nbsp;</td>
      <td style="text-align: right">
        <asp:Button ID="btnVoltParam" runat="server" text="Voltage Paramaters" 
          Visible="False" />
      </td>
    </tr>
    <tr>
      <td>
        &nbsp;</td>
      <td>
        &nbsp;</td>
    </tr>
  </table>
     <table class="style7">
       <tr>
         <td>
           <asp:Label ID="lblKVARIncr" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblVoltageIncr" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblCurrentIncr" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblFrequencyIncr" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblReactanceIncr" runat="server"></asp:Label>
         </td>
       </tr>
       <tr>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
       </tr>
       <tr>
         <td>
           <asp:Label ID="lblKVARIncrResult" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblVoltageIncrResult" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblCurrentIncrResult" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblFrequencyIncrResult" runat="server"></asp:Label>
         </td>
         <td>
           <asp:Label ID="lblReactanceIncrResult" runat="server"></asp:Label>
         </td>
       </tr>
       <tr>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
         <td>
           &nbsp;</td>
       </tr>
  </table>
     <br />





</asp:Content>
capacitor-calculations.aspx.vb
Code:
Partial Class capacitor_calculations
  Inherits System.Web.UI.Page


  Dim KR(25) As String
  Dim VR(25) As String
  Dim FR(25) As String
  Dim Q(25) As String
  Dim AR(25) As Decimal
  Dim MF(25) As Decimal
  Dim XC(25) As Decimal
  Dim TC As String
  Dim decTC As Decimal = 0
  Dim XA As Single = 0
  ' Dim intNoCap As Integer 'Removed this because using SessionState instead. Bt-30Jul2012



  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
      btnNextCap.Enabled = False
    Else
      KR = ViewState("KR")
      VR = ViewState("VR")
      FR = ViewState("FR")
      Q = ViewState("Q")
      TC = ViewState("TC")
      decTC = CDec(TC)

      btnNextCap.Enabled = True

     
    End If

  End Sub

  Protected Sub btnNextCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextCap.Click


    KR(Counter) = (txtKVAR.Text)
    VR(Counter) = (txtVoltage.Text)
    FR(Counter) = (txtFreq.Text)
    Q(Counter) = (txtHowManyThese.Text)

    'I am sending the value(s) here to ViewState because I don't trust Prerendering! BT-30Jul2012
    ViewState("KR") = KR
    ViewState("VR") = VR
    ViewState("FR") = FR
    ViewState("Q") = Q

    'Clear the textbox values now.
    txtKVAR.Text = ""
    txtVoltage.Text = ""
    txtFreq.Text = ""
    txtHowManyThese.Text = ""


    If Counter = intNoCap Then
      btnNextCap.Enabled = False
      CapCalc()
    Else
    End If

    Counter = Counter + 1
    If Counter <= intNoCap Then
      lblCapNo.Text = "Capacitor number " & Counter.ToString
    Else

      'Let's first clean-up the previous screen.
      lblTitle.Text = ""
      lblNoCapacitors.Text = ""
      lblCapNo.Text = ""
      lblKVAR.Text = ""
      lblVoltage.Text = ""
      lblFreq.Text = ""
      lblHowManyThese.Text = ""
      btnSubmit.Visible = False
      txtNoCapacitors.Visible = False
      txtKVAR.Visible = False
      txtVoltage.Visible = False
      txtFreq.Visible = False
      txtHowManyThese.Visible = False
      btnNextCap.Visible = False

      lblCapNo.Text = ""
      btnVoltParam.Visible = True
      lblFreqCalcParam.Text = "What frequency would you like to calculate parameters at?"
      lblCalcParamVolt.Text = "Now the program will calculate parameters over a range of voltage."
      lblHighVolt.Text = "Highest voltage"
      lblLowVolt.Text = "Lowest voltage"
      lblVoltIncr.Text = "In what increments"

      txtFreqCalcParam.Visible = True
      txtHighVolt.Visible = True
      txtLowVolt.Visible = True
      txtVoltIncr.Visible = True

    End If


  End Sub

  Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Counter = 1


    intNoCap = Val(txtNoCapacitors.Text)
    btnSubmit.Enabled = False
    txtNoCapacitors.Enabled = False

    lblNoCapacitors.Text = "How many capacitors used?"
    txtNoCapacitors.Visible = True
    lblCapNo.Text = "Capacitor number " & Counter.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

    btnNextCap.Visible = True

    

  End Sub

  Private Sub CapCalc()
    lblCapNoTitle.Text = "Cap #"
    lblQuantity.Text = "Quantity"
    lblKVARTitle.Text = "KVAR"
    lblVoltageTitle.Text = "Voltage"
    lblFreqTitle.Text = "Frequency"
    lblMicroFarads.Text = "Microfarads"


    For CalcCounter = 1 To intNoCap

      AR(CalcCounter) = Q(CalcCounter) * 1000 * KR(CalcCounter) / VR(CalcCounter)
      XC(CalcCounter) = VR(CalcCounter) / AR(CalcCounter)
      MF(CalcCounter) = 1000000.0! / (2 * 3.14159 * FR(CalcCounter) * XC(CalcCounter))

    Next

    Dim I As Integer = 0
    For I = 1 To Counter
      decTC = decTC + MF(I)

      lblCapNoResult.Text = lblCapNoResult.Text & I.ToString & "<BR />"
      lblQuantityResult.Text = lblQuantityResult.Text & Q(I) & "<BR />"
      lblKVARResult.Text = lblKVARResult.Text & KR(I) & "<BR />"
      lblVoltageResult.Text = lblVoltageResult.Text & VR(I) & "<BR />"
      lblFreqResult.Text = lblFreqResult.Text & FR(I) & "<BR />"
      lblMicroFaradsResult.Text = lblMicroFaradsResult.Text & MF(I) & "<BR />"

      lblTotalCap.Text = "Total Capacitance for this capitor bank is " & decTC.ToString & " microfarads"


    Next
    'I am sending the value(s) here to ViewState because I don't trust Prerendering! BT-30Jul2012
    TC = decTC.ToString
    ViewState("TC") = TC

  End Sub

  Public Property Counter As Integer
    Get
      Dim o As Object = ViewState("Counter")
      If Not o Is Nothing Then
        Return CType(o, Integer)
      Else
        Return 0
      End If
    End Get
    Set(ByVal Value As Integer)
      ViewState("Counter") = Value
    End Set
  End Property

  Public Property intNoCap As Integer
    Get
      Dim o2 As Object = ViewState("intNoCap")
      If Not o2 Is Nothing Then
        Return CType(o2, Integer)
      Else
        Return 0
      End If
    End Get
    Set(ByVal Value As Integer)
      ViewState("intNoCap") = Value
    End Set
  End Property


  Protected Sub btnNextCap_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextCap.PreRender
    ViewState("KR") = KR
    ViewState("VR") = VR
    ViewState("FR") = FR
    ViewState("Q") = Q
    TC = decTC.ToString
    ViewState("TC") = TC
  End Sub


  Protected Sub btnVoltParam_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltParam.Click

    'Setup Column Names
    lblKVARIncr.Text = "KVAR"
    lblVoltageIncr.Text = "Voltage"
    lblCurrentIncr.Text = "Current"
    lblFrequencyIncr.Text = "Frequency"
    lblReactanceIncr.Text = "Reactance"

    sngFreqForParam = Val(txtFreqCalcParam.Text)
    sngVHigh = Val(txtHighVolt.Text)
    sngVLow = Val(txtLowVolt.Text)
    sngVIncr = Val(txtVoltIncr.Text)

    VoltageCalcs()

  End Sub

  Dim sngFreqForParam As Single = 0
  Dim sngVHigh As Single = 0
  Dim sngVLow As Single = 0
  Dim sngVIncr As Single = 0

  Private Sub VoltageCalcs()

    Dim Count2 As Integer
    'Variables below as in the formulas
    Dim V(200) As Decimal
    Dim A(200) As Decimal
    Dim K(200) As Decimal

    'Calcs

    XA = 1000000.0! / (2 * 3.14159 * sngFreqForParam * decTC)



    sngVIncr = -sngVIncr
    For calcCount = sngVHigh To (sngVLow - 1) Step sngVIncr
      Count2 = Count2 + 1
      V(Count2) = calcCount
      A(Count2) = V(Count2) / XA
      K(Count2) = A(Count2) * V(Count2) / 1000


      'Print final calculations. Parameters for a range of Voltages. BT-30Jul2012
      lblKVARIncrResult.Text = lblKVARIncrResult.Text & K(Count2).ToString & "<BR />"
      lblVoltageIncrResult.Text = lblVoltageIncrResult.Text & V(Count2).ToString & "<BR />"
      lblCurrentIncrResult.Text = lblCurrentIncrResult.Text & A(Count2).ToString & "<BR />"
      lblFrequencyIncrResult.Text = lblFrequencyIncrResult.Text & sngFreqForParam.ToString & "<BR />"
      lblReactanceIncrResult.Text = lblReactanceIncrResult.Text & XA.ToString & "<BR />"


    Next



  End Sub

End Class
__________________
Bob
bdtcomp.com
http://lettersfromasoldier.com
Follow me on Twitter
“Success is not final, failure is not fatal: it is the courage to continue that counts.”~Winston Churchill
 
Old July 30th, 2012, 03:32 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

PreRender fires right before the control is rendered to the browser. A control that's not visible doesn't generate any output, and therefore doesn't render and therefore PreRender is never called.

For more info: http://stackoverflow.com/questions/2...prerender-true

I would set up methods in your code that handle the state management and call them from Page Load or another page based event....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
btcomp (July 30th, 2012)
 
Old July 30th, 2012, 03:53 PM
Authorized User
 
Join Date: Jul 2010
Posts: 81
Thanks: 10
Thanked 3 Times in 2 Posts
Default

Thanks Imar. I am working on my other project for a CNC shop, but it is strictly VB.Net using RS-232. But will be back working on the High Voltage project again shortly.
__________________
Bob
bdtcomp.com
http://lettersfromasoldier.com
Follow me on Twitter
“Success is not final, failure is not fatal: it is the courage to continue that counts.”~Winston Churchill





Similar Threads
Thread Thread Starter Forum Replies Last Post
Increment button Mark P. C# 4.0 aka C# 2010 General Discussion 3 May 1st, 2014 04:31 PM
Increment a tag stefan.yu XSLT 10 April 16th, 2007 05:45 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 03:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
Is it possible to increment in crystal cmdolcet Crystal Reports 2 August 14th, 2005 06:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.