UserControls & Properties

  • Thread starter Thread starter Andy Breward
  • Start date Start date
A

Andy Breward

Hi

I have created a range of usercontrols for displaying
various bits of information. My question is if I wish to
pass a parameter to the UserControl from the host page,
is this possible?

I have tried setting an attribute in the Server Tag which
only works if I use a constant as it won't allow the <%
=xxxx%> parameters within the control!

Any ideas?

Thanks in advance

Andy
 
Hi Andy,

Based on my research, a server tag cannot contain <% ... %> constructs. To
assign a value to an attribute of a server tag, we should use server-side
code via the id of that server tag from within code-behind class or or
inline <script runat=server> </script> section.

I have tested this issue with the hyperlink control and gotten the same
result. The following is the testing code:
...
<%
string href;
href = "http://www.microsoft.com";
%>
...
<A href="<%=href%>">http://www.microsoft.com </A>
<asp:HyperLink id="HyperLink1" NavigateUrl = "<%=href%>" style="Z-INDEX:
101; LEFT: 8px; POSITION: absolute; TOP: 112px"
runat="server">http://www.microsoft.com</asp:HyperLink>
...

The "<%=href%>" works as expected in the HTML Hyperlink but it does not
work in the ASP Hyperlink.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jacob

I don't think I explained things clearly enough :-) I am
aware of the Server Controls property assingments using
server side code.

My problem is with User Controls with a .ascx extension
you don't seem to be able to access these from the code
behind file and cannot allocate properties using the <%%>
inline code syntax either.

You can access the Request.QueryString object in the code
behind file for the control and it does pick up in page
parameters passed within the host file, but this does not
solve my problem.

Any further thoughts?

Regards

Andy
 
Hi Andy,

I am sorry if there is any misunderstanding but we are able to access the
property of the User Control (ascx) from the code behind file. Pleaes refer
to the following code snippet.

...
public class WebForm1 : System.Web.UI.Page
{
protected Footer Footer1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
public string href = "abcd";
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Footer1.testString = href;
}
...

Footer1 is a User Control.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi again

Ha Ha!

It doesn't add the reference in to the client ascx file
automatically, once I manually add these protected
references in to my controls it works :-)

Excellent news that makes a whole lot more sense now doh!

Thank you again Jacob, efficient as ever superb support.

best regards

Andy
-----Original Message-----
Hi Andy,

I am sorry if there is any misunderstanding but we are able to access the
property of the User Control (ascx) from the code behind file. Pleaes refer
to the following code snippet.

...
public class WebForm1 : System.Web.UI.Page
{
protected Footer Footer1;
protected
System.Web.UI.WebControls.HyperLink HyperLink1;
 
Back
Top