Click here to Skip to main content
12,397,805 members (59,530 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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/ 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 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


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2016
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