This is an old blog. My new blog is moved here.

How to do smart paging (digg.com style) in ASP.NET using dataset

2/13/2008

I think I have seen somebody else doing this piece of code before, but was not able to locate it, so decided to write my own. This is the problem. You want to write your own paging based of ASP.NET dataset. (For some reason you have turned away from the data grid control, maybe for the fact that the paging is not SEO friendly like me) However, you run into a problem as your page numbers start reaching into double digest. So what is the solution? Well basically bunch of loops that determine which page position number you are currently on and then render an adequate HTML for it… so here we go:

Sub PagingLinks()
        currentPageNumber = Request.QueryString("p")

        If currentPageNumber < 1 Then
            currentPageNumber = 1
        End If

        Dim number_of_pages = (ds_Count / Per_page)
        number_of_pages = Math.Ceiling(number_of_pages) - 1
        Dim sb As StringBuilder = New StringBuilder()
        Dim i As Integer

        'hide paging if only one page
        If number_of_pages >= 1 Then
            sb.Append("<div id='paging'>")
            'previous record
            If Not currentPageNumber = 1 Then
                sb.Append(" <a href='" & strPageFileName & "p=" & currentPageNumber - 1 & + _
		"' class='p'>« " & ConfigurationManager.AppSettings("tx_previous") & "</a>")
            End If

            If number_of_pages < 20 Then ' if there are less than 20 record no paging breaking

                'numbers
                For i = 0 To number_of_pages
                    If Not i = currentPageNumber - 1 Then
                        sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
			 "'> " & i + 1 & " </a>")
                    Else
                        sb.Append("<span>" & i + 1 & " </span>")
                    End If
                Next

            Else

                If currentPageNumber <= 10 Then

                    'numbers
                    For i = 0 To 10
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				 "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next

                    sb.Append("<span class='pg_dots'>...</span>")

                    'numbers
                    For i = number_of_pages - 2 To number_of_pages
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				 "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next


                ElseIf currentPageNumber >= number_of_pages - 8 Then 'last 10 numbers scenerio
                    'numbers
                    For i = 0 To 3
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				 "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next

                    sb.Append("<span class='pg_dots'>...</span>")

                    'numbers
                    For i = number_of_pages - 10 To number_of_pages
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 + _
				 & "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next
                Else
                    'numbers
                    For i = 0 To 3
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				 "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next

                    sb.Append("<span class='pg_dots'>...</span>")

                    'numbers
                    For i = currentPageNumber - 5 To currentPageNumber + 3
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				 "'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next

                    sb.Append("<span class='pg_dots'>...</span>")

                    'numbers
                    For i = number_of_pages - 3 To number_of_pages
                        If Not i = currentPageNumber - 1 Then
                            sb.Append(" <a href='" & strPageFileName & "p=" & i + 1 & + _
				"'> " & i + 1 & " </a>")
                        Else
                            sb.Append("<span>" & i + 1 & " </span>")
                        End If
                    Next

                End If
            End If


            'next record
            If Not currentPageNumber = number_of_pages + 1 Then
                sb.Append(" <a href='" & strPageFileName & "p=" & currentPageNumber + 1 & + _
			 "' class='n'>" & ConfigurationManager.AppSettings("tx_next") & " »</a>")
            End If
            sb.Append("</div><!-- end of 'paging' -->")

            'builds string
            paging.Text = sb.ToString()
        End If

End Sub

Dataset into array in ASP.NET: how to calculate an average number of a column in a dataset

2/13/2008

I have come across an interesting programming problem in ASP.NET: I had some database columns that I needed to calculate an average number. After looking around for a while if there was something in the dataset object, but no success. My reasoning then went that I needed to extract this dataset row into an array, which I could then calculate an average out of.

Here is the function to convert a dataset column into an array:

Public Function DatasetToArray(ByVal ds As DataSet, ByVal intColomn As Int32) As Array
   Dim intRows As Int32 = ds.Tables(0).Rows.Count
   Dim arrValues(intRows)
   Dim i2 As Int32
   For i2 = 0 To intRows - 1
      arrValues(i2) = ds.Tables.Item(0).Rows(i2).Item(intColomn)
   Next
   Return arrValues
End Function 

Now, this function will calculate an avarage out of the array of numbers:

    Public Function AvarageNumber(ByVal arrCol As Array, ByVal RoundDecimals As Int32) As Double

        If arrCol.Length <= 1 Then
            Return 0
        Else
            Dim arrValues(arrCol.Length + 1)

            Dim i As Int32
            Dim nOfFull As Int32

            Dim intTotal As Double = 0
            For i = 0 To arrCol.Length - 1
                If arrCol(i) > 0 Then
                    nOfFull = nOfFull + 1
                    intTotal = intTotal + arrCol(i)
                End If
            Next

            intTotal = intTotal / nOfFull

            intTotal = Math.Round(intTotal, RoundDecimals)
            Return intTotal
        End If

Finally, this is how you call the functions, where ds is the dataset, the first number is the column index number, and the last number is how many decimals you want the result avarage number to have:

AvarageNumber(DatasetToArray(ds, 1), 1) 

Inquiry about authoring a book on how to create a web based ASP.NET CMS

1/30/2008

Last week I got a very flattering email from a Pack Publishing editor asking if I would be interested in authoring a book on how to write a web based ASP.NET CMS:

Dear David,

I am an acquisition editor at Packt Publishing, and am looking to bring out a developer’s guide for creating a modular ASP.NET-based CMS.

Would you be interested in authoring a book on this?

As I currently see it, the book will take the reader through the creation of an ASP.NET website with content-management features. Each chapter will add new features to the site, leaving the reader with a functional CMS system. Each chapter will describe the problems of the feature, then set about the design of the solution (to the feature), then move onto the implementation.

I believe this book would be an excellent addition to our current line up. If you are interested in contributing as an author, please let me know. Packt pays authors in the form of an advance and royalties.

I look forward to hearing from you.

Yours Faithfully,

James.

James Lumsden
Acquisition Editor
[Packt Publishing]

Tel: +44 (0) 121 683 1170
Web:  www.packtpub.com

Czech Social Networking site inspired by facebook and flickr

1/30/2008

fungu.cz

Identifying a gap in the social networking market in the Czech Republic, I have developed released another venture of mine: fungu.cz. This social networking site currently combines functionality of facebook, flickr, and myspace. I have built this site on ASP.NET platform using jquery as a JavaScript library.

While the technology is almost done, the true challenge still lays before me: are the Czech users ready for facebook type of website and did I get the functionality right to attract them?

With this project I was able to implement some pretty advanced AJAX functionality such as drag and drop image sorting, inline editing, widget based layout much like what pageflakes or iGoogle has. This was great development experience which enriched my toolset for other future projects. I am already planning to implement some of these JavaScript functions into the next version of Dave’s ASP.NET CMS.

 

How to do inline editing using jquery JavaScript Library (tied to ASP.NET)

1/30/2008

Here is a little jquery script for inline editing. This script can be used with any back end: php asp.net… There are some inline editing pluggins for jquery out there, but I thought they were really redundant for what I needed to do. 

Let's start with the HTML: 

<span class="edit" id="13-2071">My jquery inline edit zone</span>   

The "edit" class could be bind to click event. I use the id of the span tag to pass in info about which table column and row I want to edit. You can really create any name space you would like inside of the ID. I choose one number for row and one for column (also carrying the info about the table) 

//inline editing
$(".edit").click(function(){
var strId = $(this).attr("id");
var strfCode = strId.slice(0, 2);
strId = strId.slice(3);
var strEditText = $(this).text();
$(this).after("<span id='w" + strId + "' class='inEditW'>
<input class='edit' id='i" + strId + "'
type='text' value='" + strEditText + "' />
<a onclick='postEdit(" + strId + ", " + strfCode + ");'
class='editSave'>hotovo</a></span>")
$(this).hide()
return false;
});

Lastly you need to post the edited text to the server so here is what the post edit function could look like in jquery:

UI.prototype.postEdit = function(intid, intTId){
    var strChTX = $("#i" + intid).val();
    $.post("ajax_post.aspx",
    { m: "UpdateField",
        p: intid,
        p2: strChTX,
        p3: intTId},
        function(data){
            m.AlertMessage(data);
        }
    );

On the server you just need to catch the posted variables. In asp.net it is the Request.Form("m") method... and then do your database magic. 

UPDATE:

This simple tutorial is sketching how you can write simple in-line editing code yourself. You can also use a inline editing plug-in from already written for this purpose.