• New Test Post

    by Venkata Koppaka | Dec 31, 2011

    This is a new test post for Live writer detection

    Go comment!
  • Disable AutoComplete on Textbox in ASP.NET

    by Venkata Koppaka | Sep 25, 2011
    There are cases when you want to turn off AutoComplete that all modern browsers offer for textbox.
    This article will guide you through on how to disable AutoComplete in ASP.NET.

    There are two ways you can achieve this -
    1. Using AutoCompleteType (works in IE and .NET specific). You can read more about AutoCompleteType here
    2. Using AutoComplete (works in all browsers, HTML attribute).
    AutoCompleteType
    AutoCompleteType can be used for achieve different behavior on the textbox, using AutoCompleteType="Disabled" turns off AutoComplete when using IE.

    <asp:TextBox ID="tbAutoCompleteIE" runat="server" AutoCompleteType="Disabled"></asp:TextBox>


    AutoComplete
    AutoComplete is a HTML attribute that can be used for any language / platform and it works in all the browsers. AutoComplete="Off" on the textbox (input) turns off AutoComplete on all browsers.

    <asp:TextBox ID="tbAutoCompleteAll" runat="server" autocomplete="Off"></asp:TextBox>

    You can download code for this post here.

    Hope this helps.

    Cheers
    Venkata
    Go comment!
  • Preventing Caching in JQuery Ajax requests

    by Venkata Koppaka | Sep 23, 2010
    To prevent Caching of results while making JQuery Ajax requests use the following code -
    1         <script type="text/javascript"> 
    2             $(function() { 
    3                 $.ajaxSetup({ cache: false }); // prevent caching of get requests (needed for IE). 
    4             }); 
    5         </script> 

    If you use the above script, whenever you make a JQuery ajax request it will auto-add a unique URL parameter to the requestedy url and hence the cached results wont be returned.

    Here is how a JQuery GET request would look like without cache : false


    Here is how JQuery GET request would look like with cache : false.

     

    Hope this helps,

    Cheers,

    Venkata

    Go comment!
  • Grey out Submit button after click - ASP.NET

    by Venkata Koppaka | Jul 02, 2010

    This post is small snippet I had to come up with to grey out the submit button after clicking it so the user doesn't click it twice.

    I am using a post in asp.net forums http://forums.asp.net/p/1057086/1868122.aspx as reference.

    The version I am writing here is tailored down version of what the forums website discusses.

    Here is the C# Utility method that generates a script.

    1         ///  <summary> 
    2         /// Generates auto-disable butotn 
    3         /// </summary> 
    4         /// <param name="p">Page Instance</param> 
    5         /// <param name="btn">Button Instance</param> 
    6         /// <param name="disabledText">Text to display on button when clicked</param> 
    7         /// <returns></returns> 
    8         public static string GetLockButtonJscript(Page p, Button btn, string disabledText) 
    9         { 
    10             disabledText = disabledText == "" ? "Processing..." : disabledText; disabledText = disabledText.Replace("'", ""); 
    11  
    12             StringBuilder sb = new StringBuilder(); 
    13             if (btn.CausesValidation && p.Validators.Count > 0 && btn != null) 
    14             { 
    15                 sb.Append("if (typeof(Page_ClientValidate) == 'function') { "); sb.Append("if (Page_ClientValidate('" + btn.ValidationGroup + "') == false) { return false; }} "); 
    16  
    17             } 
    18              
    19             PostBackOptions opt = new PostBackOptions(btn, "", "", false, true, true, true, true, btn.ValidationGroup); 
    20  
    21             sb.Append(p.ClientScript.GetPostBackEventReference(opt)); 
    22             sb.Append(";"); 
    23  
    24             sb.Append("this.disabled='true';this.value='" + disabledText + "';"); 
    25              
    26             return sb.ToString(); 
    27         } 

    To use this method in a .aspx file and disable a button just include this line of code in the Page_Load event -

    1        protected void Page_Load(object sender, EventArgs e) 
    2         { 
    3             btnSubmit.Attributes.Add("onclick", GetLockButtonJscript(this.Page, btnSubmit, "Processing...")); 
    4         } 

    Hope this Helps,

     

    Cheers,

    Venkata


    Go comment!
  • Sorting Tabular Data in ASP.NET

    by Venkata Koppaka | Jun 05, 2010

    In this post I will talk about how to build tabular data using ADO.NET EntityFramework and sort tabular / grid data using JQuery table sorter plugin. For the sake of demo I will use ASP.NET 4 and .NET Framework 4.

    Fire up Visual Studio 2010 and Create a New ASP.NET WebSite. For this demo I will use ADO.NET Entity Framework to get a list of Products from the database.

    I will use AdventureWorks Database for the sake of this demo.

    Add a new ADO.NET Entity Data Model, select “generate from database”, hit next choose the and add the product’s table

     

    Add a new C# Class and class it Product.cs this class will be in the same namespace as the EntityDataModel and the Product class will extend as a partial class from the class created by ADO.NET EntityFramework. Here is how the class should look like –

    1 using System; 
    2 using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Web; 
    5  
    6 /// <summary> 
    7 /// Summary description for Product 
    8 /// </summary> 
    9 ///  
    10 namespace AdventureWorksModel 
    11
    12     public partial class Product 
    13     { 
    14         public IQueryable<Product> GetProducts() 
    15         { 
    16             AdventureWorksEntities db = new AdventureWorksEntities(); 
    17             return db.Products.Take(200); 
    18         } 
    19     } 
    20

    We created a method called GetProducts which just takes the top 200 rows in from the Products table. This method will return a List of Products of type IQueryable.

    Now add a new ASP.NET WebForm and call it Products.aspx. In Products.aspx drag drop a GridView and set its DataSource as ObjectDataSource, in the GridView tasks choose new DataSource and Choose object as Data Source Type,

     

    From the choose a Business Object Screen Choose the Products Class we just created.

     

    For the select method Choose the GetProducts method we created earlier.

     

     

    Choose the necessary columns from the gridview, I choose a few columns for this demo. Once you are done with choosing columns your Products.aspx code should look something like this –

      

    1     <asp:GridView ID="gdvProducts" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> 
    2         <Columns> 
    3             <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" /> 
    4             <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
    5             <asp:BoundField DataField="ProductNumber" HeaderText="ProductNumber" SortExpression="ProductNumber" /> 
    6             <asp:CheckBoxField DataField="MakeFlag" HeaderText="MakeFlag" SortExpression="MakeFlag" /> 
    7             <asp:CheckBoxField DataField="FinishedGoodsFlag" HeaderText="FinishedGoodsFlag" SortExpression="FinishedGoodsFlag" /> 
    8             <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" /> 
    9             <asp:BoundField DataField="SafetyStockLevel" HeaderText="SafetyStockLevel" SortExpression="SafetyStockLevel" /> 
    10             <asp:BoundField DataField="ReorderPoint" HeaderText="ReorderPoint" SortExpression="ReorderPoint" /> 
    11             <asp:BoundField DataField="StandardCost" HeaderText="StandardCost" SortExpression="StandardCost" /> 
    12             <asp:BoundField DataField="ListPrice" HeaderText="ListPrice" SortExpression="ListPrice" /> 
    13         </Columns> 
    14     </asp:GridView> 
    15     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProducts" 
    16         TypeName="AdventureWorksModel.Product"></asp:ObjectDataSource> 

    Run the application and this is how your products list will show up.

     

    Download JQuery 1.4.1 and tablesorter plugins and them to the scripts folder of the website. Once done add these scripts to the head section of master page.

    1     <link href="~/Content/tablsorter.css" rel="stylesheet" type="text/css" /> 
    2     <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
    3     <script src="Scripts/jquery.tablesorter.js" type="text/javascript"></script> 

    In the HTML for gridview now let’s add a CSS class so that we can use it find the table by JQuery.

    1 <asp:GridView ID="gdvProducts" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" CssClass="tablesorter"> 
     

    Note the CssClass Property above which is set to tablesorter.

    Now add the following script in the products.aspx page –

    1         <script type="text/javascript"> 
    2             $(document).ready(function () { 
    3  
    4                 $('.tablesorter').tablesorter(); 
    5  
    6             }); 
    7         </script> 

    Run the application. Your application should be something similar to this –

     

    Notice the sort up and sort down arrows.

    Hope this helps.

    Cheers,

    Venkata

    Go comment!