Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Control Question

Status
Not open for further replies.

emblewembl

Programmer
May 16, 2002
171
GB
Hi, I am new to asp.net so forgive me if this is a dumb question! I have just created a user control which is very simple and is basically just a header which will go on each page of my site. The code is as follows:
Code:
<%@ Control Language=&quot;VB&quot; %>
<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot; border=&quot;0&quot;>
    <tbody>
        <tr>
            <td>
                <img src=&quot;images/topImage1.gif&quot; /> 
            </td>
            <td>
                <asp:label class=&quot;SectionText&quot; id=&quot;SectionName&quot; runat=&quot;server&quot;></asp:label>
            </td>
        </tr>
        <tr>
            <td colspan=&quot;2&quot;>
                <img src=&quot;images/topImage2.gif&quot; /> 
            </td>
        </tr>
    </tbody>
</table>
In the page where I am using this control I set the text of SectionName in the Page_Load sub, as follows:
Code:
     'Set up text for section heading
     SectionName.Text = &quot;NEW ORDER&quot;
However, although this worked before I turned the header into a user control, it doesn't anymore, and I'm guessing I need to set the text value in a different way or place. Can you tell me how? The error I get is:

Compiler Error Message: BC30451: Name 'SectionName' is not declared.

Source Error:

Line 30:
Line 31: 'Set up text for section heading
Line 32: SectionName.Text = &quot;NEW ORDER&quot;
Line 33:
Line 34: DateToday.Text = today

Thanks! i love chocolate
 
Remember that everything is now Object Oriented. Think of your user control as just another class. SectionName is a private variable representing a label class for your user control class.

So to set it you need to define a property for that method.

If your only setting and not ever reading it back then this will work.

Public WriteOnly Property SetSectionName()
Set(ByVal Value as String)
SectionName.Text = Value
End Set
End Property


Then in your page code-behind access the id you gave the user control .SetSectionName = whatever

And thats that That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi Mark,

I tried to follow your example here (in C#) but with no luck.

Can you help?

------------------------------------------
Here is my Header.ascx file:

<%@ Control %>
<!-- Standard Header for MCLNO pages -->

<script language=&quot;C#&quot; runat=&quot;server&quot;>

public string SectionValue(string Val) {

set {
SectionName.Text = Val;
}

} //end SetSectionValue property



</script>

<!-------------------------------------------------------->

<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot; border=&quot;0&quot;>
<tr>
<td colspan=&quot;4&quot;>
<img src=&quot;/hcsd/mclno/images/lsuhsc-hcsd-mclno-banner.jpg&quot; border=&quot;0&quot; />
</td>
</tr>
<tr>

<asp:label class=&quot;SectionText&quot; id=&quot;SectionName&quot; runat=&quot;server&quot;></asp:label>

</tr>
</table>

--------------------------------------------------------

Here is my default.aspx file:

<%@ Page Language=&quot;C#&quot; Debug=&quot;true&quot; Trace=&quot;false&quot; %>
<%@ Register TagPrefix=&quot;MCLNO&quot; TagName=&quot;HEADER&quot; Src=&quot;../user_controls/header.ascx&quot; %>
<%@ import Namespace=&quot;System.Data&quot; %>
<%@ import Namespace=&quot;System.Data.SqlClient&quot; %>
<%@ import Namespace=&quot;System.IO&quot; %>
<%@ import Namespace=&quot;System.Web&quot; %>
<script runat=&quot;server&quot;>

public void Page_Load(Object sender, EventArgs e) {

UserControl1.SectionValue = &quot;<td width=\&quot;10%\&quot; align=\&quot;center\&quot; class=\&quot;titleText\&quot;>History</td>&quot;

if (!IsPostBack) {
string myConnection = ctlConnectStrings.SQLConnectionString;
SqlDataAdapter myCommand = new SqlDataAdapter(&quot;MCL_getMenu&quot;, myConnection);

myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
myCommand.Fill(ds, &quot;Menu&quot;);

}
} //end Page_load



</script>

<!--------------------------------------------------------------------------->

<HTML>
<HEAD>
</HEAD>
<BODY>

<MCLNO:HEADER id=UserControl1 runat=&quot;server&quot;></MCLNO:HEADER>


<MCLNO:FOOTER id=UserControl2 runat=&quot;server&quot;></MCLNO:FOOTER>

</BODY>
</HTML>

------------------------------------------------------

My goal is to set the label in the ascx file with the string (a <td> tag) to be placed into the table HTML of the header.

I get a compilation error saying ';' is expected on the &quot;set&quot; statement in the user control.

Any suggestions?

Thanks,
JC [ponytails2]
 
I got it to work!

I simply changed my user control to look like:

<%@ Import Namespace=&quot;System.Data&quot; %>
<%@ import Namespace=&quot;System.IO&quot; %>

<script language=&quot;C#&quot; runat=&quot;server&quot;>

public string SectionValue {

set {
SectionName.Text = value;
}

} //end SectionValue property



</script>

<!-------------------------------------------------------->

<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot; border=&quot;0&quot;>
<tr>
<td colspan=&quot;4&quot;>
<img src=&quot;/hcsd/mclno/images/lsuhsc-hcsd-mclno-banner.jpg&quot; border=&quot;0&quot; />
</td>
</tr>
<tr>

<asp:Label id=&quot;SectionName&quot; runat=&quot;server&quot;></asp:label>

</tr>
</table>


yea!!!!

JC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top