Monday, July 14, 2008

Managing your code into Regions

Separate code with regions:
#Region "print functions"
  'place all related subs, functions, etc here.
#End Region


Why use Regions? Simply put, regions just allow you to organize your code. They provide no functionality or interact with the rest of your code. Regions allow you to separate code and collapse the regions.

Friday, July 11, 2008

Accessing a variable in your masterpage from an .aspx page.

If you have a regular .aspx page that inherits a master page and you want access to some variable(s) in that masterpage, consider the following.

Say you currently have a variable in your masterpage dim'd as such:

Dim foo As String = "bar"


In order to access this foo variable in any of your pages that inherit this masterpage, simply add the following code in your masterpage.

Public Property foo() As String
  Get
    Return foo
  End Get
  Set(ByVal value As String)
    foo = value
  End Set
End Property


Now, in your regular .aspx content page you can gain access to the foo variable by doing the following in your Page_Load sub:

Dim newFoo As String = CType(Me.Master, MasterPages_Home).foo()


Conversely, you can change foo's value (or "set" it) by doing:

CType(Me.Master, MasterPages_Home).foo() = "new value"


In this example, MasterPages_Home is the class name of our MasterPage.

Thursday, July 10, 2008

Clear Recent Projects in Visual Studio 2005

Tired of seeing your Recent Projects list appear on the start page of Visual Studio .NET? Follow the directions outlined below:

1. Go to: Start > Run
2. Type "regedit"
3. Navigate to "HKEY_CURRENT_USER\software\microsoft\visualstudio\8.0\projectMRUList"
4. Delete any unnecessary listings.


This is unconfirmed for 2008

Wednesday, July 9, 2008

Inserting Dynamic HTML via Code Behind

If you need to insert HTML or any other code into your aspx source code at runtime use an asp:Literal control. The control is placed where the HTML or code should show up:

<asp:Literal runat="server" ID="favIcon" />


Now in the code behind, you can reference the literal and place any code into the literal like so:

favIcon.Text = "<link rel=""shortcut icon"" href=""favicon.ico"" />"


asp:Literal's come in handy when you need to insert code at specific places in your html source. Page.ClientScript.RegisterClientScriptBlock() is a great way to insert JavaScript and other code at run time too, but unfortunately the code is placed on the bottom of the html source where ASP wants it and not where you want it.

This is in no way a replacement for RegisterClientScriptBlock() as this function has its own great benefits for inserting JavaScript.

Wednesday, July 2, 2008

Access any table field in RowDataBound

Make sure you're in a DataRow, we don't want to do this when in a header, footer, or paging row, then give the table's field name you're looking for:

Dim FieldValue As String = ""
If e.Row.RowType = DataControlRowType.DataRow Then
    FieldValue = DataBinder.Eval(e.Row.DataItem, "fieldname")
End If