Click here to Skip to main content
15,170,343 members
Articles / Programming Languages / Visual Basic
Posted 23 Feb 2009


17 bookmarked

Passing Data in DotNetNuke between ASCX Controls using Cookies

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Feb 2009CPOL3 min read
This article explores a method to send data in DotNetNuke ASCX controls.


In DotNetNuke (DNN), you work with web User Controls in a module rather than .aspx pages in a web form. This means your modules do not use forms to pass data from one control to another. In this article, I explore how to send data from one control to another using cookies to store data from a user edited form so that the selections can be used for further processing.

Using the code

Once you have created and designed a user control to display the form fields you wish to process, you need to do something to pass the selected options to another user control for processing. Since there are no <form></form> blocks in User Controls, you can't do:


You will have to do something like:

Response.Redirect(NavigateURL(TabId, "CompanySearchResult", _
                  "Mid=" & CStr(ModuleId)), False)

Using NavigateURL is a method in DNN for finding and loading controls registered in your custom module. You will have to register new .ascx controls in DNN before attempting to call NavigateURL.

Unfortunately, NavigateURL is not the same as Server.Transfer. It simply loads the referenced control into the browser. You do not have a method such as Response.Request("control") available to read the user selections. In order to get the entered data, you may opt to use cookies. There may be other ways to do this, but in this article, I will explore the use of cookies to persist user selections in a form.

This code walks through the loaded form and finds all the form fields where some data has been entered, and assigns the values to a cookie named with the form field control name:

Dim i As Integer
Dim Item As String
Dim ItemValue As String
Dim X As Integer

i = 0
For X = 0 To Request.Form.Count() - 1
    Item = Request.Form.Keys(X) 'grabs form field Name
    If InStr(Item, "ns_") > 0 Then 'test form field name
        If Request.Form.Item(X) <> "" Then 'acts on form fields data

            ItemValue = Request.Form.Item(X) 'assigns value to a shorter var
            Session(Item) = ItemValue.ToString 'converts for value to string 
            i = i + 1

        End If
    End If

Note, Session(Item) = ItemValue.ToString is the section where a cookie is dynamically generated and a value is assigned. In the code sample above, note as well the prefix "ns_" is checked before processing. This is included to ensure that only the form field controls desired are inspected. Prefixes on field names are optional, but can come in handy when using a MultiView or in setting up a VaidationGroup in Visual Studio.

As it stands, this code will generate name/value pairs for the appropriately prefixed form field elements. Unfortunately, when DNN renders controls, it appends prefix information on your form field control. Inspection of the cookie name/value pair will show things like this:

$ctr371$ViewSearchSOS$ns_NameSearch=Some User Entered Data

You'll probably want to get rid of the dynamically generated cruft to access the actual field control name, in this case, "ns_NameSearch".

Another issue to consider is the fact that you are using cookies to persist form selection data. If the user re-uses the form (to do another search, for example), your code that reads the existing cookie data will also see any previous entries on other fields not used in the new search. For example, if you search on a company name and city, cookies are generated for that data. If you do another search on a new company name and a state or zip code (but not a new city), the cookies read will still contain the old city data.

This amended code strips the DNN cruft and deletes all existing cookies:

Dim Item As String
Dim ItemValue As String
Dim X As Integer
Dim SplitItem() As String

Session.Contents.Clear() 'Clears all cookies for session.
i = 0

For X = 0 To Request.Form.Count() - 1
    Item = Request.Form.Keys(X)          
    If InStr(Item, "ns_") > 0 Then
       If Request.Form.Item(X) <> "" Then
          ItemValue = Request.Form.Item(X)
          'creates an array using $ as the deliminator
          SplitItem = Split(Item, "$")
          'finds the last item in the array
          Dim j As Integer = SplitItem.Length - 1
          'assigns the last item name to value
          Session(SplitItem(j)) = ItemValue.ToString
          i = i + 1
          End If
      End If

In the referring .ascx control, retrieve the name/value pairs from the cookies generated to display selections in a label on the control:

Sub Label1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) _
                     Handles Label1.PreRender
    Dim item As String
    Dim itemlist As New StringBuilder
    Dim ItemLength As Integer
    Dim ItemLengthTrim As Integer
    Dim ItemName As String

    itemlist.Append("You selected: <br>")

    For Each item In Session.Contents
        If InStr(item, "ns_") > 0 Then
            If Len(Session(item)) > 0 Then
                ItemLength = Len(item)
                ItemLengthTrim = ItemLength - 3
                ItemName = Right(item, ItemLengthTrim)
                itemlist.Append("=" & Session(item) & "<br>")
            End If
        End If

    Me.Label1.Text = itemlist.ToString

End Sub

Note the additional string processing to strip the "ns_" prefix for display purposes.

You can use the cookie name/value pair elsewhere to do SQL queries to the database, etc.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Steve Skelton
United States United States
No Biography provided

Comments and Discussions

-- There are no messages in this forum --