Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

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

  • Calling javascript from a js file

    I have a js file that contains the function that i want called when the user clicks on the textbox and the calendarextender is shown

    calendarextender.onShown = functionName

    Working with a single page i have put this at the top inside the head

    <script type="text/javascript" src="http://pics.10026.com/?src=calendar.js"> </script>

    With a single page is works perfectly but now i want to make it add this to a master page

    creating a masterpage i have also added the same line inside the head, but when i try and run my content page and click on the textbox i get "Error: object is required"

    I have viewed the source and made sure that the <script> line is present. Also have tried pasting the function directly on the master page instead of pointing to a file

    <script type= "text/javascript" >

    function dosomething() </script>

    and i get the same error

    anybody got a suggestion on how i can get my master/content page to work with my js file

    Thanks

    allan

    I could be wrong, but I don't think the <head /> element from the Master Page is included when the page is sent to the browser - you normally set the info in the content page as you'd expect things like the title and meta tags to be different.

    You can do a quick "view - source" on the executed page to check this and if it's the case, you could included the script tag right at the top of your <body /> element - as long as it appears before the call to the function.

    Monday, March 26, 2012

    Call the method bound to an linkbutton after ajax callback

    Hi everybody

    I just have the following problem:

    I created a ASP-LinkButton and added a method in the code-behind file for its onclick event. Then I added a break point at the beginning of the method to check if it's called.

    Now I added a JS which calls the __doPostback("xxx", ""); exactly like the LinkButton does and everything works fine.

    Now I add a Webservice, which validates my input and if everything works fine, its callback should call the click method of the asp-button. Guess what happens: the postback is called, but not the event of the button. If I do this before outside the callback - everything works fine, so I know the code is correct.

    I even tried to add a "window.setInterval" timer which always checks a bool and calls the postback as soon as the bool is turned to true ... Then I set the bool after the callback and guess again: The postback is called but not the method ...

    I don't have any further ideas for workarounds - anyone got a hint for me?

    THANKS!

    Hi,

    Now I get a general idea of your situation. But your description doesn't give me sufficient information to find the cause.

    Accordingly, I made a sample, please try it.

    <%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services><asp:ServiceReference Path="WebService.asmx" InlineScript="true" /> </Services> </asp:ScriptManager> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton></div> </form> <script type="text/javascript"> var count = 0; function callBack(result) { if(result) __doPostBack('LinkButton1',''); } function callWS() { count++; WebService.ShouldCallBack(count, callBack); } window.setInterval(callWS, 2000); </script></body></html>
    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass Default3 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { }protected void LinkButton1_Click(object sender, EventArgs e) { Response.Write("Linkbutton is clicked on " + DateTime.Now.ToString()); }}

    <%@. WebService Language="C#" Class="WebService" %>using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService { [WebMethod] public bool ShouldCallBack(int count) { return count == 5; } }
    Hope this helps.

    Call remotely hosted web service from client-side?

    All of the examples I have seen call the *.asmx file using a relative path but I need to reference a URL such ashttp://www.domain.com/webservice.asmx to call a remote web service. In the Atlas days I believe this was done using a bridge (*.asbx) but I can't seem to find any recent information regarding this method. Does it still exist?

    Anyone know how to accomplish this?

    Thanks,
    Janea

    Hi,

    my buddy Garbin (Allesandro Gallo) wrote an interesting article about this recently:Mash-it Up with ASP.NET AJAX: Using a proxy to access remote APIs.

    Grz, Kris.


    Wow!Yes What an excellent article!! I actually ran across it when I was initially searching for an answer but I thought there might be an easier way, however it makes a lot of sense why it is done the way he describes it. So, I'm going to try it out... create a local web service to be used as a proxy to connect to the URL containing the web service and call the remote procedures from there. Seems like a simple concept... I feel like I should've figured it out on my own. Well we'll see how it goes... Thanks a ton Kris!