Web Code Blog

A web repository of coding tips and knowledge base articles

Archive for March, 2010

Acknowledgements

Posted by Stefan Zvonar on March 25, 2010

Posted in Acknowledgements | Leave a Comment »

Split Function (SQL Server)

Posted by Stefan Zvonar on March 25, 2010

I always seem to need a split function in SQL Server and I can never remember which one I have used previously. This is one that I am using currently and it seems to be working quite well.

CREATE FUNCTION [dbo].[fn_Split]
(
    @List varchar(8000),
    @SplitOn varchar(1)
)
RETURNS @RtnValue table (
    Id int identity(1,1),
    SplitValue nvarchar(100)
)
AS
BEGIN
    While (Charindex(@SplitOn,@List)>0)
    Begin
        Insert Into @RtnValue (SplitValue)
        Select
            SplitValue = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
        Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
    End 

    If Len(@List) > 0
        Begin
            Insert Into @RtnValue (SplitValue)
            Select SplitValue = ltrim(rtrim(@List))
        End
    Return
END

Which I pretty much constructed straight from this article

Here is an example on how to call the split function:

SELECT SplitValue
FROM dbo.fn_Split('1,2,3,4,5', ',')

Hope this helps,

Stefan.

For more Web Code, ASP.NET, SQL Server and other development tips, please check back here at webcodeblog.com often.

Posted in SQL Server | 1 Comment »

EnableHistory for ScriptManager with Nested Master Pages (ASP.NET)

Posted by Stefan Zvonar on March 25, 2010

Scenario: You have a site utilising a series of Master Pages with a chain of inheritance. On one of the child pages, you show a list of results with page numbers.  There is an Ajax Update Panel on this page so that when the user clicks on the result page they want, the page will present the new rendered result list nicely and without a page flicker.

Problem: When a user navigates away from the page and then clicks the back button (or a history go back button) to return to the result list, the web page reloads but only shows the first page of the result list (instead of the result page they were viewing before they navigated away). This is because the results are rendered on an AJAX Update Panel and were not part of the original page.

Solution: For ASP.NET 3.5 only – Okay, so in your site’s master page (or where you define your script manager), you will need to add the enablehistory attribute like shown below:

     <asp:ScriptManager ID="ScriptManager1" runat="server"
        EnableScriptGlobalization="true" EnablePartialRendering="true"
        EnableHistory="true"></asp:ScriptManager>

Now, in the child page that you wish to track the history for, place the below code in your Page_Load method. Note that we can not simply implement a declarative event handler since in this situation we have a series of nested master pages, some levels down from where the script manager was defined:

     Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
     AddHandler sm.Navigate, AddressOf MyScriptManager_Navigate

And write your event handler in the child page code-behind, something like the below code:

    Protected Sub MyScriptManager_Navigate(ByVal sender As Object, _
                                           ByVal e As System.Web.UI.HistoryEventArgs)

        If e.State.Keys.Count > 0 Then
            Dim pageIndex As String = e.State("CurrentPage")
            If pageIndex IsNot Nothing AndAlso pageIndex <> "" Then
                ' Data bind the pages and show the appropriate page
                UpdatePanel1.Update()
            End If
        End If
    End Sub

Now do not forget to store the current page index into the script manager’s history point. So when the user clicks on a new result page number, you will want to write something like the below code. Note, you will need to replace the _pageIndex variable with whatever you have used to store the current result page number (maybe in viewstate):

    Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
    If sm.IsInAsyncPostBack AndAlso Not sm.IsNavigating Then
        sm.AddHistoryPoint("CurrentPage", _pageIndex.ToString())
    End If

One thing to remember though is that your event handler will be the very last thing to execute, well after the page has been loaded already and probably already pre-populated with the first result page information. You may notice your page quickly load the first page result set and then “flip” to your desired page that you kept in history. So you may want to monitor performance on this (I currently am). It does not seem to be too much of a big deal at the moment, but if it ever gets too clunky, you may want to opt to using something like a cookie to track page indexes :/

Hope this helps,

Stefan.

For more Web Code, ASP.NET, SQL Server and other development tips, please check back here at webcodeblog.com often.

Posted in .NET, ASP.NET | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.