registerClientScriptBlock in custom handler? - asp.net

I'm using a custom control in ASP.NET that handles file uploading for me. The control has a handler in the code-behind using something like this:
Protected Sub UploadFileComplete(ByVal sender As Object, ByVal e As UploadControl.FileUploadCompleteEventArgs) Handles UploadControl.FileUploadComplete
Within that sub, I post back to the server and do some work on the database, but then when I come back, I want JavaScript to register at that point.
However, when I use Page.ClientScript.RegisterClientScriptBlock or ScriptManager.RegisterClientScriptBlock, the scripts don't load on the page. I need this JavaScript to run and update the page, and to close the upload dialog window. I assume it's because the page is already loaded.
Does anyone have any good ideas on how to do this?

Alright so turns out it's like this...
On the server side (code-behind), in the UploadFileComplete() sub, you can access that EventArgs variable by using the method e.CallbackData = [WHATEVER].
And then in your javascript, you use this built in client function:
function UploadComplete(args){
alert(args.callbackData);
}
And that args.callbackData variable has whatever you put in via server side. Pretty slick I think! But hard to figure out cause they didn't have it documented very well.
This way, you don't need to add your own RegisterClientScriptBlock method, because you can pass in anything you want to JavaScript using their built in method.

Related

How to wait three seconds then turn to another webpage

I'm new to web development, and I'm currently using ASP.net. I wonder what would I need to do to let the browser wait for 3 seconds so my users can read the text "Customer Successfully Added" before turning to another page? I have attached my code as follows.
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim db As New DatabaseClass
db.addProfile(txtLN.Text, txtFN.Text, txtUsername.Text, txtPassword.Text, txtAddress.Text, txtZip.Text, txtPhone.Text, txtEmail.Text)
lblMessage.Text = "Customer Successfully Added"
End Sub
In addition, I'm not sure how to utilize MSDN. For me, its information overload, I'm wondering how to go about finding the solution on MSDN so i would be able to solve my problems in the future. Thank you!
You can't do it in the code behind of the page because of how asp.net works - the label text would not update until after the timeout occurred if you did it in the code-behind.
The server-side processing spits all the html back to the browser only after it has completely processed any server-side code unless you're using Ajax. Since you're new, I won't even bother going into how to do it with Ajax as there is a MUCH simpler option for accomplishing what you want.
A simple method to accomplish what you're looking for would be to have a simple HTML page that just has a message that says "Customer successfully added" and use javascript (client-side code) to pause and then redirect using the Javascript "SetTimeout" function.
There's an example here: http://www.bloggingdeveloper.com/post/JavaScript-Url-Redirect-with-Delay.aspx
The logic flow wshould work like this:
The original page should add the record (in code-behind) then redirect to this simple html page (in code-behind). The html page should have the "Customer Added" message and use the SetTimeout and Redirect to go to whatever page you want the user to see after viewing the message.
For stuff like this you need the code to run client side rather than on the server. The easiest way to do this is to return some javascript with your page (in the .aspx part rather than the code behind)
Take a look here for an idea of what to do :)
The page is displayed for a few seconds and then the javascript triggers a redirect to a url of your choosing. Just add something like this into your html.
You can emit javascript to redirect to the other page, using the setTimeout function.
This is best accomplished using the ScriptManager to register any javascript on the page.

In ASP.Net, How do I run code after sending page

In ASP.Net, I want to run some code (logging, other cleanup) after the page has already been sent to the user. I don't want this code to interfere with the amount of time it takes the client to receive a response. I tried placing this code in the OnUnload part of the page, but through testing (using breakpoints, or busy waiting loops) the client does not actually display the page until the code in the OnUnload is finished executing. Even though the response object is no longer available at this point, and therefore I would assume that the buffered response has been sent to the client, the client still does not display the page until the OnUnload is finished executing. The only way that seems to work at the moment is to start a new thread to do the work, and allow the OnUnload to finish immediately. However, I don't know if this is safe or not. Will the server kill the thread if it executes too long after the page is already sent out? Is there a more correct way to accomplish this?
Kibbee,
Try overriding the Page.Render method and flushing the response like so:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
Response.Flush() ' Sends all buffered output to the client
' Run some code after user gets their content
End Sub
I think at this point the response still isn't complete, but the user will get what the page has rendered before you finish running that final code.
HTH,
Mike
Unload is the last part of the ASP.NET page life cycle. See link below:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Is this something you could do with javascript or AJAX?
How about hooking into the page.Disposed event instead of the Unload event?
Depending on what you need to do an ISAPI filter can be created to do stuff at the end of the response.
You could try this solution - Best ASP.NET Background Service Implementation
Using a windows service and communicating with it through MSMQ might be a far more reliable solution and can scale up better. This will help you separate the concerns and let asp.net front end just focus on the user while the windows service focusses on the background tasks.
If you are moving to the cloud with azure, you can simply replace the asp.net front end with a web role and the windows service with a worker role and ur solution can scale seamlessly!
Here's a redneck way to do it. Have a second aspx page that does all your "clean up" post-rendering logic. Call that one 'cleanup.aspx' for example. Have your clean up code run in Page_Load of cleanup.aspx:
public partial class cleanup: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// do logging blah blah here....
}
}
In your main aspx page, have a JavaScript function that makes an AJAX call to 'cleanup.aspx'. Make the AJAX function fire after page load. I recommend jquery, if so your code will look like this:
$(function(){
yourAJAXYFunctionName();
});
function yourAJAXYFunctionName()() {
// ajax code here
//
$.ajax({
url: "cleanup.aspx",
});
}
This way, your first page's code runs, then the page is sent to the browser. As the page renders to the client, the 2nd ASPX page is called via AJAX, which really doesn't matter to cleanup.aspx, it doesn't care how its called and its page load event is executed. Your cleanup/logging code is then ran.
Caveats: client side JavaScript is required. But really who the hell doesn't have JS running anyway? Also, your cleanup.aspx page is totally abstracted from your main page, so if you want to use any object in cleanup.aspx that originated in your first page, then you'll have to store them in session or cookies, or you can pass them as parameters in the AJAX call. That will require dynamic manipulation of the AJAX script itself, but that's not too hard.
Better i think try try{Response.End()}catch{} runcode();

What is the best way to include jQuery in DotNetNuke 4.8.x?

I'm using DotNetNuke 4.8.x and want to utilize jQuery. Could anyone possibly suggest me on what is the best way to integrate jQuery into DNN? I won't be able to upgrade DNN version to 5 which has built-in support for jQuery.
Your advice would be much appreciated.
To avoid loading jQuery multiple times, it might be best to use a client-side script, like the one given here.
In the server-side, you can load jQuery library in the page header during the Page.Init or Page.Load event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
Page.Header.Controls.Add(...)
End Sub
You may want to use a helper method to create the HtmlGenericControl, like
Public Function HeadScriptResource(ByVal src As String) As HtmlGenericControl
Dim Include As New HtmlGenericControl("script")
Include.Attributes.Add("type", "text/javascript")
Include.Attributes.Add("src", src)
Return Include
End Function
This way, you can add any script to header using the source path as parameter:
Page.Header.Controls.Add(HeadScriptResource("/resources/shared/scripts/jquery/jquery.min.js"))
DNN 4.9.1 and above are shipped with jQuery located in
/resources/shared/scripts/jquery/jquery.min.js
If every server you need has web access, which is not evident in corporate environments, you can use hosted jQuery, for example:
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
With hosted jQuery, you have to use the https url if the site uses https. Otherwise, you'll get browser warnings. It may be easier to always use https.
If you need to add jQuery on page-by-page basis, you can also use Page Header Tag in Page Settings as suggested here.
See also:
How to get
Page.ClientScript.RegisterClientScriptInclude
to include in the head?
jQuery
Tips for DotNetNuke Developers

Jquery user control

I have a problem with jquery not loaded in an asp user control.
I want simply to add the click event when a checkbox is clicked.
Here is my javascript file
$(document).ready(function() {
var arr = jQuery(":checkbox[id*='drpAccountType']");
for (i = 0; i < arr.length; i += 1) {
$("#" + arr[i].id).click(function() { alert(this.id) });
}
});
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
The user control pre render events:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
ScriptManager.RegisterClientScriptInclude(Me, Me.GetType, "CheckboxdropdownScript", ResolveUrl("~/Scripts/CheckBoxDropDown.js"))
End Sub
the script is loaded fine but any usage of jQuery return undefined. Then only when the page is loaded I can excute the same code for the commande line in firebug. The jquery library is loaded in the master page.
Whenever I'm using jquery within an asp user control I always find problems, and I always have to hack around to get it to work. I try all the entries in stackoverflow but I never found a one as general solution.
did any one found a simple solution to use jquery with master pages, user control in asp.net
I would appreciate if someone can share such valuable information.
My manager is about to drop jquery from the application as we always waist lot of time just to find a workaround to have it to work with user controls.
Please help, I like jquery and I really want to use it for client script.
best regards
your script may be getting inserted into the page before the jquery library. (You can verify this by checking the HTML output of your page.)
When script files execute JS inline, and especially when that JS depends on other libraries, it's safest to use RegisterStartupScript rather than RegisterClientScriptInclude. If you use this overload of RegisterStartupScript, you can construct a reference to the same external JS file and it'll run in the right place.
As an alternative, you could use <asp:ScriptReference/> tags in a ScriptManagerProxy in your user control rather than registering the script in Page_Load or Page_Init; in my experience, those scripts are added after scripts from the master page.
this is not exactly a direct answer but here are some jQuery ASP.net controls that might help:
http://clipperhouse.com/jQuery/
(Look for the bit about Callbacks.)
Also, you might not need the "for" loop if you simply give all your checkboxes a class name and use that as the selector.

How do you call a Javascript function from an ASPX control event?

How do you call a Javascript function from an ASPX control event?
Specifically, I want to call the function from the SelectedIndexChanged event of a DropDownList.
I get a little nervous whenever I see this kind of question, because nine times out of ten it means the asker doesn't really understand what's going on.
When your SelectedIndexChanged event fires on the server, it fires as part of a full postback. That means that for that code to run, the entire rest of your page's load code also had to run.
More than that, the code runs as the result of a new http request from the browser. As far as the browser is concerned, an entirely new page is coming back in the result. The old page, and the old DOM, are discarded. So at the time your SelectedIndexChanged event code is running, the javascript function you want to call doesn't even exist in the browser.
So what to do instead? You have a few options:
Change the page so the control doesn't post back to the server at all. Detect the change entirely in javascript at the client. This is my preferred option because it avoids odd onload scripts in the browser page and it saves work for your server. The down side is that it makes your page dependent on javascript, but that's not really a big deal because if javascript is disabled this was doomed from the beginning.
Set your desired javascript to run onload in the SelectedIndexChanged event using the ClientScript.SetStartupScript().
Apply the expected results of your javascript to the server-model of the page. This has the advantage of working even when javascript is turned off (accessibility), but at the cost of doing much more work on the server, spending more time reasoning about the logical state of your page, and possibly needing to duplicate client-side and server-side logic. Also, this event depends on javascript anyway: if javascript is disabled it won't fire.
Some combination of the first and third options are also possible, such that it uses javascript locally if available, but posts back to the server if not. Personally I'd like to see better, more intuitive, support for that built into ASP.Net. But again: I'm talking about the general case. This specific event requires javascript to work at all.
As Muerte said you have to just put the javascript, or a call to it on the page from the code behind. Personally I use this:
ClientScript.RegisterClientScriptBlock("customscript", "<script>simple script here</script>")
Of you can call the function if you already have a more complex one on the page instead of the stuff I have.
You can't do it directly from an event, because ASPX control event is server side.
What you can do is emit a Javascript in the ASPX event which will call the JavaScript function when the page reloads.
For example, if in your ASPX page you have a Javascript function called "DoSomething()", in you ASPX control event, add the following:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myEvent", "DoSomething()", true);
}
The last boolean parameter defines that tags are added automatically.
In the code behind, attach some markup to the server side control via its attributes collection. This assumes that the function is already in a client script file that is already available to the page.
MyServerDDLControl.Attributes.Add("SelectedIndexChanged", "MyClientSideFunction();");

Resources