Solved

ASP.net Retrieve Clientside Hiddenfield.value on Button Click

Posted on 2010-09-06
13
480 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:karlblackburn
  • 7
  • 6
13 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33612526
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
 

Author Comment

by:karlblackburn
ID: 33612597
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33612603
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
 

Author Comment

by:karlblackburn
ID: 33612788
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33612808
Can you post the markup you are using and the javascript you are using to set the value?
0
 

Author Comment

by:karlblackburn
ID: 33612865
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33613115
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
 

Author Comment

by:karlblackburn
ID: 33613134
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33613198
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
 

Author Comment

by:karlblackburn
ID: 33613249
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33613268
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
 

Author Comment

by:karlblackburn
ID: 33613321
Ahh.. I shall give it a go :)
0
 

Author Comment

by:karlblackburn
ID: 33613381
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

759 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

20 Experts available now in Live!

Get 1:1 Help Now