Archive for the ‘ASP.NET’ Category

This content is password protected. To view it please enter your password below:

Here I am providing the code snippet that will allow us to delete the file once the file is downloaded on the client’s machine. Below are the code snippets


private void DownloadFile()
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition","attachment; filename=myFile.txt");


Private Sub DownloadFile()
Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", "attachment; filename=myFile.txt")
End Sub

I have searched many sites for to disable anchor and I know that IE supports ‘disable’ property for anchor tag, however Firefox does not support this. So whenever you try with ‘disable’ property of an anchor tag “<a></a>” you will get proper output in IE browser but not in Firefox browsers.

I have found a very good article which disables the anchor tag using few tricks because without tricks it is not possible to mange disable anchor in Firefox. You can find this article here. However, you can copy the same javascript script of above article as below.
function disableAnchor(obj, disable){
var href = obj.getAttribute("href");
if(href && href != "" && href != null){
obj.setAttribute('href_bak', href);
obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);"blue";

Also note that you can perform the same thing on the code behind (for if you are using a server control for anchor tag and you want to disable the anchor, you just need to remove the ‘href’ property using  MyAnchor.Attributes.Remove("href") and then just apply gray color to anchor and its done. It will work in both IE and Firefox (javascript and server code both).

Happy Programming!!

Today I am writing about how you can auto-refresh the page and still how you can manage the view-state of the page.

There may be requirement to fill up a long lasting form and this might take you a longer time to think or type. Now the general problem that occurs while filling up this kind of form is when you submit the form the session got expired or the timeout event occurs. So to overcome this situation we can set some timer to that particular page for refresh so that after several minutes it got refreshed automatically and we did not face any timeout or session related issues.

Now there are several ways to auto-refresh the page, the one is to put meta tag like <meta http-equiv="refresh" content="600">. What this tag will do is it will refresh the page after every 10 minutes, but when the page gets refresh then it will not persist the view-state of the page. This means if you are filling up any form and the page gets refreshed then you will not able to get all the filled content and the page will get loaded in its original form.

There was one requirement where I need to auto refresh the page as well as need to keep the view-state of the page. To achieve this I have put a simple JavaScript code on BODY tag of my page as below:

<Body onload="setTimeout('Form1.submit();', 600000);">
<form id="Form1" method="post" runat="server">

Here this JavaScript method “setTimeout()” will submit your form data after every 10 minutes and the page will also get refreshed so that it will also persist the view-state of the page after auto refresh of the page. Please note that the time 600000 here is in milliseconds. (1 second = 1000 milliseconds)

Happy Programming!

Few months back I have posted one article on “Javascript: Maximum character validation and character counter for a textbox”, now here I have given one example on how to count words out of any textarea or textbox.

The counter of word will get incremented as you type in each word (one spell) into the textbox/textarea. This script will also work when anyone copy/paste the text into the textbox (copy/paste should not through mouse). Check the following script:

<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" TextMode="multiLine" MaxLength="100"
runat="server" onkeyup="javascript:WordCounter(,'Label1');"
<asp:Label ID="Label1" runat="server" Text="100 chars
<script language="javascript" type="text/javascript">
function WordCounter(textId,lblID)
var this_field = document.getElementById(textId);
var label = document.getElementById(lblID);
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2)
word_count = 0;
if (word_count == 1)
wordOrWords = " word";
wordOrWords = " words";
if (char_count == 1)
charOrChars = " character";
charOrChars = " characters";
label.innerHTML = word_count + wordOrWords;

Happy Coding!!

So….. I have used the .First() method in many of my LINQ queries to make sure that my query only returns one record.  I thought this would be a great way to insure isolation of one record.  Very similar to a TOP 1 statement in SQL.  However, there is an issue with this concept.  If your TOP 1 SQL statement doesn’t find any records it returns nothing.  I guess I sort of hoped that the .First() method would do something similar – say return NULL or something like that.  It doesn’t!  Instead it throws an error stating “InvalidOperationException : sequence contains no elements”.  Not what I had hoped for.

No worries though!  There apparently was a reason for this method to find 1 or flip out as the LINQ team also provides the FirstOrDefault() method which will return NULL if no records were found.  Their documentation on the 101 LINQ Samples page doesn’t state anything about throwing an error.  It doesn’t really state anything at all about what would happen if the First() item wasn’t found.  It does state however that the FirstOrDefault() method will return the default value for the requested type – IE NULL!

You can face above errors on using .First(), .Last(), or .Single() method to make sure the query returns only single record. You can use .FirstOrDefault(), .LastOrDefault(), or .SingleOrDefault() instead.

Happy Programming!!

To retrieve the values from a DataKeyNames collection, you can use the following things. You can use the following code to get the DataKeys.

foreach (GridViewRow gvr in gvMyGridView.Rows)
string PrimaryKey = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();

You can use this code while doing an iteration with “foreach” or for any GridView event like OnRowDataBound.

Here, You can input multiple values for DataKeyNames by separating with comma (‘,’). For example, DataKeyNames="ProductID,ItemID,OrderID"

You can now access each of DataKeys by providing its index like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values[1].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values[2].ToString();

You can also use Key Name instead of its index to get the values from DataKeyNames collection like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ProductID"].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ItemID"].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values["OrderID"].ToString();

You can also check a similar kind of article here

Happy programing!!