Solved

Retrieving value of control in Javascript

Posted on 2010-11-28
8
643 Views
Last Modified: 2013-11-07
I have a hidden field in my master page. I default the value to "Start"

On my master page, I have a function that sets the value of the hidden field. I change it to "Save" in the codebehind:
hdnIsDirtySave.Value = "Saved"

I wrote a javascript function that checks the value of this hidden field when someone clicks a menu item. By this point, the value has been changed in the codebehind. When I check the value, even after having set it to "Saved", it still displays as "Start"

 
    alert(document.aspnetForm['ctl00$ctl00$hdnIsDirtySave'].value);

Open in new window



In reality, I build this javascript function like so, but the above is the view source version when it hits the web page. So yes, I am passing my control in by name to my function that builds and registers the clientscriptblock:

           Dim p As Web.UI.Control = control.Parent
            While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
                p = p.Parent
            End While
            .Append("alert(")
            .Append("document.")
            .Append(p.ClientID)
            .Append("['")
            .Append(control.UniqueID)
            .Append("'].value")
            .Append(");")
            .Append(Microsoft.VisualBasic.Chr(13) & Microsoft.VisualBasic.Chr(10))

Open in new window

         

But it just isn't retrieving that value for the javascript. Any ideas?

thanks.

BTW, I have also tried these and they just return null:
    alert(document.getElementById('hdnIsDirtySave'.value));
    alert(document.getElementById('ctl00$ctl00$hdnIsDirtySave'.value));
0
Comment
Question by:BobCSD
  • 4
  • 3
8 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34227017
Use : document.getElementById("<%= HiddenField1.ClientID %>").value

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
0
 
LVL 1

Author Comment

by:BobCSD
ID: 34227128
Yes, that's how to do it in code in front. My version shows how to retrieve the value in a .js file or by building the script in code behind.

My problem is not that I'm not getting the value. I'm getting the value, but it is displaying as the default value, not the new value after I changed the value.

Your solution also displays "Start" instead of "Saved."  it doesn't display the value as redefined in the codebehind.
0
 
LVL 21

Expert Comment

by:Alfred1
ID: 34227315
When you update your hidden field, do you refresh the whole page upon postback?   You need to postback whatever changed you did in your code behind in order to get the new value of your hidden field.
0
 
LVL 21

Assisted Solution

by:Alfred1
Alfred1 earned 250 total points
ID: 34227335
Also, I just don't exactly know how you register your hidden field as I only see a partial code snippet above, have a look at ClientScript.RegisterHiddenField

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerhiddenfield.aspx

I hope this helps.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:BobCSD
ID: 34227677
Alfred,

I define my hidden field in code-in-front like so:
    <asp:HiddenField ID="hdnIsDirtySave" value="Start" runat="server" />

I don't "register" it though. Is that what I need?
The problem with the code you provided is this:
ClientScriptManager cs = Page.ClientScript;
or in my vb.net case:
Dim cs As ClientScriptManager = Page.ClientScript

I'm setting it up in a master page and there is no Page.ClientScript option.

How does one Dim a ClientScriptManager on a master page?

I'm assuming after I have the cs object, I can then do this:
cs.Page.ClientScript.RegisterHiddenField("hdnIsDirtySave", "Begin")

And then it will play nicely with the javascript? Is that what we're heading to?

thanks.


0
 
LVL 1

Author Comment

by:BobCSD
ID: 34227691
I have tried this to register the hidden field on the page of the hiddenfield control (control is the hdnIsDirtySave field passed into the function:


        control.Page.ClientScript.RegisterHiddenField("hdnIsDirtySave", "Begin")
        control.Page.ClientScript.RegisterClientScriptBlock(control.GetType(), "checkIsDirtySave", sb.ToString)

It really does nothing. When I view the alert, it is still "Start" (Not "Begin" and not "Saved" as I change it to.)

0
 
LVL 21

Expert Comment

by:Alfred1
ID: 34227755
OK.  Do you have a form tag in the body of your Master page?  If you have this one, you should be able to access Page.ClientScript directly.

Also, what event or action cause the hidden field to be updated?  Does this action cause a postback on the page?  If it does, it should update the field and you can access the new value of the hidden field.  For example, you can use an ASP.NET Button Control (runat="server") to execute a server side event (click) and postback.
0
 
LVL 1

Author Comment

by:BobCSD
ID: 34228058
I created a separate project with just a master page and default page and got it to work. Now I have to figure out what is failing in my major project. i'm using the hidden field in the code in front and doing nothing fancy with registering it in the code behind. I'm just building the javascript and registering it as I indicated in my first post.

I'll have to work my way through the code to figure out what's going on.

thanks!
0

Featured Post

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

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

747 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

16 Experts available now in Live!

Get 1:1 Help Now