How to hide an element in an ASP. Net Control template based on the property of the bound object

Source: Internet
Author: User

In ASP. NET pages, this method is usually used to control the invisibility of some elements:

<% IF (this. Show edit panel) {%>

...... HtmlCode......

<% }%>

 

This method is convenient, but if you want to use it in the server control template to determine a property of the bound object, an error will be returned:

<% IF (eval ("Default photo ")! = NULL) {%>

...... HTML code to be displayed ......

<% }%>

Because our code is written in <% ...... To get the attributes of the currently bound object, write the code to <% #...... %>, But this modification is not correct:

<% # If (eval ("Default photo ")! = NULL) {%>

...... HTML code to be displayed ......

<% }%>

 

So what should we do? The following three methods are provided:

Determine and output text directly in embedded code:

<% # (Bool) eval ("use as default photo ")? "Current default image": String. Empty %>

This method is applicable when only text is output. Note: "Use as default photo" is a bool type attribute of the currently bound object.

Embed code in the visible attribute of the server control for judgment:

<Asp: button id = "button1" runat = "server" text = "set as default image" commandname = "setdefaultimage" visible = '<% #! (Bool) eval ("use as default photo") %> '/>

This method is applicable when you only need to control the invisibility of a single control. Note: "Use as default photo" is a bool type attribute of the currently bound object.

The placeholder control is used to load multiple elements as a container, and the code embedded in its visible attribute controls the hiding of multiple elements inside the container:

<Asp: placeholder id = "placeholder1" runat = "server" visible = '<% # eval ("Default photo ")! = NULL %> '> default photo: <a target =' _ blank 'Title =' click to open the original size image 'href = '<% # eval ("Default photo. full-size graph file path ") %> '>
'/>
<Br/> </ASP: placeholder>

This method is applicable when multiple elements need to be controlled simultaneously. Note: The "Default photo" is a custom property of the currently bound object.

 

Download the XPS version of this article:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.