Reference An Anonymous JavaScript Function?

Apr 14, 2010

I'm trying to call a Page Method using a jQuery 'attached' event function, in which I like to use the closure to keep the event target local, as below, but page method calls declare several 'error' functions, and I would like to use one function for all of them. If, in the below code, I was handling an error and not success, how could I use my single, anonymous handler for all 3 error functions?

$(":button").click(function () {
var button = this;
PageMethods.DoIt(
function (a, b, c) {
alert(button);
});
});

This example passes an anonymous function for the success callback. There is only one of these. If I was passing an error callback, how could I use 'function (e, c, t)' for all 3 error callbacks?

ADDED: What I would like to do here is trigger an AJAX call whenever the user clicks a toggle button (checkbox), but to improve responsiveness, I want to toggle the button state immediately, and only 'untoggle' it if the AJAX call fails.

Now, in my client-side click() event handler, I would like to use anonymous functions inside the scope of click()' so that the functions have access to thethisevent argument, but I don't want to 'declare' three functions for theonTimeout,onError, and 'onAbort arguments of the PageMethods.MyFunction function. if I declare a named function outside of the click handler, it no longer has access to the 'this' parameter of the click() event handler.

View 6 Replies


Similar Messages:

Pass An Anonymous Array Of Strings To A JavaScript Function?

Apr 5, 2010

I want to pass to an array of controls' IDs to a javascript script function so it will switch control's enable state.

For example, in C# it would be like this:

func(false, new[] { "Control1", "Control2", "Control3" });

In that function I want to find corresponding controls and disable/enable them. For one control I do this next way:

<script type="text/javascript" language="javascript">
function switchControls(value, arr) {
for (var n = 0; n < array.length; n++)
document.getElementById([n]).disabled = value;
}
</script>
<asp:CheckBox runat="server"
onclick="switchControls(this.checked,
[
'<%= Control1.ClientID %>',
'<%= Control2.ClientID %>'
])"
Text="Take?" />

How to implement this properly? Have I to use jQuery?

View 3 Replies

MVC :: Using An Anonymous Function For An Action Result?

Oct 14, 2010

i can assign a simple anonymous function and it works, but i'm having trouble with a more complex one.

i am attempting to disable an image button after it is pressed.

any alternatives would be welcome, but i think this is the recommended

[Code]....

View 2 Replies

How To Anonymous Function Fire On Grid.prerender?

Apr 23, 2010

In my gridview I have fields for inserting a new record in the footer.

In my objectdatasource selecting event if no records came back I bind a single mock row to force the footer to show so they can still add records. Since the row does not contain real data I hide the row.

[Code].....

View 3 Replies

C# And Anonymous Types - Iterate Through A DataTable While Manually Building An Anonymous Type

Jan 18, 2010

I am currently implementing a client-side paging solution using ASP.NET, jQuery and JSON.

I have been following the excellent article from encosia: http://encosia.com/2008/08/20/easily-build-powerful-client-side-ajax-paging-using-jquery/

In my Web Method I retrieve my data from the database as a DataTable:

DataTable categoryProducts = ProductViewerAccess.GetCategoryProducts
("AA", 4, 0, Page.ToString(), out howManyPages, "FALSE", 0, "CostPrice", "asc", destinationList);

I then retrieve the data from the DataTable into an anonymous type:

var feeds =
from feed in categoryProducts.AsEnumerable()[code]....

This all works great.

However, I would like to extend the code to perform some evaluation checks (e.g., check that various columns in the DataTable are not NULL) and other pre-processing (e.g., call various functions to build the image URL based on the image ID - which is another column in the DataTable not shown in the code fragment) before I return the resulting rows of the DataTable as an anonymous type to the client-side.Basically, I want to iterate through the DataTable, perform the evaluation checks and pre-processing, while building my anonymous type manually as I go. Or maybe there is a better way to achieve this?

View 2 Replies

Web Forms :: Getting Error Calling Javascript Function From Another Javascript Function

Jan 3, 2011

Getting error calling Javsscript function from another Javascript function

[Code]....

View 4 Replies

Call Javascript Function And Server Side Function From Linkbutton

Jan 27, 2010

<asp:LinkButton CssClass="button" ID="btnApply" runat="server" OnClick="btnApply_Click()" OnClientClick="Apply1('btnApply')" >
hi ihave this functin in .vb file
Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
end sub

and javascript function also in aspx

function ApplySummerization(id)
{
alert("hai");
}

View 4 Replies

How To Replace A Function Name Using JavaScript And Have It Be Recognized As A Function

Aug 25, 2010

I am trying to replace the JavaScript onclick event handler in ASP.NET that is added to a button control when using validation controls. This is what is output into the HTML from ASP.NET in this scenario:

<input type="image" name="ibSubmit1" id="ibSubmit1" src="button-green-submit.gif" onclick="showProgress1();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ibSubmit1", "", true, "Group1", "", false, false))" style="border-width:0px;" />

I have looked pretty estensively, and unfortunately there doesn't seem to be a way to modify the function server side before it is injected into the page.

Since I am developing a control and desire it to be non-invasive and self contained, and I am interested in obtaining the validationGroup parameter of the WebForm_PostBackOptions object, it seems that the easiest solution would be to use JavaScript to replace the WebForm_DoPostBackWithOptions function name with my custom wrapper function and leave all of the rest of the parameter information intact - then I can extract the information I am interested in, call my custom functions, and then forward the call on to WebForm_DoPostBackWithOptions.

NOTE: I am using jQuery to build my custom function, so if there is an easier way to do this with jQuery it is an option I will consider.

Here is the code I tried to replace the onclick event handler (not working):

$('[onclick*=WebForm_DoPostBackWithOptions]').each(function() {
var txt = this.onclick;
txt = txt + '';
txt = txt.replace('WebForm_DoPostBackWithOptions','ml_DoPostBackWithOptions');
this.onclick = eval(txt);
});

Using alert(), I verified that the text is being changed correctly, however whether or not I use the eval() function, the onclick handler doesn't seem to recognize it as JavaScript.

I thought of using a regular expression to get the validationGroup value, but this seems like it will be far more elegant and flexible if I can get it working...

Note: If there is a way for my control to interrogate the page it is on to find all of the buttons that will post back (regardless of what type of buttons they are) so I can retrieve the property server-side, this is also something I will consider.

View 3 Replies

Call Codebehind If Javascript Doesn't Exist In Browser - Else Javascript Function

Sep 29, 2010

In asp.net page, How can i call the javascript methods for form processing-submitting if the user browser supports javascript and use code behind events if the browser does not support javascript.I have the javascript code to send the form data to an ajax server page using jquery. Don't know how to invoke the needed one based on the browsers javascript availability

View 1 Replies

C# - Reference An ASP Control Inside A Function Or Class Method?

Oct 7, 2010

How do you reference an asp.net control on your page inside a function or a class.
private void PageLoad(object sender, EventArgs e)
{
//An example control from my page is txtUserName
ChangeText(ref txtUserName, "Hello World");
}
private void ChangeText(ref HtmlGenericControl control, string text)
{
control.InnerText = text;
}
Will this actually change the text of the txtUserName control?
I tried this and is working
private void PageLoad(object sender, EventArgs e)
{
ChangeText(txtUserName, "Hello World");
}
private void ChangeText(TextBox control, string text)
{
control.Text = text;
}

View 2 Replies

Web Forms :: Invoke C# Function From Another Project Without Adding Reference?

Sep 24, 2010

There is a C#.net libraray project whose dll name is customer.dll. It has a class name customer with a function name show(). I want to call this function from another project but dont want to add reference to the caller project. Can this be done? Is there any C#.net class that can acheive this?

View 1 Replies

JavaScript - User Control JavaScript Function Doesn't Run

Sep 22, 2010

I have the function put here like below:

$(document).ready(function () {
UserControlNameInit();
});

The script are put in the following and the block is in the .ascx page.

<script type="text/javascript">
</script>

However, the function UserControlNameInit() does not run when the page loads. It is showing in the page source. I can still call this function through FireBug console by manually typing the name of the function.

I did the same way with other user controls, and it works. Just 1-3 user controls are not working...

View 1 Replies

How To Use Dynamic Javascript Reference

Apr 18, 2010

I've inherited some code (not mine- I swear!) which uses a session variable in the header of the HTML to determine which javascript file to link to.

<SCRIPT language="javascript" src="../JavaScript/<%=Session("jsFileName")%>.js"></SCRIPT>

It does work, except that it won't let me change to design view. It gives the message "Could not open in design view. Quote Values differently inside a '<%... "value" ...%>' block.

View 3 Replies

Using Javascript Local Reference?

Jan 25, 2010

In .Net I can use the "~/image/image.png" as a reference to a graphic in the image file in the image folder from whatever file or folder I am working with. How do i use this type of reference in a javascript where I need to reference a source file?

View 4 Replies

Add JavaScript Reference From Code Behind (C#)?

Sep 13, 2010

Is it possible to add javascript reference dynamically from code behind aspx.cs?Like this:

private void AddScriptReference(string path)
{
//Add reference to <head></head>
}

[code]...

View 2 Replies

JQuery :: Reference A Javascript File In .net?

Jul 7, 2010

I have a hquery plugin that consists of jquery itself, 2 .js files and some script to configure the plug-in which resides on the page. I noticed that in .net you can create a .js file.What I would like to do is put the configuration inside a .js file created in .net, but how do I then reference this in the header of the page?am I over complicating the issue or just missing something?

View 5 Replies

Modify Every File That Contains A Javascript Reference?

Mar 24, 2010

I have some pages that reference javascript files.

The application exists locally in a Virtual Directory, i.e. http://localhost/MyVirtualDirectory/MyPage.aspx

so locally I reference the files as follows:

<script src="/MyVirtualDirectory/Scripts/MyScript.js" type="text/javascript"></script>

The production setup is different though. The application exists as its own web site in production, so I don't need to include the reference to the virtual directory. The problem with this is that I need to modify every file that contains a javascript reference so it looks like the following:

<script src="../Scripts/MyScript.js" type="text/javascript"></script>

I've tried referencing the files this way in my local setup but it doesn't work.

View 3 Replies

JQuery :: How To Make Reference A Javascript File In .net

Apr 21, 2010

I have a hquery plugin that consists of jquery itself, 2 .js files and some script to configure the plug-in which resides on the page. I noticed that in .net you can create a .js file.What I would like to do is put the configuration inside a .js file created in .net, but how do I then reference this in the header of the page

View 1 Replies

Properly Reference A JavaScript File In A Project?

Apr 16, 2010

I have some pages that reference javascript files.

The application exists locally in a Virtual Directory, i.e. http://localhost/MyVirtualDirectory/MyPage.aspx

so locally I reference the files as follows:

<script src="/MyVirtualDirectory/Scripts/MyScript.js" type="text/javascript"></script>

The production setup is different though. The application exists as its own web site in production, so I don't need to include the reference to the virtual directory. The problem with this is that I need to modify every file that contains a javascript reference so it looks like the following:

<script src="../Scripts/MyScript.js" type="text/javascript"></script>

I've tried referencing the files this way in my local setup but it doesn't work.

View 1 Replies

Call Javascript Function Of Child Page From Master Page Javascript?

Jul 28, 2010

I am writing an ASP.Net application. I am making use of master page in it. I have several child pages with me, which consist of some java script functions; Let's say;

function ChildPageFunction()
{
//Do something;
}

And master page java script function as;

function MasterPagefunction()
{
//Need to call ChildPagefunction(); here
}

Now is it possible to call ChildPageFunction() from MasterPageFunction()?

View 2 Replies

JQuery :: How To Include Javascript Reference In Master Page

Nov 2, 2010

I am using Asp.Net C# 2.0. My website is working fine in local. Website contains 2 js files included in master page. It works fine in local environment, but when i publish my website i get "Object expected" error on page load, and thus the js functions are not working in published website.

Currently i am writing <script src="/javascripts/jquery.hotkeys-0.7.9.js" type="text/javascript"></script> in master page.

View 5 Replies

Jquery - Reference Form By Name In Javascript Using An BeginForm Html Helper

Apr 5, 2011

I'm sure I'm going about this wrong but I'm trying to write a simple javascript method that will set a hidden type value upon a link click. I'm using the Html.BeginForm() helper that contains two links similar to:

@Html.ActionLink("Delete Review Only", "Delete", new { id = Model.ReviewId }, new { onclick = "SetDeleteType(1);" })

<script language="JavaScript" type="text/javascript">
function SetDeleteType(selectedtype) {
document.supportform.deleteType.value = selectedtype;
document.supportform.submit();
}

The supportform name obviously doesn't exists since I'm using BeginForm() and can't specify a form name. Is there a clever way of doing this without calling Forms(0) using jQuery or something or am I completely off?

View 1 Replies

AJAX :: Reference To A JavaScript File To Use In HTML Editor Content?

Jan 6, 2010

I wish to execute a javascript function in HTML Editor content. Hower, I give me the error "object expected". It seems like the browser cannot locate my javascript file.

Here is my setup:

- I have an external javascript file, "myscript.js" under "script" folder which has "test" function which contains a single "alert()" command.

- I referenced to "myscript.js" via <script src=....> tag (and also tried using Script manager's ScriptReference Path, same result).

- In my default.aspx page, I have a button which call "test" on mouseover event, which works call "test" correctly whenever I mouseover.

- In my default.aspx, I have the HTML Editor, with the this HTML content:

<span id="0001" onmouseover="alert('ok');">OK</span>
<span id="0002" onmouseover="test('not ok');">Not OK</span>

When I mouseover "ok" alert() script executed, but it give "object expected" error on I mouse over "not ok", it look like the browser cannot find "test" in myscript.js.

How do I reference this a script file inside the HTML Editor content?

View 6 Replies

Dynamic Javascript Reference Gives The Message Could Not Open In Design View?

Feb 21, 2011

I've inherited some code (not mine- I swear!) which uses a session variable in the header of the HTML to determine which javascript file to link to.i.e.

<SCRIPT language="javascript" src="../JavaScript/<%=Session("jsFileName")%>.js"></SCRIPT>

It does work, except that it won't let me change to design view. It gives the message

"Could not open in design view. Quote Values differently inside a '<%... "value" ...%>' block."

Anyone got any suggestions as to a workaround, that doesn't involve a huge rewrite.

View 3 Replies

C# - Javascript Function Don't Run At Once?

Feb 13, 2011

I've some function javascript to check loaded file with FileUpload Control

<script language="javascript" type="text/javascript">
function CheckFileBeforeUpdate()
{
var filePath = document.getElementById('<%= this.upFile.ClientID %>').value;
var validExtension = 'xml';
var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
if (ext == validExtension) return true;
alert('The file extension ' + ext.toUpperCase() + ' is not allowed!');
return false;
}
</script>

and it's called with Button: OnClientClick="return CheckFileBeforeUpdate();

<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In"
onclick="LoginButton_Click" OnClientClick="return CheckFileBeforeUpdate();" />

So there's unusual situation: I choose appropiate file, click Login...and it goes on to next functions despite inadequate extension.

Otherwise if I e.g. click FileUpload, but select no file.
Next time I choose some file (even with bad extension) and then function run (shows alert).

Why there's sth like blockade? What can I do to change CheckFileBeforeUpdate() runs everytime?

EDIT
if I select no file there's show alert("Select file to log in"); of course. And then is lock release

[Code]....

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved