passing a response.write value to a label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    passing a response.write value to a label

    I have the following subroutine:

    Code:
     Public Sub StartTimeDif()
            Dim dt As DateTime = Convert.ToDateTime(starttimeInput.Text)
            Dim dt1 As DateTime = Convert.ToDateTime(endtimeInput.Text)
            Dim ts As TimeSpan = dt1.Subtract(dt)
            Response.Write(ts.Minutes)
        End Sub
    and I'm trying to pass (ts.minutes) to durationLabel. Can anyone offer me a solution to that.

    Thank you

    Doug
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    If you have a "durationLa bel" on your ASPX page:
    Code:
    <asp:Label ID="durationLabel" runat="server" />
    Then you can set it's Text property in code:
    Code:
    Public Sub StartTimeDif()
       Dim startTime As DateTime    
       Dim endTime As DateTime 
       If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
          DateTime.TryParse(endtimeInput.Text, endTime) Then
            Dim ts As TimeSpan = endTime - startTime
            durationTime.Text = ts.Minutes.ToString()
       Else
            durationTime.Text = "Start Time or End Time value is invalid"
       End If
    End Sub
    -Frinny

    Comment

    • dougancil
      Contributor
      • Apr 2010
      • 347

      #3
      Frinny,

      What if I wanted to have that value show up in a popup window that would allow a user to select yes or no and then if they selected yes, it submits the information to the sql server and if they select no it takes them back to the web page to re-enter data.

      Thank you

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Then you need to implement JavaScript that will do that.
        This will not be done in your C# code.

        Use a JavaScript confirm popup box.

        Comment

        • dougancil
          Contributor
          • Apr 2010
          • 347

          #5
          Frinny,

          Ok so if I use this:

          Code:
          <input type="button" onclick="show_confirm()" value="Confirm" />
          <script type="text/javascript">
          function show_confirm()
          {
          var r=confirm (ts.Minutes.ToString();
          if (r==true)
            {
            alert("You pressed OK!");
            }
          else
            {
            alert("You pressed Cancel!");
            }
          }
          </script>
          and want to fire off my insetrow() sql command, how do I call that from the javascript when the user presses ok?

          Code:
              Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button.Click
                  StartTimeDif()
                  InsertRow()
              End Sub

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Check out this article on How to use JavaScript in ASP.NET.

            It explains how you can use JavaScript to prevent ASP.NET buttons from posting back to the server.

            -Frinny

            Comment

            • dougancil
              Contributor
              • Apr 2010
              • 347

              #7
              Frinny,

              I'm reading the article and trying to reproduce what you did so that I can understand it more and I've copied your code as you have it written, except that mines not referencing a master page and in my subroutine for the CreateHelloWorl dJavaScript() on line 7, I'm getting "Name Confirm is not declared."

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Where did you place this code?

                Please copy&paste exactly what you have.
                It sounds like you're missing a double quote or something.

                -Frinny

                Comment

                • dougancil
                  Contributor
                  • Apr 2010
                  • 347

                  #9
                  I'll post my code in my next reply but I also wanted to say that I tested the code that I have for passing the response to my label and I'm not seeing anything in my label. Here's the code that I have for it:

                  Code:
                   Public Sub StartTimeDif()
                          Dim startTime As DateTime
                          Dim endTime As DateTime
                          If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
                             DateTime.TryParse(endtimeInput.Text, endTime) Then
                              Dim ts As TimeSpan = endTime - startTime
                              durationLabel.Text = ts.Minutes.ToString()
                          End If
                      End Sub
                      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                          InsertRow()
                          StartTimeDif()
                      End Sub
                  I took your error out until I figure out what's causing this to not display at all. Can you see what I may be missing?

                  Thank you

                  Doug

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Maybe you didn't enter the if block.

                    Try this:
                    Code:
                     Public Sub StartTimeDif()
                            Dim startTime As DateTime
                            Dim endTime As DateTime
                            If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
                               DateTime.TryParse(endtimeInput.Text, endTime) Then
                                 Dim ts As TimeSpan = endTime - startTime
                                 durationLabel.Text = ts.Minutes.ToString()
                            Else
                                 durationLabel.Text = "The start-time or end-time value provided was invalid. It could not be converted into a DateTime type."
                            End If
                        End Sub

                    Comment

                    • dougancil
                      Contributor
                      • Apr 2010
                      • 347

                      #11
                      I entered the if block but the regular expression validator is supposed to catch if someone doesnt enter the data correctly. When someone enters a starttime and endtime, it's posting correctly to the database, but for whatever reason, it's not showing it in the label. A user will enter the starttime as 11:00 pm and the end time as say 11:15 pm , to me that's already a datetime format. I'm missing something but I have no idea what.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        • Make a new project.
                        • Add the starttimeInput and endtimeInput TextBoxes to the page.
                        • Add a button to the page
                        • Add the durationLabel to the page
                        • In the VB.NET Code:
                          • Add the StartTimeDif() method to the code
                          • Implement a method that handles the button's Click event and call the StartTimeDif() method.
                        • Try using the program.

                        Comment

                        • dougancil
                          Contributor
                          • Apr 2010
                          • 347

                          #13
                          It works as a "standalone " and I'm not quite sure what's causing that failure. Also, if I want to find out both minutes and hours, between starttime and endtime, what kind of equation should I use? Obviously, it can't be just ts.minutes.toSt ring()

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Well the TimeSpan (the ts variable) has all of that information...

                            Comment

                            • dougancil
                              Contributor
                              • Apr 2010
                              • 347

                              #15
                              Frinny,

                              Ok I figured out what to do with the hours and minutes but what I'm not understanding is whenever I enter any digits and then press submit, I'm getting this error:

                              System.FormatEx ception: String was not recognized as a valid DateTime.

                              If I have this as a "stand alone" web page it works just fine. I removed the validation from the text boxes thinking that this may have something to do with it but I'm still getting this error. Do you have any other ideas as to what it may be?

                              Thanks

                              Doug

                              Comment

                              Working...