C# - Calling Functions From Code File With Javascript?
Feb 24, 2010
I have a JavaScript-based timeline which needs to use data from an SQL server. The queries and the JSON conversions have been worked out in the form of C#.NET functions within a code file related to an .aspx page. So for a complete newbie to ASP.NET, how do you set up a function call like this from JavaScript?I would love it if someone had some simple example code I could learn/get some pointers from.Edit: I am using .NET 3.5
I am trying to call some javascript functions which I ahve written in some file. Eg.
function OpenPopup() { alert("OpenPopUp"); return false; }
when I call this from button from OnClientClick = "OpenPopup()" it is not called but when I put this function on MasterPage it is able to call the function.I added this is the MasterPages's Head
I have a user control which opens a new window for some item selection. Once an item is selected on that window, the item ID should be passed to a callback javascript function in the user control. In order to have a unique callback function name in the user control (it may have multiple instances on the same page), I added the user control ID to the function name as follows (defined in the ascx file):
function OnItemSelection_<%=this.ID%>(selectedItemID) { OnItemSelection("<%=SomeControl.ClientID%>", selectedItemID) } Where OnItemSelection is defined in an external js file: function OnItemSelection(controlID, selectedItemID) { $(controlID).do_something(selectedItemID); } The issue is that now I'm not sure how the new page can call the "right" function (of the user control instance where it was opened from). The only idea I have in mind so far is passing the name of the callback function in the query string: var CallbackFunction = function(selectedItemID) { window.opener.<%=Request.QueryString["CallbackFunctionName"]%>(selectedItemID); } or maybe just the opening user control ID: var CallbackFunction = function(selectedItemID) { window.opener.OnItemSelection_<%=Request.QueryString["UserControlID"]%>(selectedItemID); }
My questions are:
1) Is it a bad practice passing javascript function names (or user control IDs) like that (I suspect it is)?
2) What could be better way to handle this scenario? Edit: Another way I'm considering now is declaring a global variable in the user control (registering it from the code behind so it'll appear only once). Then, when opening the new window, it's possible to assign the relevant function to it (OnItemSelection_<%=this.ID%>), and call it from the new window. Seems much cleaner than passing user control data through the query string.
I have a page with three HTML labels and their corresponding ASP.NET gridviews contained within divs. Now while learning jQuery, I am trying to achieve two things:1. Change the css class of the lables upon mouse hover/out.2. Slide up/down the grid div upon clicking of the labels.It looks to be working as expected, but I wish to know if I am doing it the right way.My complete jQuery code is:
And I am calling these function from onmouseover, onmouseout and onclick events of the label controls passing in the label ID as parameter. As an example:
<label id="lblWebComp" class="gridLabelDefault" onmouseover="ColorChange('lblWebComp')" onmouseout="ColorChange('lblWebComp')" onclick="ShowHide('gvDivWC')"> Web Components </label>
Kindly let me know if this is the best way to achieve these effects? Don't I have to right the document ready function in the jQuery code?
I have a web application that will need to access code behind methods as well as javascript methods. For this particular implementation it doesn't really matter which order they are called in from a program flow perspective.
I'm looking for insight into when it would be appropriate to use a code behind to call javascript and when it would be appropriate to call javascript from a code behind. Are there any ramifications of doing it one way or the other that I should be aware of before moving forward with an implementation?
Is there a best practice way to do it or is it very specific to the actual implementation?
So I have this idea of creating a asp.net user control, to handle all forms of modal popups (whether it be error handling, forms, dialogs, etc).
I already created this before, placing the user control in the top master page, exposing it to all content pages, so I could do something like:
Master.Popup.ShowException(Exception);
And the usercontrol itself would have the necessary markup to look like a modal dialog, and in the show method just do a .Visible = true.
This is all fine, but I've started thinking of implementing a bit of style with jquery. I'd like for the usercontrol to have some sort of jquery animation on show. But I don't know how to go about achieving this, as I don't know how I would call that jquery function from the codebehind instead of the popup.visible = true.
I have an .aspx.cs page in my solution and I'm trying to call a function which is located in a Master page.Would anyone be able to offer any pointers on how to achieve this as I cannot figure it out - thought it would have been fairly straight forward!
I am using one webservice, which is for .NET platform, for address validation.Its running successfully on .NET (C#). However, I also want to use the same service from classic asp (legacy code) pages. Would appreciate if someone could give a clue how togo about it? Here are some ideas that I came up with and would appreciate if you could add to it
I have a java script function which presents a countdown timer.when the timer reaches zero, I want to somehow activate a function on the server side in the code-behind.I understand it is impossible to directly call a server-side function, but is there a simple way to still fire it?I want to fire the (event-handler) code behind function: protected void ButtonFinish_Click(object sender, EventArgs e).
var _countDowncontainer = 0; var _currentSeconds = 0; function ActivateCountDown(strContainerID, initialValue) { _countDowncontainer = document.getElementById(strContainerID); [code]...
I get this error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Partial Class _Default Inherits System.Web.UI.Page <WebMethod()> _ Public Shared Function ParseData() As String Dim value as string = GetValue() End Function Private Function GetValue() as String Return "halp" End Function End Class
I know it has something to do with the fact that the first function is shared and the second function should probably be Public as well but I don't fully understand the reason behind it. Probably not relevant but I'm calling the web method from some javascript.
I am using Ajax's EnablePageMethods way to call server side code using javascript. The problem is that in IE8 the page automatically refreshes after the ajax call has been completed i.e. the server side function has been executed successfully. I want the same to happen with Chrome and Firefox but it doesnt refresh the page once the server side function has been executed.
I am using this way to call server side code from Javascript -- [URL]
I am trying to call a JavaScript function from my ascx control code behind in the catch blockI have tried the below two ways but they don't seem to work.
I have a page that contains a few user controls which all use an id in a querystring. I want to change this so that the page can send the id to the user control and can call a function on the user control.
Also is it possible for the user control to get the id from the page or a control on the page as its parent?
I've done a search and read a few things, All i'm trying to do is set a variable in the code file and call it in the page file asp.net vb.net 3.5. I've tried like this in the code file:
[Code]....
And in the design part of the page:
[Code]....
But it says that name isn't declaired. What am I doing wrong?
I think I summed up the question in the title. Here is some further elaboration...I have a web user control that is used in multiple places, sometimes more than once on a given page.The web user control has a specific set of JavaScript functions (mostly jQuery code) that are containted within *.js files and automatically inserted into page headers.However, when I want to use the control more than once on a page, the *.js files are included 'n' number of times and, rightly so, the browser gets confused as to which control it's meant to be executing which function on.What do I need to do in order to resolve this problem? I've been staring at this all day and I'm at a loss.
I have a website that shows images people have uploaded.
[URL]
I want to be able to show a popup window when i move the cursor over an image. It should show information about the user that i take from the database.
I have found a javascript that works, it fades a window in and out. But i cant call it from codebehind.
I've looked at about 5 Questions here on SOF and haven't really found a useful answer for this. How am I supposed to invoke my javascript functions from client-side events in my .ascx controls? Here is my .ascx file.. what most the answers have led me to:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UIMenu.ascx.cs" Inherits="controls_UIMenu" %> <script language="javascript" type="text/javascript"> function insetItem(myMenu) { myMenu.setAttribute("class", "myClass");.......
I'm getting different errors when doing it this way. ".....ascx does not contain a definition for "insetItem"", and my other pages that use this file don't recognize it anymore. When I try to put the js on the actual .aspx pages that will be using it, I get the same no definition error.
I just started learning jscript, still pretty noob. I'm trying to make it so when the user down clicks a menu item, I want that item's borderstyle to change to "inset". I'm not sure how to change that attribute for the specific item that gets clicked either in javascript >_< but that's another question.
I have a cascading dropdown. After the dropdown is populated(it is in another file) i would like to return back to the original form where the control is placed. is there anyway of doing it
I wanted to use some javascript functions in a ASP.net 3.5 web site. I have added a javascript1.js page in the Sites Folder with a Javascript Test function.
I'm not familier with Javascript, and I have to convert a Project's Javascript codes to Jquery scripts.
ofcourse I'm not project leader or something like it, I'm just elaborating that this process may has what kind of issues and may face with what kind of needness ?
So if you know any solution to convert Javascript Functions to Jquery ones
I have a code behind page that has several method; one of them is a page method.
[WebMethod] public static void ResetDate(DateTime TheNewDate) { LoadCallHistory(TheNewDate.Date); } protected void LoadCallHistory(DateTime TheDate) { bunch of stuff }
The method LoadCallHistory works fine when the page loads and I can call it from other methods inside the page. However, in the web method part, it gets underlined in red with the error "an object reference is required for the non-static field".
How do you access functions from the page method part of the code?