ASP.net Retrieve Clientside Hiddenfield.value on Button Click

Hi,

I am positive I have acheive this before, but im racking my brains.

I use Hiddenfields to store various bits of information whilst using Javascript to manipulate the page, the problem im having is on pressing the "add to basket" button, the hiddenfields revert to the original values?

Any ideas?

Kind Regards
Karl
karlblackburnAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
That's partly true and partly not. If you use "Value" then IE will read and write a psuedo-attribute called "Value", which isn't the same as the fields "value" property, which is what gets posted back to the server.
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you're manipulating them through Javascript then you'll need to retrieve the updated value from the Request.Form object using the UniqueID of your field as the key:
string updateValue = Request.Form[YourHiddenField.UniqueID];

Open in new window

0
 
karlblackburnAuthor Commented:
I'll test this shortly, but if my hiddenfiekd was called hiddenfield1 for example what would the unique I'd be?  Many thanks for your help.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Carl TawnSystems and Integration DeveloperCommented:
If it's an ASP HiddenField control then it would be (assuming it's not inside a repeater or grid, or some other nested control):


string value = Request.Form[hiddenfield1.UniqueID];

Open in new window

0
 
karlblackburnAuthor Commented:
I tried the below

        Dim value1 As String = Request.Form(HFballoonbdesc.UniqueID)
        Page.Response.Write(value1)

to no avail, if I do a javascript alert on the actual hiddenfield it gives me the correct value, but after button click its blank.

Sorry, any ideas?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Can you post the markup you are using and the javascript you are using to set the value?
0
 
karlblackburnAuthor Commented:
I have just written an example as I was trying it on a fresh page just in case something was interfering with it.

ASPX PAGE

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script type="text/javascript" language="JavaScript" />
function test(desc)
{
    document.getElementById('<%= HiddenField1.ClientID%>').Value = "testing";
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<a href="#" onclick="javascript:test('Foil Circle White')"><asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton></a>
   
</asp:Content>

CODE BEHIND


Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

        Page.Response.Write("THIS IS BEING WRITTEN TO SCREEN")
        Dim value As String = Request.Form(HiddenField1.UniqueID)
        Page.Response.Write(value)

    End Sub
End Class

Cheers
0
 
Carl TawnSystems and Integration DeveloperCommented:
OK, the problem is stemming from the fact that you essentially have one anchor tag wrapped in another. Therefore the outer one (the one with the javascript) isn't executing, and it's simply the postback for the inner one that is executing.

In order to do what you are trying to do in that sample you would use the OnClientClick of the LinkButton:
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="javascript:test('Foil Circle White');">LinkButton</asp:LinkButton>

Open in new window

0
 
karlblackburnAuthor Commented:
Hi,

I changed the main page to

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script type="text/javascript" language="JavaScript" />
function test(desc)
{
    alert(desc)
    document.getElementById('<%= HiddenField1.ClientID%>').Value = [desc];
    alert(document.getElementById('<%= HiddenField1.ClientID%>').Value)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="javascript:test('Foil Circle White');">LinkButton</asp:LinkButton>
   
</asp:Content>

and used alerts to check the javascript fires, which is fine, it just doesn't pull through to the actual capture server side?
0
 
Carl TawnSystems and Integration DeveloperCommented:
You've run into a case-sensitivity issue. The javascript "Value" needs to be "value":
function test(desc)
{
    document.getElementById('<%= HiddenField1.ClientID%>').value = desc;
}

Open in new window

0
 
karlblackburnAuthor Commented:
In the above example I did an alert to check the hiddenfield had updated which it had,

Then after the button click and postback I can't retrieve?
0
 
karlblackburnAuthor Commented:
Ahh.. I shall give it a go :)
0
 
karlblackburnAuthor Commented:
I cant tell you how thankful I am, I have been going at that since this morning and its been doing my brain in. AGain, much appreciatted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.