Thursday, December 30, 2010

Format width of AutocompleteExtender AjaxControlToolkit

By default, the width of the autocomplete list of autocomplete extender is inherited from the target control which is the textbox. However, you can customize the width and appearance of the ajax control:
1. Create an website
2. Add a webservice to your site
3. In your webservice, add a code to retrieve customer or any info.
4. In your website, add an aspx markup similar below:
 Note that CompletionListElementID target control is the div called autocomplete declared below the textbox.
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
 <asp:ServiceReference Path="~/YourSampleService.asmx"/>  
 <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>  
 <td><div id="AutoComplete" runat="server"> </div></td>  
 <asp:AutoCompleteExtender ID="AtNames" runat="server"  
 ServiceMethod="Your_method" CompletionListElementID="AutoComplete"  
 CompletionSetCount="25" TargetControlID="txtName" >  
5. Add a custom css for your div

   width: 250px !important;  
   overflow: visible !important;  
6. Compile and run your website
Reference: Increase Width of Autcomplete Extender

Wednesday, December 29, 2010

Remove autorun.inf virus in USB 2.0 Flashdisk

I was bothered by this weird pop-up generated by avira antivirus regarding autorun.inf..
After googling, the solution was to use cmd(previously DOS) command.

del /a:rhs [driveletter]:autorun.inf

Example: del /a:rhs F:autorun.inf (where F is my USB drive)


Monday, December 27, 2010

SQL Server Formatting Dates Tip/Article

Here is a handy site when im currently working with dates in sql server 2005/2008..

Sunday, December 26, 2010

WPF Datagrid Demo

I have developed a project on WPF before. WPF has been utilized in Vista OS and other software. But this cannot replace the stability of Windows Forms.
Still, I don't have ample time to dive deeply in WPF, hopefully soon! LOL.. :D
So, to begin with:
1. Download and install WPF Toolkit (Visual Studio 2008)
2. Open Visual Studio 2008
3. Add a WPF Project (Just call it GridDemo)
4. Add a DataGrid in your Window.xaml
5. Make sure to have referenced wpf toolkit:
6. Customize Your Grid
1:  <grid:DataGrid AutoGenerateColumns="False" Margin="8,11,10,78"  
2:  ItemsSource="{Binding}" Name="CusGrid">  
3:  <grid:DataGrid.Columns>  
4:  <grid:DataGridTextColumn Binding="{Binding Path=CustomerID}"  
5:  Header="Customer ID" />  
6:  </grid:DataGrid.Columns>  
7:  ....other columns goes here  
8:  </grid:DataGrid>  
7. In your Window1.cs file, populate your grid with resulset
 protected void GridLoad()  
  SqlConnection conn = new SqlConnection(your_connection_string);  
  SqlCommand cmd = new SqlCommand("select * from customers",conn);  
  DataSet ds = new DataSet();  
  SqlDataAdapter da = new SqlDataAdapter(cmd);  
  CusGrid.DataContext = ds.Tables[0];  
8. Run your application.
That's It!

Using jQuery Intellisense in an external javascript file.

Create an aspx page and an external javascript file. Call the javascript external file in your aspx page.

1:  <script src="js/jquery-1.4.1.js" type="text/javascript">  
2:  </script>  
3:  <script src="js/Sample.js" type="text/javascript">  
4:  </script>  

In your external .js file, add the following code on top of the js file page.
 ///<reference path="jquery-1.4.1.js"/>  
 function dothis()  
     //your code here  

Saturday, December 25, 2010

Code in gridview rowdatabound not working if rowstate is (Alternate|Edit)

In a gridview rowdatabound event I have this condition to check if rowstate is in edit mode:
if (e.Row.RowState == DataControlRowState.Edit) //row is in edit mode  
      TextBox actdaterec = (TextBox)e.Row.Cells[1].FindControl("txtDateRec");  
      actdaterec.Text = string.Empty;  
      TextBox cashRec = (TextBox)e.Row.Cells[2].FindControl("txtCashReceived");  
      cashRec.Text = string.Empty;  
The code above usually works if the rowstate is in edit mode, however on the succeeding rows the rowstate becomes Alternate|Edit. The code above does not work. So the solution would involve C# bitwise operator (&).
See the revised code below:
if ((e.Row.RowState & DataControlRowState.Edit) > 0) //row is in edit mode  
    TextBox actdaterec = (TextBox)e.Row.Cells[1].FindControl("txtDateRec");  
    actdaterec.Text = string.Empty;  
    TextBox cashRec = (TextBox)e.Row.Cells[2].FindControl("txtCashReceived");  
    cashRec.Text = string.Empty;  
The code will execute even if rowstate is Alternate|Edit. It will ignore the alternate value.
Reference: ASP.NET Forum

Friday, December 24, 2010

Formatting date values shown in label control inside a Gridview

Reference: Set Date Format in Gridview
To format date as short date format of a label control inside a template field use Bind() as shown below:
1:  <ItemTemplate>  
2:    <asp:Label id="lblDateRec" runat="server" Text='<%#   
3:    Bind("GeneratedDateRec", "{0:MM/dd/yyyy}") %>'></asp:Label>  
4:  </ItemTemplate>  

Reset autonumber in sql server (TSQL)

Here is the script to reset autonumbers in sql server. Usually, i have to delete the table contents and then reseed the tables.

TSQL Script:
1:  use Transmittal  
2:  go  
3:  delete from dbo.TransDetails;  
4:  delete from dbo.TransMaster;  
5:  DBCC CHECKIDENT (TransMaster, reseed, 0)  
6:  DBCC CHECKIDENT (TransDetails, reseed, 0)  

Validation not working if i added client script in a save button attribute property

In this scenario, I have a series of textbox controls that has required field validations and a save button with the following attribute property set in pageload event:
1:  btnSave.Attributes.Add("onclick", "return confirm  
2:  ('Proceed saving or updating this record?')");  

In most cases, this works fine, but the problem is if i clicked ok in the confirm dialog of javascript, the validation of required fields will be bypassed.

So the solution would be to modify the script to return false if cancel is pressed in the confirm dialog of javascript so that validation will work..

Here is the modified script:
 btnSave.Attributes.Add("onclick", "if(!confirm  
 ('Proceed saving or updating this record?'))  
 return false;");  

If the user will click ok, in the javascript confirm dialog, validations will be executed.

Thursday, December 23, 2010

Check if Leap Year

Below is a custom code i have been working to check if a year is considered as leap year.
  protected bool CheckLeap(int year)  
       return true;  
     return false;  

However, as i checked the DateTime class methods in MSDN, i found a handy tool to do the same functionality.
DateTime.IsLeapYear(int year);  


Tuesday, December 21, 2010

Re-order DataColumns in a DataTable

Here's how to re-order DataColumns in a DataTable. This solves a particular problem in a project I'm currently working with.

ADO.NET Entity Framework Tutorial

Here's an example from MSDN. There are some minor bugs that I encountered, but this is a good start.
ADO.NET Entity Framework in Winforms

Tuesday, December 14, 2010

Access DOM elements in ASP.NET Master/Content Pages using ClientID and jQuery

The example below will display Bill Murstein in textbox

var name = 'Bill Murstein';  
//javascript DOM  
document.getElementById('<%=txtCustomerName.ClientID%>').value = name;  

//jQuery Counterpart  

Monday, December 13, 2010

jQuery in ASP.NET Ajax does not work after partial postback

I encountered a problem with jquery in ajax with master page content. During first pageload, the jquery script works fine, but after partial postback, the jquery script does not work.

After a few googling activity, I came upon this site:

This explains how to use document.ready, pageload, and Application.init. In my previous code, i've used document.ready since my aspx page was not ajax enabled. But since, Im switching my application to ajax ready, the solution I came upon was using function pageLoad() instead of document.ready().
function Pageload() {
    $("#txtsomething").blur(function() {
        //your js code here  
        var x = 25 + 25;

Hope this insight will help other developers out there...

Here is the summary from the direct link:

  • Ideal for onetime initialization.
  • Optimization black magic; may run slightly earlier than pageLoad().
  • Does not re-attach functionality to elements affected by partial postbacks.


  • Unsuitable for onetime initialization if used with UpdatePanels.
  • Slightly less optimized in some browsers, but consistent.
  • Perfect for re-attaching functionality to elements within UpdatePanels.


  • Useful for onetime initialization if only ASP.NET AJAX is available.
  • More work required to wire the event up.
  • Exposes you to the “sys is undefined” error if you aren’t careful.

Formatting numbers with comma in JavaScript

In c#, this can be achieved by String.Format(), however in javascript/jquery, this can be achieved using a simple JavaScript function:

  function addCommas(nStr) {
      nStr += '';
      x = nStr.split('.');
      x1 = x[0];
      x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
      return x1 + x2;

In your script, you can call it using:
var number = 123456.99;  
var result = addCommas(number);  

Source: JavaScript Number Format

Tuesday, December 7, 2010

Manipulating DataTable Columns and Rows

I found this link to be of help. DataTable Manipulation

Saturday, December 4, 2010

CSS color for textbox in disabled state

i was wondering what's the hex equivalent of textbox color rendered in firefox/IE.
The default color is:
#7F9DB9 (Firefox/chrome/IE)
#DADADA (Safari)

CSS Code:
   border-color: #7F9DB9;  

Or you may use the border attribute and define other properties such as border-style, border-width and border-color.