Formatting DateTime value in .NET to RFC-822 format

Posted by Max | Posted in asp.net | Posted on 02-02-2009

0

If you need to generate a datetime value for RSS feed you need to use RFC-822 format for dates. You can do it the following Extension method:

public static string ToRFC822String(this DateTime date)
{
    return date.ToString("ddd, dd MMM yyyy HH:mm:ss ") +
        date.ToString("zzzz").Replace(":", String.Empty);
}
  • Share/Bookmark

Html Encode values for Telerik’s RadGrid control

Posted by Max | Posted in asp.net | Posted on 25-01-2008

3

RadGrid GridBoundColumn values are not HTML encoded by default (see screenshot), so there is a risk of cross site scripting attack.

RadGrid control and MS GridView controls displaying string array

Extract from code behind:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     string[] str = new string[] {
  4.         "test <h1>string</h1>"
  5.     };
  6.    
  7.     radGrid.DataSource = str;
  8.     radGrid.DataBind();
  9.  
  10.     GridView1.DataSource = str;
  11.     GridView1.DataBind();
  12.  }

There is no Html encode option available at this moment so the best you can do is to encode values with ItemDataBound event:

  1. protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
  2. {
  3.     if (e.Item is GridDataItem)
  4.     {
  5.         GridDataItem item = e.Item as GridDataItem;
  6.         item["Content"].Text = Server.HtmlEncode(item["Content"].Text);
  7.     }
  8. }

More information about this on Telerik support forum.

Another option is to create new new class which inherits from RadGrid column (e.g. GridBoundColumn) and override its PrepareCell method:

ASPX page:

  1. <rad:RadGrid ID="radGrid" runat="server">
  2.     <MasterTableView GridLines="Vertical" AutoGenerateColumns="False">
  3.         <Columns>
  4.             <wt:MyGridBoundColumn DataField="Field1" UniqueName="Field1" HeaderText="Field1" />
  5.         </Columns>
  6.     </MasterTableView>
  7. </rad:RadGrid>

Code behind:

  1. public class MyGridBoundColumn : Telerik.WebControls.GridBoundColumn
  2.     {
  3.         private bool _htmlEncode = true;
  4.  
  5.         public bool HtmlEncode
  6.         {
  7.             get { return _htmlEncode; }
  8.             set { _htmlEncode = value; }
  9.         }
  10.  
  11.         public override void PrepareCell(
  12.             TableCell cell,
  13.             Telerik.WebControls.GridItem item
  14.         )
  15.         {
  16.             base.PrepareCell(cell, item);
  17.             if (_htmlEncode)
  18.             {
  19.                 cell.Text = HttpUtility.HtmlEncode(
  20.                     cell.Text
  21.                 );
  22.             }
  23.         }
  24.     }
  25.  
  • Share/Bookmark