problem with user control

M

Mad Scientist

I am having problems setting a asp user control's property from code.


I have an asp.net page and have a user control "page_footer" which has
a property sFooterText. Here is the control:

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="page_footer.ascx.vb" Inherits="Site1.page_footer"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<script language="VB" runat="server">
Public sFooterText As String = ""
</script>
<table border="0" width="100%" cellSpacing="0" cellPadding="0">
<tr>
<td align="right"><font
class="copy"><%=sFooterText%></font>&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>


The control is defined at the top of my aspx page as so:

<%@ Register TagPrefix="site1" TagName="page_footer"
Src="page_footer.ascx" %>



Everything works great when i set the property explicitly on the page,
such as:

<site1:page_footer id="page_footer_1" runat="server"
sFooterText="Copyright 2004"></site1:page_footer><br>



However if i try to set it from codebehind's Page Load with a line
such as:

page_footer_1.sFooterText = "Copyright 2004"

the "page_footer_1" is underlined in blue, with a message "Name
'page_footer_1 is not declared."



Help me, Obi Wan.
 
M

Mad Scientist

In case anyone else has this problem, I found out what was wrong.

1. in the codebehind of the aspx page that includes the control,
before pageinit, i needed to (manually) add:

Protected WithEvents page_footer_1 As site1.page_footer

2. in the user control ascx itself, you can't put the property
declaration in a <script> tag. WRONG:

<script language="VB" runat="server">
Public sFooterText As String = ""
</script>

instead, it has to go in the code behind, above Private Sub Page_Load.
RIGHT:

Public sFooterText As String = ""
 

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

Top