How to restrict download of specified file types in asp.net

In this post i will show you how to restrict your web app so that .txt (whatever file extensions you want ) files can not be downloaded. Open your web.config file and register following setting in your web.config
<system.web>
   <httpHandlers>
       <add verb="*" path="*.txt" type="System.Web.HttpForbiddenHandler" />
   </httpHandlers>
</system.web>
For more details how to register handler in web.config check out this link

How to animate grdivew using Jquery

Photobucket
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#<%=ddlCategory.ClientID %>').change(function() {
                var categoryid = $('#<%=ddlCategory.ClientID %>').val();
                $.ajax(
            {
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: "{'CategoryID':'" + categoryid + "'}",
                url: "Default.aspx/ProductByID",
                success: function(ret) {
                    $("#grd").html(ret.d).slideUp("fast").slideDown("slow");
                }

            }
        );
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlCategory" runat="server">
        </asp:DropDownList>
    </div>
    <div id="grd">
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Web.Script.Serialization;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindCategory();
    }
    private static string ConvertControlToHTML(Control source)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        source.RenderControl(htw);
        return sw.ToString();
    }
    private void BindCategory()
    {

        ddlCategory.DataSource = new Category().GetCategoryList();
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataValueField = "CategoryID";
        ddlCategory.DataBind();
    }
    [System.Web.Services.WebMethod]
    public static string ProductByID(int CategoryID)
    {

        var query = from p in new Product().GetProductList()
                    join c in new Category().GetCategoryList()
                    on p.CategoryID equals c.CategoryID
                    where c.CategoryID == CategoryID
                    select new
                    {
                        CategoryID = p.CategoryID,
                        ProductName = p.ProductName,
                        Price = p.Price

                    };
        GridView gv = new GridView();
        gv.ShowHeader = true;
        gv.AutoGenerateColumns = true;
        gv.DataSource = query;
        gv.DataBind();
        return ConvertControlToHTML(gv);

    }

}
public class Product
{
    public int ID { get; set; }
    public int CategoryID { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }

    public List<Product> GetProductList()
    {
        List<Product> list = new List<Product>();
        for (int i = 1; i < 5; i++)
        {

            list.Add(new Product() { CategoryID = i, ProductName = "Product" + i.ToString(), Price = i * 4.5 });

        }
        for (int i = 1; i < 10; i++)
        {

            list.Add(new Product() { CategoryID = i + i, ProductName = "Product" + i.ToString(), Price = i * 4.5 });

        }
        return list;

    }
}

public class Category
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public List<Category> GetCategoryList()
    {
        List<Category> list = new List<Category>();
        for (int i = 1; i < 10; i++)
        {

            list.Add(new Category() { CategoryID = i, CategoryName = "Category" + i.ToString() });

        }
        return list;
    }

}

GridView row double click and postback using Jquery

Photobucket
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <style type="text/css">
        .selected
        {
            background-color: Red;
        }
    </style>

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <title></title>

    <script type="text/javascript">
    var selected = null;

$(document).ready(function(){
   $("#<%=GridView1.ClientID %>").find("tr").click(function(){
      $(selected).removeClass("selected");
      $(this).addClass("selected");
      selected = this;
   });
   $("#<%=GridView1.ClientID %>").find("tr").dblclick(function(){

      /* if you just want to dig into that record I would put a custom attribute on the row */
      window.location = "<%=ResolveUrl("~/Default2.aspx")%>?record=" + $(this).find("td:last").text();
      /* or you could have a hidden LinkButton in the row (Text="" or not set) that you could trigger. Make sure you set the CommandName="Something" and CommandArgument="RecordId" */
      $(this).find("a").click();
   });

});


    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Create Object of person class
        Person personObject = new Person();
        //Assign Person list to GridView
        GridView1.DataSource = personObject.GetPersonList();
        //Call Bindmethod of GridView
        GridView1.DataBind();

    }
}
public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> GetPersonList()
    {
        //Retrun List of Person
        List<Person> list = new List<Person>()
        {
            new Person{ID=1,Name="Person1",Age=32},
            new Person{ID=2,Name="Person2",Age=45},
            new Person{ID=3,Name="Person3",Age=43},
            new Person{ID=4,Name="Person4",Age=21},
            new Person{ID=5,Name="Person5",Age=76},
            new Person{ID=6,Name="Person6",Age=54},

        };

        return list;

    }
}

How to get cell value of gridview using JQUERY

Photobucket
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            var list = "";
            $("#btnGet").click(function() {
                $("#<%=GridView1.ClientID %> tr").each(function() {
                    //Skip first(header) row
                    if (!this.rowIndex) return;
                    var age = $(this).find("td:last").html();
                    list += age + "</br>";

                });
                $("#listAge").html(list)
            });

        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <input type="button" id="btnGet" value="Get Cell Value" />
    <div id="listAge">
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //Create Object of person class
        Person personObject = new Person();
        //Assign Person list to GridView
        GridView1.DataSource = personObject.GetPersonList();
        //Call Bindmethod of GridView
        GridView1.DataBind();

    }
}
public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> GetPersonList()
    {
        //Retrun List of Person
        List<Person> list = new List<Person>()
        {
            new Person{ID=1,Name="Person1",Age=32},
            new Person{ID=2,Name="Person2",Age=45},
            new Person{ID=3,Name="Person3",Age=43},
            new Person{ID=4,Name="Person4",Age=21},
            new Person{ID=5,Name="Person5",Age=76},
            new Person{ID=6,Name="Person6",Age=54},

        };

        return list;

    }

}

How To change the color of gridview row on mouseover using JQUERY

Photobucket
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="_Default" %>

<!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 id="Head1" runat="server">
    <title></title>

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#<%=GridView1.ClientID%> tr").filter(function() {
                return $('td', this).length
                && !('table', this).length
            }).hover
                (
                function() { $(this).css("background-color", "red") },
                function() { $(this).css("background-color", "white") }
                );
        });

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {  //Create Object of person class
        Person personObject = new Person();
        //Assign Person list to GridView
        GridView1.DataSource = personObject.GetPersonList();
        //Call Bindmethod of GridView
        GridView1.DataBind();

    }
}
public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> GetPersonList()
    {
        //Retrun List of Person
        List<Person> list = new List<Person>()
        {
            new Person{ID=1,Name="Person1",Age=32},
            new Person{ID=2,Name="Person2",Age=45},
            new Person{ID=3,Name="Person3",Age=43},
            new Person{ID=4,Name="Person4",Age=21},
            new Person{ID=5,Name="Person5",Age=76},
            new Person{ID=6,Name="Person6",Age=54},

        };

        return list;

    }

}