Wednesday, March 28, 2012

Calling javascript from ASP.NET AJAX

I am trying to read a cookie with some javascript, without postback, using a timer control. I all need to do is return the value of the .js file and use it in the timer event.

So far here's what I have done:

I have a ScriptManager and an updatepanel with a timer control inside.

I set the ScriptReference to the file that reads the cookie:

<Scripts>
<asp:ScriptReference Name="ReadCookie" Path="javascripts/ReadCookie.js" />
</Scripts>

The timer calls this event:

protected void Timer1_Tick(object sender, EventArgs e)
{

}

My Question is how do I call that cookie and access the value in my code behind?

Thanks, Justin.

I guess I'll should restate my question:

How do I call a javascript function that has been added to the scripts collection of the scriptmanager (as shown above) from my c# codebehind?

Thanks, Justin.


You can register that JavaScript function in Timer's Tick Event.


Yes, I have tried that with no success but maybe I am doing it wrong:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Scripts>
<asp:ScriptReference Path="ReadCookie.js"/>
</Scripts>
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="readCookie">
</asp:Timer>

....

readCookie.js:

function readCookie() {
var nameEQ = "PhoneNumber" + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}

if (c.indexOf(nameEQ) == 0){
return c.substring(nameEQ.length,c.length);
}
else {
return null;
}
}
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

I Get this Error:

Compiler Error Message:CS0117: 'ASP.agents_customerdetailsform_aspx' does not contain a definition for 'readCookie'


Hi, Justin


Check this link for answer and more help:

  • CS0117 :: 'Type' does not contain a definition for 'Identifier' :http://dotnetslackers.com/_NET/re-56753_CS0117_Type_does_not_contain_a_definition_for_Identifier.aspx

  • No comments:

    Post a Comment