Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Friday, April 9, 2010

Force the users to download rather than opening

For known content types like PDF, doc, mp3 etc..... when you have a download link it just opens it in the browser if the browser knows how to open..

If you want to force the user to download rather than being opened / played in web browser do the following with asp.net

Create a link using a link button and on button click do the following

string myFilename="../docs/abc.doc";

Response.AppendHeader( "content-disposition", "attachment; filename=" + myFilename );

The 2 lines above would force the browser to show the download box .

Hope this Helps.

Bobby

Monday, February 22, 2010

Inject javascript in Head Section using asp.net

Even though it doesn't matter where the script is in the form or in the head section, you want to have it in the HEAD section of the HTML.
When you use Page.RegisterClientScriptBlock it injects the script in the body.
To inject in the HEAD section do the following...
HtmlGenericControl myScriptCode = new HtmlGenericControl("script");
myScriptCode.Attributes.Add("type", "text/javascript");
myScriptCode.InnerHtml = "alert('Alert generated by Script in Head Section of HTML');";
this.Page.Header.Controls.Add(myScriptCode); 
that should do it.check it by clicking view source and you should see the Script in HEAD section If you want to refer to a js file as a external file use the SRC attribute
string jssrc="../abc.js";
HtmlGenericControl myScriptCode = new HtmlGenericControl("script");
myScriptCode.Attributes.Add("type", "text/javascript");
myScriptCode.Attributes.Add("src",jssrc);
this.Page.Header.Controls.Add(myScriptCode);