Blog Archive

Search Blog

Blog List

Contact Me






Enter the code shown above in the box below
Send

 

Announcements

Recommended Websites

Events

Event StartEvent EndTitle

Recommended Reading

Most recent blog entries

Author: Darren McLeod Created: Wednesday, October 17, 2007 9:05 PM
This will contain tips and tricks I learn at work. Mostly about programming with Visual Studio 2005 Team System, ASP.NET 2.0, VB.NET.

Was using the MaintainScrollPositionOnPostback=true in a page directive with the ASP.Net 2.0 Wizard control and started getting a stack overflow error when clicking on a Sidebar button.  Could not find a decent fix so I opted for a workaround and removed the MaintainScrollPositionOnPostback attribute and implemented this:

http://aspnet.4guysfromrolla.com/articles/111704-1.2.aspx

Say you had the following select:

 

select id="ItemSelect" runat="server">

 option selected="selected">Select your itemoption>

 option>Item oneoption>

 option>Item twooption>

 option>Item threeoption>

select>

 

And you wanted to force the user to select Item one/two/three. You could do this by creating a RegularExpressionValidator to to accept anything but the “Select your item” option like so:

asp:RegularExpressionValidator ID="ItemRegularExpressionValidator" runat="server" ControlToValidate="ItemSelect"

                                ErrorMessage="Item Required" Text="*" SetFocusOnError="true" ValidationExpression="^((?!Select your item).)*$">asp:RegularExpressionValidator>

...

Read More »

Say you had a GridView with a dropdownlist like this:

asp:GridView ID="testGridView" runat="server">

Columns>

asp:BoundField DataField="TestField" />

asp:TemplateField>

ItemTemplate>

asp:DropDownList ID="TestDropDownList" runat="server" AutoPostBack="true">

asp:ListItem Value="1">Item 1asp:ListItem>

asp:ListItem Value="2">Item 2asp:ListItem>

asp:DropDownList>

ItemTemplate>

asp:TemplateField>

Columns>

asp:GridView>

 

And you set it’s datasource in the page load event like this:

ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load

If Not Me.IsPostBack Then

Dim testTable As New DataTable

Dim testColumn As New DataColumn

testColumn.ColumnName = "TestField"

testColumn.DataType = GetType(String)

testTable.Columns.Add(testColumn)

Dim testRow As DataRow

testRow = testTable.NewRow

testRow.Item("TestField") = "Item...

Read More »

If you want to get a list of the current objects private fields you can do so like this:

me.GetType.GetFields(system.Reflection.BindingFlags.Instance or Reflection.BindingFlags.NonPublic)

If you get this error message when running an ASP.NET unit test in VS Team System 2005:

 

The web request 'http://localhost:19298/' completed successfully without running the test. This can occur when configuring the web application for testing fails (an ASP.NET server error occurs when processing the request), or when no ASP.NET page is executed (the URL may point to an HTML page, a web service, or a directory listing). Running tests in ASP.NET requires the URL to resolve to an ASP.NET page and for the page to execute properly up to the Load event. The response from the request is stored in the file 'WebRequestResponse_lnkGetMoreWork_Click.html' with the test results; typically this file can be opened with a web browser to view its contents.

 

Remove these lines/attributes from the unit test method declaration:

 

HostType("ASP.NET"), _

AspNetDevelopmentServerHost("%PathToWebRoot%", "/"), _

UrlToTest("http://localhost/")> _

 

...

Read More »

If you have a DataList control and capture the ItemDataBound event like this:

 

Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound

 

And then suddenly have the urge to do something to e.item and expect it to exist after a postback you best be a suppressing that urge because nothing you do to e.item in the ItemDataBound event of a DataList control will survive a postback. The reason is described here:

 

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104649&wa=wsignin1.0

...

Read More »

You can set the title attribute on the drop down list items to have ToolTip popups on your DropDownList items. This does not work in IE6 but works in IE7. For example you can do it right in the aspx like this:

 

asp:DropDownList ID="DropDownList1" runat="server">

            asp:ListItem title="Choice 1 ToolTip" Value="1">Choice 1asp:ListItem>

            asp:ListItem title="Choice 2 ToolTip" Value="2">Choice 2asp:ListItem>

asp:DropDownList>

 

Visual Studio will say that “Attribute 'title' is not a valid attribute of element 'ListItem'.” but it will run fine.

Or you can do it in the code behind like this:

 

Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.DataBound

Dim toolTip As String = string.Empty

For Each item As ListItem In DropDownList1.Items

toolTip = GetToolTips.(Integer.Parse(item.Value))

item.Attributes.Add("title", toolTip)

Next

...

Read More »

Here is an excellent tip to speed up ASP.NET 2.0 Data Binding by avoiding reflection overhead:

http://www.dasblonde.net/CommentView,guid,edc5323f-2afa-4ae5-9513-fbfb380940d4.aspx

And if your in a VB project then of course you have to use CType to cast Container.DataItem like so:

asp:Label ID="Label3" runat="server" Text='CType(Container.DataItem, ConfigurationSection).SectionInformation.SectionName %>'>asp:Label>

Another benefit of doing this early binding, if your using VS 2005, is you get intellisense popup to aid in selecting the right property.

...

Read More »

To access a property on the masterpage in ASP.NET 2.0 you have to cast it like this:

For a Web Application project

CType(MyBase.Master, WebApplication1.Site1)

Or for a website project

CType(MyBase.Master, MasterPage)

Or you can specify the MasterType in the aspx like this:

For a Web Application project

@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="Default.aspx.vb"

    Inherits="WebApplication1._Default" %>

@ MasterType TypeName="WebApplication1.Site1" %>

Or for a website project

@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb"

    Inherits="Default" title="Untitled Page" %>

@ MasterType VirtualPath="~/MasterPage.master" %>

Which will then create a shadow property on Master that does the casting for you so in the code behind you can just use:

Me.Master

...

Read More »

Finally managed to get my DNN v4.6.2 install into the root, thanks to http://www.bahrenburgs.com/Default.aspx?tabid=58&EntryID=3.  My problem was I had tried darrenmcleod.com/ as the portal alias but it kept giving me an infinite redirect error.  Then I tried darrenmcleod.com as the portal alias as bahrenburgs suggested and everything is goodness.  Note that when you make a change like this just through the database you need to do something to cause your application to reload, like editing a line in web.config and saving it.