Problem with User Control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bronquites
    New Member
    • Jun 2007
    • 1

    #1

    Problem with User Control

    Hi! I'm having a problem with a User Control that I want to implement in my project.

    All the variables and classes are declared as Public.

    My User Control has a procedure that calls the variables that are declared in the main page and calls a sub on the main page at the end of its cycle.

    However on the User Control, the variables and procedure that are in the main page show up as 'Variable 'xyz' Not Declared'.... how come? Shouldn't the Public scope solve these problems?

    This is my User Control:

    <%@ Control Language="VB" AutoEventWireup ="true" CodeFile="contr olo.ascx.vb" Inherits="compo nentes_controlo " %>

    <script language="vbscr ipt" runat="server">
    Public Sub adiciona_foto_a ctiva_1()
    id_activa = 0
    dia = "1"
    id_foto = 1
    form_fotos_acti vas()
    End Sub
    </script>

    And this is my main page:

    <!--#include virtual="compon entes\funcoes.a spx" -->

    <%@ Import Namespace="Syst em.IO" %>
    <%@ Import Namespace="Syst em.Data" %>
    <%@ Import Namespace="Syst em.Data.OleDB" %>

    <%@ Page Language="VB" AutoEventWireup ="true" CodeFile="index .aspx.vb" Inherits="_Defa ult" Debug="true" Explicit="true" %>

    <%@ Register Src="componente s/controlo.ascx" TagName="contro lo" TagPrefix="uc" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <script runat="Server">
    'page load
    Public Sub Page_Load()
    'definir caminho para imagens
    path_ficheiro = Request.MapPath ("foto_dia/")
    path_tmp = Request.MapPath ("tmp/")
    'definir caminho para base de dados
    path_db = Request.MapPath ("bd/foto_dia.mdb")
    'criar e abrir a ligação para base de dados
    db = New Data.OleDb.OleD bConnection("Pr ovider=Microsof t.Jet.OLEDB.4.0 ;Data Source=" & path_db)
    db.Open()

    'chamar o menu principal (onde está o formulário)
    menu_principal( )

    'definir o caminho da página (e mostrá-lo na label caminho)
    path_self = Request.FilePat h

    'saber qual a acção a executar
    accao = Request.QuerySt ring("accao")

    'select case - consoante a acção definida acima executar um caso particular
    Select Case (accao)
    Case "fotografos "
    adiciona_fotogr afo()
    lista_fotografo s()
    Case "adicionar_foto grafo"
    adiciona_fotogr afo()
    Exit Select
    Case "adicionar_foto grafo_2"
    adiciona_fotogr afo_2()
    Exit Select
    Case "editar_fotogra fo"
    edita_fotografo ()
    Exit Select
    Case "editar_fotogra fo_2"
    edita_fotografo _2()
    Exit Select
    Case "fotos_disponiv eis"
    adiciona_foto()
    Exit Select
    Case "listar_fotogra fos"
    listar_fotograf os()
    Exit Select
    Case "editar_fot o"
    edita_foto()
    Exit Select
    Case "editar_fot o_2"
    edita_foto_2()
    Exit Select
    Case "apagar_fotogra fo"
    apaga_fotografo ()
    Exit Select
    Case "apagar_fot o"
    apaga_foto()
    Exit Select
    Case "fotos_acti vas"
    adiciona_foto_a ctiva_1() <-- this is the sub that is on the User control
    ' adiciona_foto_a ctiva_tmp()
    lista_fotos_act ivas()
    Exit Select
    Case "adicionar_foto _activa_2"
    adiciona_foto_a ctiva_2()
    Exit Select
    Case "editar_foto_ac tiva"
    edita_foto_acti va()
    Exit Select
    Case "editar_foto_ac tiva_2"
    edita_foto_acti va_2()
    Exit Select
    Case "apagar_foto_ac tiva"
    apaga_foto_acti va()
    Exit Select
    End Select
    'fechar a ligação à base de dados
    db.Close()
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <uc:controlo id="Controlo1" runat="server" Visible="true" />
    (below this are the remaning subs and some other stuff.)

    Thanks for all the help!
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by Bronquites
    Hi! I'm having a problem with a User Control that I want to implement in my project.

    All the variables and classes are declared as Public.

    My User Control has a procedure that calls the variables that are declared in the main page and calls a sub on the main page at the end of its cycle.

    However on the User Control, the variables and procedure that are in the main page show up as 'Variable 'xyz' Not Declared'.... how come? Shouldn't the Public scope solve these problems?

    This is my User Control:

    <%@ Control Language="VB" AutoEventWireup ="true" CodeFile="contr olo.ascx.vb" Inherits="compo nentes_controlo " %>

    <script language="vbscr ipt" runat="server">
    Public Sub adiciona_foto_a ctiva_1()
    id_activa = 0
    dia = "1"
    id_foto = 1
    form_fotos_acti vas()
    End Sub
    </script>

    And this is my main page:

    <!--#include virtual="compon entes\funcoes.a spx" -->

    <%@ Import Namespace="Syst em.IO" %>
    <%@ Import Namespace="Syst em.Data" %>
    <%@ Import Namespace="Syst em.Data.OleDB" %>

    <%@ Page Language="VB" AutoEventWireup ="true" CodeFile="index .aspx.vb" Inherits="_Defa ult" Debug="true" Explicit="true" %>

    <%@ Register Src="componente s/controlo.ascx" TagName="contro lo" TagPrefix="uc" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <script runat="Server">
    'page load
    Public Sub Page_Load()
    'definir caminho para imagens
    path_ficheiro = Request.MapPath ("foto_dia/")
    path_tmp = Request.MapPath ("tmp/")
    'definir caminho para base de dados
    path_db = Request.MapPath ("bd/foto_dia.mdb")
    'criar e abrir a ligação para base de dados
    db = New Data.OleDb.OleD bConnection("Pr ovider=Microsof t.Jet.OLEDB.4.0 ;Data Source=" & path_db)
    db.Open()

    'chamar o menu principal (onde está o formulário)
    menu_principal( )

    'definir o caminho da página (e mostrá-lo na label caminho)
    path_self = Request.FilePat h

    'saber qual a acção a executar
    accao = Request.QuerySt ring("accao")

    'select case - consoante a acção definida acima executar um caso particular
    Select Case (accao)
    Case "fotografos "
    adiciona_fotogr afo()
    lista_fotografo s()
    Case "adicionar_foto grafo"
    adiciona_fotogr afo()
    Exit Select
    Case "adicionar_foto grafo_2"
    adiciona_fotogr afo_2()
    Exit Select
    Case "editar_fotogra fo"
    edita_fotografo ()
    Exit Select
    Case "editar_fotogra fo_2"
    edita_fotografo _2()
    Exit Select
    Case "fotos_disponiv eis"
    adiciona_foto()
    Exit Select
    Case "listar_fotogra fos"
    listar_fotograf os()
    Exit Select
    Case "editar_fot o"
    edita_foto()
    Exit Select
    Case "editar_fot o_2"
    edita_foto_2()
    Exit Select
    Case "apagar_fotogra fo"
    apaga_fotografo ()
    Exit Select
    Case "apagar_fot o"
    apaga_foto()
    Exit Select
    Case "fotos_acti vas"
    adiciona_foto_a ctiva_1() <-- this is the sub that is on the User control
    ' adiciona_foto_a ctiva_tmp()
    lista_fotos_act ivas()
    Exit Select
    Case "adicionar_foto _activa_2"
    adiciona_foto_a ctiva_2()
    Exit Select
    Case "editar_foto_ac tiva"
    edita_foto_acti va()
    Exit Select
    Case "editar_foto_ac tiva_2"
    edita_foto_acti va_2()
    Exit Select
    Case "apagar_foto_ac tiva"
    apaga_foto_acti va()
    Exit Select
    End Select
    'fechar a ligação à base de dados
    db.Close()
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <uc:controlo id="Controlo1" runat="server" Visible="true" />
    (below this are the remaning subs and some other stuff.)

    Thanks for all the help!
    I would recommend using Session variables to pass to your stored procedure. That should allow you to access the value of the variable on the main page. Give that a try and let us know if this solves your problem.

    Nathan

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Nathan has given you a really good solution.
      The reason you are having this problem is because you're setting the variables in your main page to those stored in your user control, and then your user control goes out of scope and all of that information is lost.

      Sessions will help you get around that.

      If you have something against sessions let us know.

      -Frinny

      Comment

      Working...