Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript VB.NET
Have a function in javascript as :
 
window.onunload = unloadPage;
function unloadPage()
        {
        var abc= <%=Price %>
        alert(abc)
}
 
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
Comments
Ankur\m/ at 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
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
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.
    alert(abc) 
}
 
hope it helps.
  Permalink  
Comments
swatiiiiiiiiii at 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 at 9-Nov-12 1:10am
   
as i think, hidden field id are not static always, unless we st staticClientID="true".
swatiiiiiiiiii at 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
0 Sergey Alexandrovich Kryukov 585
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Raul Iloc 160
0 Sergey Alexandrovich Kryukov 7,295
1 OriginalGriff 6,841
2 CPallini 5,420
3 George Jonsson 3,654
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
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