Solved

ASP.net Retrieve Clientside Hiddenfield.value on Button Click

Posted on 2010-09-06
13
486 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
[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
  • 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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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

623 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