Wednesday, March 28, 2012

How to force a pdf file to download ASP.NET

If you try to open pdf files on a browser, those are automatically open but if you need to force the browser to directly download the pdf instead you have to add file header and give the full path. see the example:


<span style="color:red">Click <a href="download.aspx"
target="_blank"><u> here </u></a> to download resume.</span>



then on the page load method of the download.aspx page, write this:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=documents/resume.pdf")
Response.ContentType = "application/pdf"      Response.WriteFile(Server.MapPath("~/documents/resume.pdf"))
Response.[End]()
End Sub


Happy Coding!!

Wednesday, February 22, 2012

Read & Display SQL data into Label

Code:

Dim conn As New SqlConnection
(ConfigurationManager.ConnectionStrings("SqlServerExecSP").ToString())
conn.Open()
Dim sql As String = "Select Name from People where Id = 1"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
Dim rd As SqlDataReader = cmd.ExecuteReader()

While rd.Read()
label1.Text = rd("Name")
End While


Change the connection string and table name in the above code.

Happy Programming!!

Checkbox-- Select at least one box using jQuery

Code:
<script>
$(document).ready(function () {
$('.error').hide();
$('.submit').click(function (event) {
var count = $('input:checked').length;
if (count == 0) {
alert("select at least one");
return false;
}
else

{
return true;
}
});
});
</script>

<input id="app" class="box" value="5" type="checkbox" name="apple">Apple$5

<input id="ora" class="box" value="2" type="checkbox" name="orange">Orange $2

<input id="mel" class="box" value="1" type="checkbox" name="melon">Melon$1

<input id="gra" class="box" value="3" type="checkbox" name="grape">Grapes $3

<input class="submit" value="Submit" type="submit">


more:
http://code.google.com/p/m-jq-projects/wiki/enableControlOnCheck_jQuery

Tuesday, May 24, 2011

Redirects handled by Google Analytics

Google Analytics won't track a visit to a page unless that page runs the analytics tracking code. Although, there are at least 2 ways to track the redirects:
-Tracking a redirect using 301 redirect.
-Tracking a redirect using a JavaScript redirect.

In both instances, make sure the directory actually exists as a file (/it/growthmodel.aspx). I always use the JavaScript redirect so, let's talk about it:
Let say, you have a pdf file (growthmodel.pdf), you want to post it online and also want to count the number of visitors and their behaviours. For this, create a page growthmodel.aspx under the /it directory and redirect this page to pdf file using javascript redirect. Put the below code at growthmodel.aspx page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
>

var _gaq = _gaq
[];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

</head>
<body onLoad="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label'])">
<script type="text/javascript"
>

function redirect()
{
window.location =
"/growthmodel.pdf"
}

var temp = setInterval("redirect()", 1000);

</script>
</body>
</html>

setInterval=> 1000 so that tracking doesn't get missed.
'UA-XXXXXX-X' => please replace xxxx with your actual analytics account number.

Happy Programming!!