Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieving value of control in Javascript

Posted on 2010-11-28
8
Medium Priority
?
651 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 83

Accepted Solution

by:
leakim971 earned 1000 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:Alfred A.
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 21

Assisted Solution

by:Alfred A.
Alfred A. earned 1000 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:Alfred A.
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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

877 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