Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Manage a checkbox via jQuery

the checkBox in html should be

<input type="checkbox" class="myCheckBox" />

Bind a click event to a checkbox

$(".myCheckBox").click(myCheckboxEvent);

The click event like this

function myCheckboxEvent() {
    alert($(this).is(':checked')); // alert the checked value
}

You can see, it is quite easy to ‘get’ the checkbox sender via jQuery -‘this’

Add NHibernate Configuration in web.config, and reuse an existing connectionstring

When we use nHibernate in asp.net, most time we use a nhibernate.cfg.xml to define the database connection. And for some scenarioes, we may also have a database connection defined in web.config. So, there might be 2 definitions for the same database. Here is the way to combine the 2 definitions and in web.config.

in web.config

 1:  <configuration>
 2:    <configSections>
 3:      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 4:    </configSections>
 5:    <appSettings>
 6:    </appSettings>
 7:    <connectionStrings>
 8:      <add name="MyDefaultDatabase" connectionString="Data Source=......" />
 9:    </connectionStrings>
10:    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
11:      <session-factory>
12:        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
13:        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
14:        <property name="connection.connection_string_name">MyDefaultDatabase</property>
15:        <property name="command_timeout">300</property>
16:        <property name="show_sql">false</property>
17:      </session-factory>
18:    </hibernate-configuration>
19:  ……………

 

and pay attention,

  1. The secion name must be ‘hibernate-configuration’ , or it will not work. Very tricky.
  2. The type must be ‘NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" , also very tricky.
  3. Use connection.connection_string_name if you want to reuse an existing database connection.
  4. in your code, use
  5. new NHibernate.Cfg.Configuration().Configure();
    instead of given a nhibernate.cfg.xml file parameter.

 

Now, you only have 1 database connections in your web.config~

Visual Studio LightSwitch

Microsoft Visual Studio LightSwitch helps you solve specific business needs by enabling you to quickly create professional-quality business applications, regardless of your development skills. LightSwitch is a new addition to the Visual Studio family. Visual Studio LightSwitch is designed to simplify and shorten the development of typical forms-over-data business applications.

Scenario: forms-over-data business
Advantages:

  •  simplify and shorten the development
  •  you do not need to know .net and etc.

The build-in starter kit
Customer Service
Application for managing customer accounts and invoices.

Expense Tracker
Application for creating and tracking expense reports.

Issue Tracker
Application for tracking issues logged to an IT helpdesk.

Job Candidate Tracker
Application for managing job applicants and open positions.

Performance Review
Application for tracking employee performance data.

Status Report
Application for tracking project updates and other statuses.

Time Tracker
Application for managing employee time entry.

HTML vs XHTML

HTML syntax is the format suggested for most authors. It is compatible with most legacy Web browsers. If a document is transmitted with an HTML MIME type, such as text/html, then it will be processed as an HTML document by Web browsers. This specification defines version 5 of the HTML syntax, known as “HTML5″.

XHTML syntax is an application of XML. When a document is transmitted with an XML MIME type, such as application/xhtml+xml, then it is treated as an XML document by Web browsers, to be parsed by an XML processor. Authors are reminded that the processing for XML and HTML differs; in particular, even minor syntax errors will prevent a document labeled as XML from being rendered fully, whereas they would be ignored in the HTML syntax. This specification defines version 5 of the XHTML syntax, known as “XHTML5″.

The DOM, the HTML syntax, and XML cannot all represent the same content. For example, namespaces cannot be represented using the HTML syntax, but they are supported in the DOM and in XML. Similarly, documents that use the noscript feature can be represented using the HTML syntax, but cannot be represented with the DOM or in XML. Comments that contain the string “–>” can only be represented in the DOM, not in the HTML and XML syntaxes.

The term MIME type is used to refer to what is sometimes called an Internet media type in protocol literature. The term media type in this specification is used to refer to the type of media intended for presentation, as used by the CSS specifications.

jQuery script to scroll to the top of window

$('html, body').animate({ scrollTop: 0 }, 'slow');

Call web service via jQuery in asp.net

step 1. Add Web service, and you must add a [System.Web.Script.Services.ScriptService] to the service class

(in webservice.asmx.cs file)

   1: /// <summary>

   2: /// Summary description for WebService1

   3: /// </summary>

   4: [WebService(Namespace = "http://tempuri.org/")]

   5: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   6: [System.ComponentModel.ToolboxItem(false)]

   7: // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

   8: [System.Web.Script.Services.ScriptService]

   9: public class WebService1 : System.Web.Services.WebService

  10: {

  11:     [WebMethod]

  12:     public string DeleteProduct(string id)

  13:     {

  14:         Thread.Sleep(800);

  15:         return "Hello World22: " + id;

  16:     }

  17: }

step 2. call webservice in aspx via jQuery

(in head of the page)

   1: <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

   2: <script type="text/javascript">

   3:     $(function () {

   4:         $('a').click(function () {

   5:             deleteProduct(111);

   6:         }); // end of  $('a').click(function () {

   7:  

   8:     });     // end of $(function () {

   9:  

  10:  

  11:  

  12:     function deleteProduct(id) {

  13:         $.ajax({

  14:             type: "POST",

  15:             url: "WebService1.asmx/DeleteProduct",

  16:             data: "{id: '" + id.toString() + "'}",

  17:             contentType: "application/json; charset=utf-8",

  18:             dataType: "json",

  19:             success: function (msg) {

  20:                 AjaxSucceeded(msg);

  21:             },

  22:             error: AjaxFailed

  23:         });

  24:     }

  25:     function AjaxSucceeded(result) {

  26:         alert(result.d);

  27:     }

  28:     function AjaxFailed(result) {

  29:         alert(result.status + ' - ' + result.statusText);

  30:     }  

  31: </script>