Solved

Retrieving value of control in Javascript

Posted on 2010-11-28
8
645 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:Starr Duskk
  • 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 2

Author Comment

by:Starr Duskk
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Author Comment

by:Starr Duskk
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 2

Author Comment

by:Starr Duskk
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 2

Author Comment

by:Starr Duskk
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

770 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