Click here to Skip to main content
12,953,175 members (59,221 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Have a function in javascript as :

window.onunload = unloadPage;
function unloadPage()
        var abc= <%=Price %>


Price is the variable in codebehind and its value is set in Datagrid OnPageIndexChanged event.

VB.NET Code is:
Public Price As String

Protected Sub dgttumfileview_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgttumfileview.PageIndexChanged
                   Price = "True"
               End Sub

But I'm getting value of Price as 'undefind' in alert. How can I get value as "True" which is set in codebehind.
Posted 8-Nov-12 18:26pm
Ankur\m/ 9-Nov-12 0:51am
Have you defined public get property for Price (Price should be the name)? Declaring Price just a variable won't do.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


some times the values set in code behind will not appear in javascript. so, i suggest to use hidden field instead of variable.

add one hidden field to the source and set the value in code behind and read from javascript function.

function unloadPage()
    var abc= document.getElelmentById('hdfID').value;
    //hdfID is the id of hidden field.

hope it helps.
swatiiiiiiiiii 9-Nov-12 0:51am
Ya karthik, I tried using Hiddenfield but the value does not show for the first time. I'm initializing 'hdfID'=False and setting the value as "True" in codebehind but for the first time it gives me value as "False" and works from second time. And even hiddenfields are static so I dont want to use them.
Karthik Harve 9-Nov-12 1:10am
as i think, hidden field id are not static always, unless we st staticClientID="true".
swatiiiiiiiiii 9-Nov-12 1:16am
even then hiddenfield values are not retained for the first time. That's the issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100