戰地連結︰ Home My Flickr NBA.com About

2008年10月28日星期二

ASP.NET CAPTCHA implementation with C#

Overview

CAPTCHA stands for "completely automated public Turing test to tell computers and humans apart." It is used to distinguish human end users from machines (including software).
This implementation is posted on CodeProject by BrainJar. A zip files containing the source code is attached with the post too.

Reference

CAPTCHA Image

Repeater with Empty Data Template

Overview

Apart from Paging Support, one of the features that Repeater lacks is the "Empty Data Template" that will be shown when there is no result too display. MONA blogged a very simple method to implement a "Empty Data Template" for Repeater, and this implementation does not include writing anything in code behind.

Reference

Repeater with Empty Data Template

Code Snippet

I modified the code from MONA's blog a bit, by changing the "Label" to "Panel" inside the Footer Template. In this way we can treat the "Panel" as "Empty Data Template".

<asp:Repeater ID="RptrContacts" runat="server">

    <ItemTemplate>

        <!-- Add your Item Template Here -->

    </ItemTemplate>

    <FooterTemplate>

        <asp:Panel ID="pnlEmpty" runat="server" Visible='<%#bool.Parse((RptrContacts.Items.Count==0).ToString())%>'>

            <asp:Label ID="Label1" Text="no result" runat="server" />

        </asp:Panel>

    </FooterTemplate>

</asp:Repeater>

Adding Paging Support to the "Repeater"

Overview

Repeater is very useful in displaying data with a fully customized layout. However, repeater does not have built-in paging support. I found an article in "4GuysFromRolla" by "Harrison Enholm", talking about how to add paging support to the Repeater or DataList, with the use of "PagedDataSource" Class.

Reference

Adding Paging Support to the Repeater or DataList with the PagedDataSource Class

Steps

  1. Add a Repeater to the aspx page:

    <asp:Repeater ID="RprChild" runat="server" OnItemDataBound="RprChild_DataBound">

         <ItemTemplate>

              <%# DataBinder.Eval(Container.DataItem, "FirstName") %>

         </ItemTemplate>

    </asp:Repeater>

  2. Setup a property to stored the "Current Page" variable of the Repeater in ViewState:

    public int CurrentPage

            {

                get

                {

                    // look for current page in ViewState

                    object o = this.ViewState["_CurrentPage"];

                    if (o == null)

                        return 0;    // By default shows the first page

                    else

                        return (int)o;

                }

                set

                {

                    this.ViewState["_CurrentPage"] = value;

                }

            }

  3. Declare a method "ItemsGet()" to assign a list of objects to the "PagedDataSource" objPds, and bind it with the Repeater:

    protected void ItemsGet(List<Profile> children)

            {

                // Populate the repeater control with the Items DataSet

                PagedDataSource objPds = new PagedDataSource();

                objPds.DataSource = children;

                objPds.AllowPaging = true;

                objPds.PageSize = 6;

     

                objPds.CurrentPageIndex = CurrentPage;

     

                // Disable Prev or Next buttons if necessary

                LkbPrevPage.Enabled = !objPds.IsFirstPage;

                LkbNextPage.Enabled = !objPds.IsLastPage;

     

                RprChild.DataSource = objPds;

                RprChild.DataBind();

            }

  4. Add a "Previous Page" & a "Next page" button, with their OnClick Event like these:

    protected void LkbPrevPage_OnClick(object sender, EventArgs e)

            {

                CurrentPage -= 1;

                ItemsGet(member.Children);

            }

     

    protected void LkbNextPage_OnClick(object sender, EventArgs e)

            {

                CurrentPage += 1;

                ItemsGet(member.Children);

            }

  5. Done!

2008年10月21日星期二

Switching Culture in an ASP.NET web app

Overview

In my previous blog post Globalization of ASP.NET with Resources File, I talk about my experience in providing multi-language support for an ASP.NET web app. In that example, the web app get the cultures from preference of user's browser. If a user wants to change the preferred culture, he/she has to change it in browser.
Is there a way to do it in the web app, so that user can change the culture by clicking a button / select from a dropdown? The answer is yes, and Michael Ulmann had done a very nice implementation of that by using "Master Page".

Reference

Developing an ASP.NET page with MasterPage and Localization

How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization (MSDN)

Steps

  1. Store the current culture name in a session variable.
  2. Make a new class (e.g. BasePage) inheriting System.Web.UI.Page, override method "InitializeCulture()". You cannot override this method in the MasterPage as the "MasterPage" class does not inherit System.Web.UI.Page, as  Example Code Snippet:

    protected override void InitializeCulture()

            {

                //retrieve culture information from session

                string culture = Convert.ToString(Session["MyCulture"]);

     

                Culture = culture;

               

                //set culture to current thread

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);

                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

     

                //call base class

                base.InitializeCulture();

            }

  3. In the Master Page, make two Buttons with "Command Argument" set to "zh-TW" and "en-US" respectively. Assign the following method as their On-Click event:

    protected void LbnCulture_Click(object sender, EventArgs e)

    {

        // Change Culture

        Session["MyCulture"] = (sender as LinkButton).CommandArgument;
        // Reload Page to let new culture take effect

        Server.Transfer(Request.Path);

    }

  4. Create a new ASP.NET page (e.g. SamplePage) using the above Master Page, inherit "SamplePage" from "BasePage" (the class created in Step 1). Then you can test the buttons.

2008年10月7日星期二

Using parameter with Microsoft Query 'CONTAINS' criteria

Overview

Microsoft Query does provide the "CONTAINS" criteria to check whether a field contains a value, the SQL 'WHERE' clause for this is "WHERE criteria_field LIKE '%value_to_check%'". The problem is how to use parameter with "CONTAINS" criteria?

Steps

With the '&' operator we can easily concatenate the parameter in the "LIKE '%%'" clause. The SQL is:
WHERE criteria_field LIKE '%' & ? &'%'

Query Access DB from Excel 2007 by "Microsoft Query"

Overview

Let's consider this scenario: I have a Access DB and want to retrieve the data in Excel. In Excel 2007, the easiest way to do it (as far as I know) is to use built-in Access connect ("Data" tab => "Get External Data" group => "From Access").

However, in this way I cannot pass parameter into the query, how about if I want to use the value of a cell as the query parameter? In this case, you can use "Microsoft Query" in Excel 2007.

Seems that "Microsoft Query" can be used on other DB, but I haven't tried it out yet.

Reference

  1. Connect to (Import) external data (Excel 2007)
  2. Use Microsoft Query to retrieve external data
  3. Customize a parameter query

Steps

Build a parameter query with "Microsoft Query"

  1. In Excel 2007, select "Data" tab => "Get External Data" group => "From Other Sources" => "From Microsoft Query"
  2. Select your Access database
  3. In the "Query Wizard", setup your query and select "View data or edit query in Microsoft Query" to open "Microsoft Query"
  4. In "Microsoft Query", click the "Show/Hide Criteria" button (the button with a pair of glasses on it)
  5. Select the Criteria Field (Field to be compared in SQL 'WHERE' clause), and enter "[Parameter Name]" in "Value" to specify a parameter in the SQL query.
  6. Switch to SQL view and you should see a 'WHERE' clause like this:
    WHERE customer_id = ?
    The question mark "?" represents a parameter.
  7. Go ahead and modify the SQL as you like. When finished, select "File" => "Return Data to Microsoft Office Excel" to save and exit.

Use a cell's value as parameter (For more detail, go to Reference 3)

  1. Click on any cell of your query result
  2. Select "Data" tab => "Connections" group => "Properties" (If "Properties" button is grayed out, redo Step 1 )
  3. Click "Connection properties" (The little button next to Connection Name)
  4. Select "Definition" tab, click "Parameters" (If there is no "Parameters" button, it means that your query doesn't contain parameter, redo Step 1-7 in previous section)
  5. Select the desired parameter, click "Get the value from the following cell:" and select the cell, you may also select "Refresh automatically when cell value changes"
  6. Done. Change the value of the cell to test the query.
  7. Happy building query! (Hopefully...)

2008年10月6日星期一

Eliminate error messages with "IFERROR" in Excel 2007

Overview

In making dynamic Excel spreadsheet, sometimes we may have a function with result "#REF" or "#DIV/0". Some of these error messages are avoidable, but some of them aren't. In this case we have to conceal (hide) the error message. In Excel 2003 or before, we were using the "IF" function to check whether the function results in an error. However, this workaround is cumbersome and not efficient, because we have to put the function twice inside the "IF" statement.

Now in Excel 2007, we have a new function "IFERROR" designed to due with this particular situation. Let's see how we can benefit from it.

Reference

Eliminate those unfriendly error messages with Excel 2007’s IFERROR function

Step

The step is very simple, just replace your old "IF" function:

=IF(SOME_FUNCTION(),”Error Message”, SOME_FUNCTION())

with this one

=IFERROR(SOME_FUNCTION(),”Error Message”)

and you are good to go~