Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

convert textbox into hiddenfield

Posted on 2010-11-15
3
Medium Priority
?
731 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

650 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