<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

Formatting Strings onRowDataBound

by Tim 31. August 2010 16:58

Sometimes, just when you think you've got it all wrapped-up, there comes an unexpected oversight. In my GridView, I have a hyperlink column - I want the NavigateUrl to point to Google Translate and populate a text area with a dynamic keyword: For example:

<asp:TemplateField>
    <ItemTemplate>
      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Keywords", "http://translate.google.com/#en|no|{0}") %>' Target="_blank" Text='<%# Eval("Keywords", "Google Translation") %>'></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

I am trying to add a keyword in English that will be translated into Norwegian. The problem here is that The first letter of the keyword is uppercase so I need to make it lowercase for Google Translate to work properly (this may just be a bug on their side). The easiest way to tackle this is to make it lower case onRowDataBound:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink myLink = (HyperLink)e.Row.FindControl("HyperLink1");
            myLink.NavigateUrl = "http://translate.google.com/#en|no|" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Keywords")).ToLower();
        }
    }

I remove the NavigateUrl parameter from the source and add an onRowDataBound parameter to the GridView. Now my keyword(s) are lower case and Google Translate gives me the correct translation. Still got a long way to go before I learn Norweigan though.

Tags: ,

ASP.NET | C#

Comments are closed