Solved

convert textbox into hiddenfield

Posted on 2010-11-15
3
713 Views
Last Modified: 2012-05-10
Hello,
I have a user control built on Visual Studio 2005 using vb.net. I have adapted a previous form that declared a control as a "hiddenfield" and now I'm using a textbox to declare the total "totabox".

I am attempting to re-declare the declared field "Dim totalBox As TextBox" to something like "Dim totalBox As HiddenField" such that my Form_Load can capture the value of hdnTotalID.  My Form Load code follows:

-----------
 Protected Sub RSVPForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim hdnTotal As HiddenField
       
        hdnTotal = RSVPForm1.FindControl("hdnTotal")
             
        If Not (hdnTotal Is Nothing) Then
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "hdnTotalIdScript", "var hdnTotalId = '" + hdnTotal.ClientID + "';", True)
        End If
               
    End Sub
---------

In the original user control, there was a call to javascript as follows:
--------
<script language="JavaScript" type="text/javascript">

var obj = document.getElementById(hdnTotalId);
obj.value = form.Total.value;

}
</script>
-------
I am attempting to extract the value of  this "hdnTotalId" in a subroutine.

ANY  HELP would be greatly appreciated.
0
Comment
Question by:Codeaddict7423
  • 2
3 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 34136212
use this

<asp:HiddenField ID="hdnTotal" runat="server" />

0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 34136219
0
 

Author Comment

by:Codeaddict7423
ID: 34136319
YZlat,
Thank you for the quick reply.
I tried your code suggestion but VS is indicating that the name "hdnTotal" is already  being used  by another control
My code is uploaded to a test location:
http://www.h-gac.com/TRAINING/discount_coupon_test.aspx
what hapens is that i the call to "hdnTotalIdScript" to get the value of "hdnTotalId" is not being passed.
-----
 If Not (hdnTotal Is Nothing) Then
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "hdnTotalIdScript", "var hdnTotalId = '" + hdnTotal.ClientID + "';", True)
        End If
------
I am attempting to obtain the value of "hdnTotalId" in my sub (code follows)
-------
 Protected Sub btnVerifyPromos_Click(ByVal sender As Object, ByVal e As System.EventArgs)
               
        ' Creates myConnection as a new sqlconnection and sets it equal to DSN_PROD
        Dim myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("DSN_PROD").ConnectionString)
     
        myConnection.Open()

        ' Build a sql statement string    
        Dim query1 As String = "Select PromoCode, DiscountValue FROM tblPromoCodes WHERE PromoCode = @PromoCode"

        ' Initialize the sqlCommand with the new sql string.  
        Dim Command1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query1, myConnection)
       
        ' Declare txtPromoCode As TextBox and find in the form.
        Dim txtPromoCode As TextBox = DirectCast(RSVPForm1.FindControl("txtPromoCode"), TextBox)
        Dim lbl_Discount As Label = DirectCast(RSVPForm1.FindControl("lbl_Discount"), Label)
        Dim txtDiscountValue As TextBox = DirectCast(RSVPForm1.FindControl("txtDiscountValue"), TextBox)
       
        Command1.Parameters.AddWithValue("@PromoCode", txtPromoCode.Text)
        ' Command1.Parameters.AddWithValue("@DiscountValue", lbl_Discount.Text)
        'Command1.Parameters.AddWithValue("@DiscountValue", txtDiscountValue.Text)
       
        'If there is a single quote in the string below (Dim PromoCode As String = ""),            this causes validation of the to fail
       
        Dim PromoCode As String = ""

        'Dim DiscountValue As Int16 = "0"
        Dim DiscountValue As Integer = 0


        'Create new parameters for the sqlCommand object and initialize them to the input values.    

        'Execute the command
        Dim reader As System.Data.SqlClient.SqlDataReader = Command1.ExecuteReader
       
        If reader IsNot Nothing AndAlso reader.HasRows Then
            reader.Read()
            PromoCode = reader.GetString(0)
            DiscountValue = reader.GetInt32(1)
        End If

        ' Display whether the page passed validation.
        Dim lbl_message As Label = DirectCast(RSVPForm1.FindControl("lbl_message"), Label)
         
        If Not String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            'Response.Write("--" + PromoCode.ToString() + "Accepted")
            lbl_Discount.Text = "Discount Applied."
            txtDiscountValue.Text = DiscountValue.ToString()

        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
            lbl_Discount.Text = "Discount Not Applied."
            txtDiscountValue.Text = "0"

        End If

        ' args.IsValid = False
       
        If Not String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            ' Response.Write("--" + PromoCode.ToString() + "Accepted")
            lbl_Discount.Text = "Discount Applied."
            txtDiscountValue.Text = DiscountValue.ToString()
 
           
        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
            lbl_Discount.Text = "Discount Not Applied."
            txtDiscountValue.Text = "0"

        End If

        '******* Calculate the event price if PromoCode isValid *********
        Dim txtTotal As TextBox = DirectCast(RSVPForm1.FindControl("txtTotal"), TextBox)
        Dim txtNumberofTickets As TextBox = DirectCast(RSVPForm1.FindControl("txtNumberofTickets"), TextBox)

        Dim hdnTotal As TextBox = DirectCast(RSVPForm1.FindControl("hdnTotal"), TextBox)
       
        If Not String.IsNullOrEmpty(txtNumberofTickets.Text) Then

            Dim nt As Integer = Convert.ToInt32((txtNumberofTickets.Text))
            Dim price As Integer = nt * 25
           
            'to have a percent discount from price, use the formula below like  this:
            'Dim total As Decimal = price - (price * DiscountValue / 100)
           
            'to have a hard-dollar discount from price, use the formula above like this:
            Dim total As Decimal = price - DiscountValue

           
           
            txtTotal.Text = total.ToString()
                       
            hdnTotal.Text = total.ToString()

           

        Else
            txtTotal.Text = "0"
        End If
       
        myConnection.Close()
    End Sub
---------------
The call to this sub is as follows:
-----
 <!--Promo Coupon call starts here /\/\/\/\/\/\/\/-->
 
 <tr>
  <td style="text-align: right; white-space: nowrap; height: 25px; background-color:#FFFFFF">
<asp:Label ID="Label2" runat="server" Text="Promo Code:"></asp:Label>&nbsp;</td>
     
<td style="text-align: left; white-space: nowrap; height: 25px;">
    <asp:TextBox ID="txtPromoCode" Width="150px" runat="server"></asp:TextBox>  
 </td>    
   
 <td style="text-align: left; white-space: nowrap; height: 25px;">&nbsp; </td>
 
 <td style="text-align: left; white-space: nowrap; height: 25px;"><asp:Button ID="btnVerifyPromos" runat="server" OnClick="btnVerifyPromos_Click" Text="Verify"  />
     <asp:Label ID="lbl_message" runat="server" Text="" ></asp:Label><br />
     <asp:Label ID="lbl_Discount" runat="server" Text="" BackColor="white"></asp:Label>
 </td>      
 
  </tr>
-----------

ANY HELP would be greatly appreciated.


0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

14 Experts available now in Live!

Get 1:1 Help Now