Wednesday, 20 November 2013

How to allow only letters in a text box with JavaScript

function lettersOnly(evt) {
       evt = (evt) ? evt : event;
       var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
          ((evt.which) ? evt.which : 0));
       if (charCode > 31 && (charCode < 65 || charCode > 90) &&
          (charCode < 97 || charCode > 122)) {
          alert("Enter letters only.");
          return false;
       }
       return true;
     }
 
 
 
onkeypress="return lettersOnly(event)" 
 
 
http://answers.oreilly.com/topic/134-how-to-allow-only-numbers-or-letters-in-a-text-box-with-javascript/ 

How to allow only numbers in a text box with JavaScript

  function numeralsOnly(evt) {
       evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
           ((evt.which) ? evt.which : 0));
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
           alert("Enter numerals only in this field.");
           return false;
          }
           return true;
   }
 
 
 
onkeypress="return numeralsOnly(event)" 

Tuesday, 19 November 2013

How To Change DataType of a DataColumn in a DataTable?

DataTable dtCloned = dt.Clone();
dtCloned.Columns[0].DataType = typeof(Int32);
foreach (DataRow row in dt.Rows) 
{
    dtCloned.ImportRow(row);
}

Monday, 18 November 2013

Set Default Page in Asp.net / ASP.NET Web Application. How do I change the default page when app is enetered

<configuration>
   <system.webServer>
      <defaultDocument enabled="true">
         <files>
            <add value="login.aspx" />
         </files>
      </defaultDocument>
   </system.webServer>
</configuration>
 
Above code is useful when we have a login page of web application
 and we want directly move to login page when we write
 domain name only. 
Suppose we have domain www.abc.com, when we write www.abc.com 
it will redirect to www.abc.com/login.aspx automatically. 

Export to excel

 Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename= Barcodes_Detail.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            StringWriter StringWriter = new System.IO.StringWriter();
            HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
            DataGrid datagrid = new DataGrid();
            datagrid.DataSource = m_details;
            datagrid.DataBind();
            datagrid.HeaderStyle.Font.Bold = true;
            datagrid.AllowPaging = false;
// Code for making all columns data text in excel. Otherwise numeric data : text-align: right.
// We need this code when a column have text as well as numeric data.. to resolve alignment issue we have to use below bold 5 lines
            foreach (DataGridItem item in datagrid.Items)
            {
                for (int i = 0; i < item.Cells.Count; i++) // loop through all the cells in grid
                    item.Cells[i].Attributes.Add("style", "mso-number-format:\\@");
            }

            datagrid.RenderControl(HtmlTextWriter);
           // string headerTable = @"<div style='width:100%;text-align:center; font-size :16px; font-weight :bold;'>" + search_text.Text + " </div>";
            string headerTable = @"<div style='width:100%;text-align:center; font-size :16px; font-weight :bold;'>" + search_text.Text + "</div>";
            Response.Write(headerTable);
            Response.Write(StringWriter.ToString());
            Response.Flush();
            Response.End();

Sunday, 17 November 2013

Asp.net Difference between Website and Web Application in C#, VB.NET

Web Application

1. If we create any class files / functions those will be placed anywhere in the applications folder structure and it is precomplied into one single DLL.

2. In web application we have chance of select only one programming language during creation of project either C# or VB.NET.

3. Whenever we create Web Application those will automatically create project files (.csproj or .vbproj).

4. We need to pre-compile the site before deployment.

5. If we want to deploy web application project we need to deploy only .aspx pages there is no need to deploy code behind files because the pre-compiled dll will contains these details.

6. If we make small change in one page we need to re-compile the entire sites.

WebSite

1. If we create any class files/functions those will be placed in ASP.NET folder (App_Code folder) and it's compiled into several DLLs (assemblies) at runtime.

2. In website we can create pages in multi programming languages that means we can create one page code in C# and another page code in vb.net.

3. Web Sites won’t create any .csproj/.vbproj files in project

4. No need to recompile the site before deployment.

5. We need to deploy both .aspx file and code behind file.

6. If we make any code changes those files only will upload there is no need to re-compile entire site  



MORE.............................

Scenarios in which web application projects are the preferred choice include the following:
  • You want to be able to use the Edit and Continue feature of the Visual Studio debugger.
  • You want to run unit tests on code that is in the class files that are associated with ASP.NET pages.
  • You want to refer to the classes that are associated with pages and user controls from standalone classes.
  • You want to establish project dependencies between multiple web projects.
  • You want the compiler to create a single assembly for the entire site.
  • You want control over the assembly name and version number that is generated for the site.
  • You want to use MSBuild or Team Build to compile the project. For example, you might want to add prebuild and postbuild steps.
  • You want to avoid putting source code on a production server.
Scenarios in which Web site projects are the preferred choice include the following:
  • You want to include both C# and Visual Basic code in a single web project. (By default, a web application is compiled based on language settings in the project file. Exceptions can be made, but it is relatively difficult.)
  • You want to open the production site in Visual Studio and update it in real time by using FTP.
  • You do not want to have to explicitly compile the project in order to deploy it.
  • If you do precompile the site, you want the compiler to create multiple assemblies for the site, which can include one assembly per page or user control, or one or more assemblies per folder.
  • You want to be able to update individual files in production by just copying new versions to the production server, or by editing the files directly on the production server.
  • If you precompile the site, you want to be able to update individual ASP.NET web pages (.aspx files) without having to recompile the entire site.
  • You like to keep your source code on the production server because it can serve as an additional backup copy.
---------------------------------------------------------------------------------------------------
  • You like to keep your source code on the production server because it can serve as an additional backup copy.
The following table summarizes the main differences.
AreaWeb application projectsWeb site projects
Project file structureA Visual Studio project file (.csproj or .vbproj) stores information about the project, such as the list of files that are included in the project, and any project-to-project references.There is no project file (.csproj or .vbproj). All the files in a folder structure are automatically included in the site.
Compilation
  • You explicitly compile the source code on the computer that is used for development or source control.
  • By default, compilation of code files (excluding .aspx and .ascx files) produces a single assembly.
  • The source code is typically compiled dynamically (automatically) by ASP.NET on the server the first time a request is received after the site has been installed or updated.
    You can precompile the site (compile in advance on a development computer or on the server).
  • By default, compilation produces multiple assemblies.
NamespacesExplicit namespaces are added to pages, controls, and classes by default.Explicit namespaces are not added to pages, controls, and classes by default, but you can add them manually.
Deployment
  • You copy the assembly to a server. The assembly is produced by compiling the application.
  • Visual Studio provides tools that integrate with Web Deploy (the IIS web deployment tool) to automate many deployment tasks.
  • You copy the application source files to a computer that has IIS installed on it.
  • If you precompile the site on a development computer, you copy the assemblies produced by compilation to the IIS server.
  • Visual Studio provides tools that integrate with Web Deploy (the IIS web deployment tool) to automate many deployment tasks.

Regular Expression at least one digit, at least one alphabetic character, no special characters, and from 8-20 characters in length.

 (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$

Tuesday, 5 November 2013

get first 50 characters in sql

SUBSTRING(myColumn, 1, 50)

Remove HTML tags in String

using System.Text.RegularExpressions;
...
const string HTML_TAG_PATTERN = "<.*?>";

static string StripHTML (string inputString)
{
   return Regex.Replace 
     (inputString, HTML_TAG_PATTERN, string.Empty);
}

Friday, 1 November 2013

Trim Or Remove space from start and end in textbox, Remove more than one space between two words, trim in javascript on blur.

 function trimdata(el){
         el.value = el.value.replace (/(^\s*)|(\s*$)/gi, "").replace (/[ ]{2,}/gi," ").replace (/\n +/,"\n");
        }

 <asp:TextBox ID="txtSecTitle"  runat="server" TabIndex="15" onblur="trimdata(this);"
                                                    MaxLength="50" CssClass="inputfield" />

How to avoid space in textbox, no space can enter in text box.

<html>
<head>
<script type="text/javascript">
 
function blockSpace(e)
{
 var key;
 var isCtrl = false;
 var keychar;   
 if(window.event) {
  key = e.keyCode;
  isCtrl = window.event.ctrlKey
 }
 else if(e.which) {
  key = e.which;
  isCtrl = e.ctrlKey;
 } 
 keychar = String.fromCharCode(key); 
 if(keychar==" ")
 {
  return false;
 } 
}
 
</script>
</head>
<body>
 
<input type="text" onkeypress="return blockSpace(event);" />
 
</body>
</html>