.Net Newbie web user control question

C

Craig

I'm having a problem calling a public method for a web user control. When I
reference the UC's id (MyTester) from within default.aspx.vb, I get a message
that the variable has not been declared. I'm posting the code below, can
anyone help out with this.

the statement that throws the error is:
[default.aspx.vb]
MyTester.SetAppInfo(1001)


[default.aspx]
<%@ Register tagprefix="test" tagname="Message"
src="UserControls/tester.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb"
Inherits="MasterSite._default"%>
[snip]
<form id="Form1" method="post" runat="server">
<test:message id="MyTester" runat="server" />
</form>
[snip]

[default.aspx.vb]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MyTester.SetAppInfo(1001)
end Sub


[tester.ascx]
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="tester.ascx.vb" Inherits="MasterSite.tester"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:Label id="lblTester" runat="server"></asp:Label>

[tester.ascx.vb]
Public Class tester
Inherits System.Web.UI.UserControl
Private _AppId As Integer
[snip]
Public Sub SetAppInfo(ByVal AppId As Integer)
'do some biz logic here
_AppId = AppId
End Sub

End Class
 
R

Robert Koritnik

Probably you didn't put your control as a protected (at least) declaration
in the page's codebehind. When you have server controls in the ASPX and not
declared in the codebehind, those controls don't get by any chance
automaticly created in the codebehind.

If you would use Design view instead of the HTML view of your page, the
protected declaration would be automaticly created for you in the
codebehind.
 
C

Craig

I tried dragging the user control to the web form in design view, but it
didn't create a declaration for the user control. This is what shows up in
the auto generated in the code behind:

Public Class _default
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Robert Koritnik said:
Probably you didn't put your control as a protected (at least) declaration
in the page's codebehind. When you have server controls in the ASPX and not
declared in the codebehind, those controls don't get by any chance
automaticly created in the codebehind.

If you would use Design view instead of the HTML view of your page, the
protected declaration would be automaticly created for you in the
codebehind.

--
RobertK
{ Clever? No just smart. }

Craig said:
I'm having a problem calling a public method for a web user control. When I
reference the UC's id (MyTester) from within default.aspx.vb, I get a message
that the variable has not been declared. I'm posting the code below, can
anyone help out with this.

the statement that throws the error is:
[default.aspx.vb]
MyTester.SetAppInfo(1001)


[default.aspx]
<%@ Register tagprefix="test" tagname="Message"
src="UserControls/tester.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb"
Inherits="MasterSite._default"%>
[snip]
<form id="Form1" method="post" runat="server">
<test:message id="MyTester" runat="server" />
</form>
[snip]

[default.aspx.vb]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MyTester.SetAppInfo(1001)
end Sub


[tester.ascx]
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="tester.ascx.vb" Inherits="MasterSite.tester"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:Label id="lblTester" runat="server"></asp:Label>

[tester.ascx.vb]
Public Class tester
Inherits System.Web.UI.UserControl
Private _AppId As Integer
[snip]
Public Sub SetAppInfo(ByVal AppId As Integer)
'do some biz logic here
_AppId = AppId
End Sub

End Class
 
R

Robert Koritnik

So simply put it there manualy. GIve it the same type and ID as in the ASPX
file.

--
RobertK
{ Clever? No just smart. }


Craig said:
I tried dragging the user control to the web form in design view, but it
didn't create a declaration for the user control. This is what shows up in
the auto generated in the code behind:

Public Class _default
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Robert Koritnik said:
Probably you didn't put your control as a protected (at least) declaration
in the page's codebehind. When you have server controls in the ASPX and not
declared in the codebehind, those controls don't get by any chance
automaticly created in the codebehind.

If you would use Design view instead of the HTML view of your page, the
protected declaration would be automaticly created for you in the
codebehind.

--
RobertK
{ Clever? No just smart. }

Craig said:
I'm having a problem calling a public method for a web user control.
When
I
reference the UC's id (MyTester) from within default.aspx.vb, I get a message
that the variable has not been declared. I'm posting the code below, can
anyone help out with this.

the statement that throws the error is:
[default.aspx.vb]
MyTester.SetAppInfo(1001)


[default.aspx]
<%@ Register tagprefix="test" tagname="Message"
src="UserControls/tester.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb"
Inherits="MasterSite._default"%>
[snip]
<form id="Form1" method="post" runat="server">
<test:message id="MyTester" runat="server" />
</form>
[snip]

[default.aspx.vb]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MyTester.SetAppInfo(1001)
end Sub


[tester.ascx]
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="tester.ascx.vb" Inherits="MasterSite.tester"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:Label id="lblTester" runat="server"></asp:Label>

[tester.ascx.vb]
Public Class tester
Inherits System.Web.UI.UserControl
Private _AppId As Integer
[snip]
Public Sub SetAppInfo(ByVal AppId As Integer)
'do some biz logic here
_AppId = AppId
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top