Run JavaScript After DataBind - javascript

I have a ASP.Net Repeater control with a Table inside it. Is it possible to run a JavaScript function directly AFTER I call MyRepeater.DataBind()? I've been trying different things, but nothing is triggering the JavaScript function.
Thanks

Databinding occurs on the server in a postback as part of the Page Lifecycle process. In other words, excluding partial-postbadks, at the time this happens any existing DOM in the browser is destroyed. A whole new page is constructed on the server and transmitted to the browser, so that a new DOM can be built and rendered.
What all that means is that you want to think in terms of running your javascript in the page's onload event. One way to make this happen is using the ClientScriptManager.

Javascript can be called from server side by using RegisterStartupScript and RegisterClientScriptBlock methods.
http://www.mindfiresolutions.com/Register-clientside-startup-script-from-serverside-code-286.php

No. The javascript isn't even going to render and run until the code-behind has executed and the page delivered to the client. So it won't matter if adding the script is the first thing you do in the code-behind or the last thing you do (or directly after the DataBind()).
When using the ClientScriptManager Class, look at your code behind and you'll see the dynamic javascript is added just before the ending </form> tag (although it still may be possible to accomplish what you want to do, just with a different approach).

Well I found a solution, not sure it's the cleanest way to do it, but for my application's context it works:
I ran the javascript code after a partial postback using: Sys.WebForms.PageRequestManager.getInstance().add_endRequest();
Again, not the cleanest but suits the needs I have.
Thanks for all your input

Related

Why am I able to use django template variables dynamically?

My understanding of drango templates is that everything happens on server side and then it generates an html out of the templates. After, generation, it is just plain text in the html. But for some reason, I am able to use django variables dynamically in javascript.
Here is a javascript example:
$("#smth").append("<li>{{djangoObject.0.id}}</li>");
Even if I put this in an event handler, meaning, it for sure will be called after the html generation, it works just fine.
How and most importantly, WHY does django keep the variable in the client-side?
It's not being used client side, it's just being rendered directly into the Javascript string. So if djangoObject[0].id were 12, for example, the resulting code would look like
$("#smth").append("<li>12</li>");
Which, obviously, would run just fine. It's not dynamic though, and be sure to keep that in mind - it doesn't fetch id at the point of the event happening. It fetches it at template rendering, which happens before the HTML (and embedded Javascript) is sent to the client (your browser).
This code rendered to something like $("#smth").append("<li>123</li>");. Of course this work without any problem after the html generation.

Registering script for event validation from within the JS file

Just a quick question here. I am trying to register a js file for script validation using
if (!Page.ClientScript.IsClientScriptBlockRegistered("strIncludeJSFile")) Page.ClientScript.RegisterClientScriptBlock(strIncludeJSFile.GetType(), "strIncludeJSFile", strIncludeJSFile);
code in C# and it works well for the js files. But, some js files are used in multiple pages, so I am unsure if the above code will be a good idea. As such, I want to do the same thing in the js file itself, instead of using the code behind. Is there any possibility to do that? Or is this thing specific to C#?
of course you can register a js file (or script block) from html by using a script tag. However, the main reason RegisterClientScriptBlock exists is because developers might need to generate (or modify) a script block dynamically from code behind or conditionally register a script file dynamically.
If you need any of the above...generate a script block dynamically, then you "might" be better off registering it from code behind, I mean, it depends on the whole solution itself, it's hard to recommend an approach without having some context. Either way, some options are:
register the script blocks from code behind if you need to generate based on some conditions that can only be evaluated from server side code
use a master page for better and register the script block from the master page. This will make it easier to maintain if you keep the logic in one place
similar to the option above, you can use a base page class if using a master page is not possible
use an html script element if what you need is to reference a static js file
use place holders such as js variables and fetch dynamic data from the server using ajax
use unobtrusive javascript, custom data attributes and ajax
Whatever the suitable option(s) is depends on too many factor that you have to assess

Modify an UpdatePanel by calling server-side code from client code

It's possible that there's no way to do this, but I figure I would ask. I'm relatively new to asp.net, having played with it for about a week now. What I have right now is a page that calls a web service, polls it until it's done (with progress displayed in an UpdatePanel), then hides the progress text and instead displays the result (a recursive list of files with some metadata) by creating a TreeView and adding it to the UpdatePanel. What I would like is for clicking a node in the TreeView to update a second UpdatePanel with extended information (obtained server-side) about the node that was just clicked on. I don't see any way to call a codebehind function by clicking a TreeNode, but I can call Javascript code by setting the node's NavigateUrl to "javascript:function([the full path of the node])".
At this point, though, I'm kind of stumped. StackOverflow is full of correctly-answered questions about how to call back into the codebehind from javascript (using a WebMethod, or equivalent), but apparently you can't call code that isn't static, which would mean I couldn't modify the page itself, or for that matter, access the session or page state. StackOverflow is also full of questions about how to have javascript request that an UpdatePanel refresh itself (__doPostBack()), but without any way to communicate to the server what was clicked on, the UpdatePanel wouldn't know what to display.
Thus, the question, which I'm hoping has an answer: am I missing some clever way to have javascript on the page trigger a server-side function capable of taking a parameter and using it to do a partial postback of a different UpdatePanel?
Thanks!
Actually it's quite straightforward. Just place a LinkButton inside of second update panel (it can have an empty text and thus be invisible), in JavaScript call client-side .click() method of that control, and in ASP.NET handle server-side OnClick event.

Problem loading remote script with jQuery multiple times in Firefox

I have a script element in my webpage, something like this:
<script id="myscript"></script>
Now, from a javascript file, I'm doing something like the following:
$('#myscript').src('http://foo.bar?callback=somefunc')
Now this remote script 'returns javascript' of the following form:
somefunc(somearg);
When I run all of this, things work neatly, the script gets loaded dynamically, and the 'somefunc' callback is executed.
The problem happens when I do the same thing again. Let's say I again call the same thing:
$('#myscript').src('http://foo.bar?callback=somefunc')
This, for some reason, DOESNT return the javascript call in Firefox only. (Works fine in IE - somefunc gets executed again as expected).
I can think of ugly workarounds (such as doing a $('head').append('<script...')) every time - but I'd like to know what's going on here.
Thanks in advance!
I would recommend you to use $.getScript instead of using a single script tag load scripts multiple times:
$.getScript("http://foo.bar?callback=somefunc");
That function will abstract the script element creation and its introduction to the DOM.
But it seems you are accessing a JSONP service, in that case you need only $.getJSON:
$.getJSON("http://foo.bar?callback=?", function(json){
// callback
});
I can think of ugly workarounds (such as doing a $('head').append('
Ugliness is subjective; personally, I find the technique you're trying to use (making a single script tag load multiple scripts) far uglier.
But that's not really important. Adding a new script tag works - so if you're having trouble with what you're doing, just use the normal method and live with it.
FWIW: Firefox probably doesn't respond because you're not actually changing anything... If you want to make this even uglier, append some do-nothing querystring parameter that changes each time through.

Possible to append a ActiveX control to a page using javascript?

I'm trying to append an ActiveX control dynamically to a page using jQuery. The append is successful; however, the control doesn't initialize when it is done this way. I believe IE calls the OnCreate method of an ActiveX control when a page that contains a control has finished rendering. The problem is that the tag is not present on the page until after rendering is finished, so OnCreate is never called.
I'm not sure if that's the problem, it's just a guess. Does anyone have experience with this? Is it possible to force IE to call OnCreate at a specific time?
The control works fine if the tag is in the html. The only time I see problems is when I add the object to the page via javascript.
Update: I need to know what IE does when it encounters an
<object>
tag on the page at render time. The control works fine in that context, so IE is calling something at that time. I need to invoke that manually after I've added the control to the page post render.
Thanks, Pete
You can instantiate the control in a totally cross-platform-unfriendly manner using new ActiveXObject(ProgID). ProgID is a string of the form "appName.typeName". e.g.,
var excel;
excel = new ActiveXObject("Excel.Application");
...
The example will only work if excel is installed on your machine.
I had a similar problem to yours today with a java applet under IE. My workaround (i wanted to put the applet after page has finished rendering) was to dynamically create invisible iframe with src pointing to simple html page with my applet. After loading iframe i called it's parent to notify that the applet was loaded.

Categories