Conditional display of web page element

T

Tom Wells

I have a few things such as buttons and URLs that may or may not be shown
based on the value of a database column. I can hook to the database and get
the dataset and datarecord just fine. I can retrieve the value into a
variable fine. My problem is that I have no idea what code to use to tell a
button not to be displayed if I don't get a certain value. In ASP I would
just embed VBScript to check the value of the variable in an if then and
either include the button or skip it on the fly. I can't seem to figure out
how to do this in ASP.NET.

Would someone please help me out with a code sample or a pointer to where
this is explained?
 
G

Guest

You didn't say whether the controls in question are to be embedded in other
DataBound controls, like a DataGrid, or not.

To start, let's say that you have a button that's not in a grid. If should
not appear on the rendered page when some condition exists, you simplay set
the visible parameter of that control to false (e.g. Button1.visible=False).
When visible is false, the control does not get rendered at all. (It doesn't
even get rendered with display:none.)

Now, it does get a little more involved if you have lots of rows in a
DataGrid, let's say, and a button should appear in some rows and not others.
But this should work:

Use an Item Template in your DataGrid, instead of a bound column. Don't try
to use the Grid Designer to do anything more than add an Item Template and
bind it to a specific column. Any attempt to do anything else with that
thing poses serious risks to your mental health. Once you've set up a
template or two, close the Grid Designer and try to forget that you ever
looked at it. Go into the HTML view and you should see a <Columns> tag with
one or more <asp:TemplateColumn> tags contained within it. You should also
see something that looks roughly like this...

<%# DataBinder.Eval(Container.DataItem, "FULLNAME") %>

This expression is what causes the column value for "FULLNAME" to get
rendered into the DataGrid.

You could edit the codeto something like this...

<% If <%# DataBinder.Eval(Container.DataItem, "FULLNAME") %> = "Tom" Then %>
<INPUT Type="Button" Style="..." OnClick="...">
<% End If %>

HTH
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top