Page_load event firing twice. User control not properly loading

Posted by Phil on Stack Overflow See other posts from Stack Overflow or by Phil
Published on 2010-05-27T14:29:59Z Indexed on 2010/05/27 14:31 UTC
Read the original article Hit count: 285

Here is the code I am using to pull my usercontrol (content.ascx):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'load module 
        If TheModule = "content" Then
            Dim control As UserControl = LoadControl("~\Modules\Content.ascx")
            Controls.Add(control)
        End If

  End Sub

Within the usercontrol is the following code (data access taken care of by DAAB and ive replaced sql statements with 'sql'):

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data

Partial Class Modules_WebUserControl
    Inherits System.Web.UI.UserControl
    Dim db As Database = DatabaseFactory.CreateDatabase()
    Dim command As SqlCommand

    'database
    Dim reader As IDataReader

    'general vars
    Dim pageid As Integer
    Dim did As Integer
    Dim contentid As Integer
    Dim dotpos As String
    Dim ext As String
    Dim content As String
    Dim folder As String
    Dim downloadstring As String

    Function getimage(ByVal strin As String) As String
        If strin > "" Then
            dotpos = InStrRev(strin, ".")
            ext = Right(strin, Len(strin) - dotpos)
            getimage = ext & ".gif"
        Else
            getimage = String.Empty
        End If
        Return getimage
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
        'test
        Response.Write("(1 Test from within page_load)")

        'get session vars
        folder = Session("folder")
        pageid = Session("pageid")
        did = Session("did")

        'main content
        command = db.GetSqlStringCommand("sql")
        db.AddInParameter(command, "@pageid", DbType.Int32, pageid)
        reader = db.ExecuteReader(command)

        While reader.Read
            If reader("content") IsNot DBNull.Value Then
                content = Replace(reader("content"), Chr(38) + Chr(97) + Chr(109) + Chr(112) + Chr(59) + Chr(98) + Chr(104) + Chr(99) + Chr(112) + Chr(61) + Chr(49), "")
                If reader("id") IsNot DBNull.Value Then
                    contentid = reader("id")
                End If
            Else
                contentid = -1
                content = String.Empty
            End If
        End While

        Outputcontent.Text = content

        'contacts info
        If did = 0 Then
            command = db.GetSqlStringCommand("sql")
            db.AddInParameter(command, "@contentid", DbType.Int32, contentid)
            reader = db.ExecuteReader(command)

            While reader.Read()
                Contactinforepeater.DataSource = reader
                Contactinforepeater.DataBind()
            End While
        End If

        If Not did = 0 Then
            command = (db.GetSqlStringCommand("sql")
            db.AddInParameter(command, "@contentid", DbType.Int32, contentid)
            db.AddInParameter(command, "@did", DbType.Int32, did)
            reader = db.ExecuteReader(command)

            While reader.Read
                Contactinforepeater.DataSource = reader
                Contactinforepeater.DataBind()
            End While
        End If

        'downloads box
        command = db.GetSqlStringCommand("sql")
        db.AddInParameter(command, "@contentid", DbType.Int32, contentid)
        reader = db.ExecuteReader(command)

        While reader.Read
            If reader("filename") IsNot DBNull.Value Then
                downloadstring += "<a href='/documents/" & folder & "/" & reader("filename") & "'>"
                downloadstring += "<img src=images/" & getimage(reader("filename")) & " border=0 align=absmiddle />"
            End If
            If reader("filesize") IsNot DBNull.Value Then
                downloadstring += Convert.ToInt32((reader("filesize") / 1000)) & "kb - "
            End If
            If reader("filename") IsNot DBNull.Value Then
                downloadstring += "<a href='/documents/" & Session("folder") & "/" & reader("filename") & "'>" & reader("description") & "</a><br />"
            End If
        End While

        Dim downloadsarray As ArrayList
        downloadsarray = New ArrayList
        If downloadstring IsNot Nothing Then
            downloadsarray.Add(downloadstring)
        End If
        If downloadsarray.Count > 0 Then
            DownloadsRepeater.DataSource = downloadsarray
            DownloadsRepeater.DataBind()
        End If


        'get links
        command = db.GetSqlStringCommand("sql")
        db.AddInParameter(command, "@contentid", DbType.Int32, contentid)
        reader = db.ExecuteReader(command)
        While reader.Read
            Linksrepeater.DataSource = reader
            Linksrepeater.DataBind()
        End While

    End Sub
End Class

Now instead of seeing my page content and what should be within the repeaters on the page all I get is 2 x the output of Response.Write("(1 Test from within page_load)")

(1 Test from within page_load)(1 Test from within page_load)

This leads me to believe the page_load is firing twice, but not properly displaying all the information.

Please can one of you willing experts help me to get this working?

Thanks a lot in advance

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about events