Can not access properties of controls of user control???

C

chaoy

Hi,

I have created a user control, and need to change the properties of
controls on the user control. However, if the form of user control are
not runat=server, I got error of "...is null or not a object". If I
have the form runat=server, I got error of "A page can have only one
server-side Form tag". For detail, please code below.

It will be greatly appreciated if anyone can give me quick reply.

Thanks.

Charles

<%@ Register TagPrefix="mintfact"
Namespace="mintFact.webServerControls" Assembly="cMenu" %>
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs"
Inherits="JavaInUserControl.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<form id="form11" name="form11" runat =server>

<P>
<TABLE id="Table1" style="HEIGHT: 27px" cellSpacing="1"
cellPadding="1" width="700" align="center" border="1">
<TR>
<TD align="middle">
<P align="center"><Center>
<mintfact:cMenu id="CMenu1" runat="server"
MenuData="MenuData.xml" TopMenuIsHorizontal="True"
MenuWidth="102"></mintfact:cMenu></Center>
<P></P>
</TD>
</TR>
</TABLE>
</P>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P></P>
<DIV style="DISPLAY: inline; WIDTH: 153px; HEIGHT: 24px"
ms_positioning="FlowLayout">above
is server control</DIV>
<P></P>
<DIV style="DISPLAY: inline; WIDTH: 153px; HEIGHT: 19px"
ms_positioning="FlowLayout">below
is a htmp control</DIV>
<P><INPUT id="hdnWidth" type="hidden" name="hdnWidth"
runat="server"></P>
<script language="javascript">
debugger;
var iWidth;
iWidth = window.screen.width;
iWidth = (iWidth - 700)/2-10;
document.form11.elements
//document.form11.TextBox1.value = iWidth;
document.form11.hdnWidth.value = iWidth;
//if (iWidth != 800)
//document.all.CMenu1.Left = 60;

//document.all.hdnWidth.value = window.screen.width
//document.all.item("servercontrol").value=sWidth;
sWidth = "try";
</script>
</form>
 
J

John Saunders

Hi,

I have created a user control, and need to change the properties of
controls on the user control.

ASP.NET is based on OO principals. One of the most basic of the OO
principals is "encapsulation". This means that what's inside of the User
Control is concern of the User Control alone, unless it chooses to expose
its internals.

If you have a control, say, "Btn1" within your user control, and if it's
really necessary that arbitrary properties of that control be accessible
from outside of the user control, then the user control should have a
property to expose that control:

public Button ButtonOne
{
get {return Btn1;}
}

The user of an instance of the user control (call it UserControl1) could
then access UserControl1.ButtonOne.Text, for instance.

If you only need a few properties of the inner controls to be visible, then
make only those properties visible:

public string ButtonOneText
{
get {return Btn1.Text;}
set {Btn1.Text = value;}
}

public Color ButtonOneForeColor
{
get {return Btn1.ForeColor;}
set {Btn1.ForeColor = value;}
}

Hiding the details of the inner controls allows you the freedom to change
how they are implemented in the future. For instance, if only certain
properties are exposed, you could decide some day to change Btn1 to be a
LinkButton or ImageButton, and the callers of your user control would never
need to know.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top