Solved

Retrieving value of control in Javascript

Posted on 2010-11-28
8
648 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

617 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