Link to home
Start Free TrialLog in
Avatar of karlblackburn
karlblackburnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of karlblackburn

ASKER

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.
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

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?
Can you post the markup you are using and the javascript you are using to set the value?
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
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

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?
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

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?
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh.. I shall give it a go :)
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.