Using UserControl.RenderControl()

Perhaps you want to reuse a usercontrol in a email? or you need to generate the html from a usercontrol for a specific purpose?

Firstly, here is the code to render the html from a user control to a string builder object:

1
2
3
4
5
6
7
8
9
   Dim sb As New StringBuilder()
   Using sw As New StringWriter(sb)
      Using htmlTw As New HtmlTextWriter(sw)
          Dim ucUserControl As UCUserControl = LoadControl("myUserControl.ascx")
          ucUserControl .Visible = True
          ucUserControl .Display() 'This is a custom method to do some processing
          ucUserControl .RenderControl(htmlTw)
      End Using
   End Using

Secondly, make sure that you set enableEventValidation=”false” on the page you are generating the html from. You will get an error if this is enabled. You could extend this futher by adding a boolean variable to the page and only disabling Event Validation while rending the control.

Thirdly, override the VerifyRenderingInServerForm method and make it doing “nothing”. If you do not do this, you will get an error saying that you have no form sever tag in your user control.

1
2
3
Public Overrides Sub VerifyRenderingInServerForm(control As System.Web.UI.Control)
'Do nothing
End Sub

Detect when a session has ended ASP.NET

A problem that I have had in the past is trying to find out when a session has expired within ASP.NET and how to deal with the expired session.   One solution is to add the following code into the Page_Init event.   You can add this event into a class that inherits the Page class so that you can reuse on more than one page or all pages if needed.

1
2
3
4
5
6
7
8
If Not Context.Session Is Nothing Then
If Session.IsNewSession Then
Dim szCookieHeader As String = Request.Headers("Cookie")
If Not String.IsNullOrEmpty(szCookieHeader) And szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0 Then
Response.Redirect("/sessiontimeoutpage.aspx")
End If
End If
End If

This code checks if a new session has been created and if the header has a ASP.NET Session id.   This makes sure that we are checking for a session that has just been created.

Match Whole Words Regular Expressions

Example:

Dim regExp As New Regex(String.Format(“\b{0}\b”, keyword), RegexOptions.IgnoreCase)

This is the same as doing the following:


1
2
3
note.note_content.StartsWith(InputKeyword & " ") OR _
note.note_content.EndsWith(" " & InputKeyword) OR _
note.note_content.Contains(" " & InputKeyword & " ")

References:

http://answers.oreilly.com/topic/217-how-to-match-whole-words-with-a-regular-expression/

http://stackoverflow.com/questions/810078/search-for-whole-word-with-linq-to-sql

Unable to cast object of type ‘ Issue

I came across this error:

System.InvalidCastException: Unable to cast object of type ‘<TakeIterator>d__3a`1[Class]’ to type ‘System.Collections.Generic.List`1[Class]’.

This occured when trying to use the Take extension (list.Take(n)) from a generic list of objects.   The problem is when you use the Take extension, it returns a new sequence (

1
IEnumerable).

I got around this issue by using:

list.Take(8).ToList() which gets the new sequence and then converts back to the generic list.

References:

http://stackoverflow.com/questions/1431782/unable-to-cast-object