Category Archives: VB.NET

ASP.NET accessing session variables in class modules

Sessions are a great way to share variable across your entire application, but they’re not accessable directly in class modules since they’re outside of page scope. So if you try something like Session(“VariableName”) = value  inside of your class, you will get an error.

Fortunately there is a way.  Thru HttpContext.Current notation available such common properties as Session, Server, Application, Cache. So to access your session variable inside a class just use HttpContext.Current.Session(“VariableName”) = value

Ultrawebgrid: Highlight row on MouseOver

To highlight row in Infragistics Ultrawebgrid on mouse over event you just need to follow 4 simple steps:

First, define a class to be used for highlighting, for example:

<style type=”text/css”>.over { BACKGROUND-COLOR: lightcyan }</style>

Second, set mouse over/out handler (either in source or thru designer):

<ClientSideEvents MouseOutHandler=”MouseOutHandler” MouseOverHandler=”MouseOverHandler” />

Third, implement event handlers:

/* highlight row on mouse over */
function
MouseOverHandler(gridName, id, objectType){
     var
ohRow = igtbl_getRowById(id)
      if (ohRow) ohRow.Element.className =
“over”
}

/* return row backcolor on mouse out */
function
MouseOutHandler(gridName, id, objectType){
     var
ohRow = igtbl_getRowById(id)
      if (ohRow) ohRow.Element.className =
“”
}

Fourth (and this is important, see why) set background of the row style to inherit:

<RowStyleDefault BackColor=”Window” BorderColor=”Silver”
     
 BorderStyle=”Solid” BorderWidth=”1px”
       
CustomRules=”background-color:inherit” Cursor=”Default”> …

Now you’re all set! The row will be highlighted on mouse over.

Group By and Aggregates in .NET DataTable

Often there is a need to perform group by operations on in-memory .NET data tables. You can’t run a standard SQL statement and LINQ is not available prior version 3.0 of .NET. To compensate for this shortcoming I put together a small function that works in .NET 2.0. As input parameters it accepts source DataTable, column name to group by and a column name to perform aggregate operation on. It returns a grouped data as data table:

Function GroupBy(ByVal i_sGroupByColumn As String, ByVal i_sAggregateColumn As String, ByVal i_dSourceTable As DataTable) As DataTable


        Dim dv As New DataView(i_dSourceTable)

        'getting distinct values for group column
        Dim dtGroup As DataTable = dv.ToTable(True, New String() {i_sGroupByColumn}) 

        'adding column for the row count
        dtGroup.Columns.Add("Count", GetType(Integer)) 

        'looping thru distinct values for the group, counting
        For Each dr As DataRow In dtGroup.Rows
            dr("Count") = i_dSourceTable.Compute("Count(" & i_sAggregateColumn & ")", i_sGroupByColumn & " = '" & dr(i_sGroupByColumn) & "'")
        Next        

        'returning grouped/counted result
        Return dtGroup
End Function

The function first gets distinct values for group-by column, by creating a data view from source data table and using DataView’s “ToTable” method. It then loops thru these distinct values performing aggregate function on the source table using DataTable’s “Compute” method – Count in this case, but it can easily be replaced with other aggregates.

FileUpload and htmlfile: Access is denied error

If you use ASP.NET FileUpload control or <INPUT TYPE=”FILE” HTML> control in Internet Explorer on Windows XP SP2 or later, trying to enter file path manually may cause “htmlfile: Access is denied error” error if entered string is not well formed local or UNC path.

The reason is that the control in Internet Explorer after version 6 in Windows XP SP2 does not allow relative path and if the form is submitted via Javascript form.submit method (like for example a __doPostback() call in ASP.NET) you will get untrappable error from above, if the form is submitted by an <INPUT TYPE=”SUBMIT> button you won’t get any notice at all.

The way to work around this issue is to catch invalid file name prior form being sumbitted. Add onclick event to your submitting button (OnClientClick if this is ASP.NET server side button) and add following code to it:

onclick=”if (!FilePathIsValid()) return false;”

then add following JavaScript function to your code:

function FilePathIsValid() {
 var reg = /^(([a-zA-Z]:)|(\))(\{1}|((\{1})[^\]([^/:<>”|]*))+)$/g
 if (!reg.test(document.getElementById(‘xfuFile’).value)) {
    alert(‘Please enter valid file path’);
    return false
 } else
   return true
}

where ‘xfuFile’ is the name of your FileUpload control. The function uses Jens K. Suessmeyer’s RegEx Expression to test if the file path is well formed. If it’s not – user gets error message and the form is not submitted.

UltraWebGrid in OutlookGroupBy Mode: How to count total number of rows in subgroups

Infragistics UltraWebGrid has a nice feature – OutlookGroupBy mode – which allows to group rows by common field either by dragging them into a GroupBy Box or programmatically.

The only drawback – when you have several levels of groupings (group within a group) – the outer group shows count of inner groups, not total number of individual rows within a group.

before

When I contacted Infragistics tech support asking how to show total count of rows within a group they told me it was impossible. Well, turned out it wasn’t entirely true.

Continue reading →

Infragistics UltraWebGrid grouping in code problem (solved)

I was having a real hard time with this error  – when I set grid group in in code by assigning IsGroupedBy property of a column to “true” – every other grid.databind()  was throwing error “Failed to compare two elements in the array.” Event though I cleared (or so I thought) Bands collection, Rows collection and Columns collections with their respective .Clear() methods.

The solution?

Continue reading →

removeChild, SSL and Infragistics WebHtmlEditor

I was working with Infragistics WebHtmlEditor control, and on client side it was being moved around by appendChild, removeChild DOM methods. All worked fine until I tried it over SSL connection. Immediatly removeChild method cause Secure/Unsecure waring.  Digging a bit I found that it was an IE bug. According to Microsoft this problem occurs if the Web page script calls the removeChild method to delete a DIV element that references a background image, and they suggest either to set outerHTML of the DIV to an emtpy string (which I think is dumb if you still need to use element you removed), but their second solution to move background-image declaration into an external CSS class actually works. In reality this problem occurs not only with DIVs but with any HTML element with background image in its inline style. In my case WebHtmlEditor rendered itself as an HTML table and its cells had background-image specified. The control itself didn’t have corresponding images set, but its UseDefaultStyles property was set to BackrgoundImages – which resulted in rendered styles with background images. To apply modified Microsoft’s solution I set this property to None, then segregated background image into a separate class:

.HTML_EDITOR {
   background-image: URL(ig_common/images/htmleditor/backimagerow.gif)
}

and used that class in Toolbar and TabStrip CSSClass properties. Worked like a charm.