Conditional display of web page element

Discussion in 'ASP .Net' started by Tom Wells, Dec 27, 2004.

  1. Tom Wells

    Tom Wells Guest

    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?
     
    Tom Wells, Dec 27, 2004
    #1
    1. Advertising

  2. 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

    "Tom Wells" wrote:

    > 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?
    >
    >
    >
     
    =?Utf-8?B?UmljaA==?=, Dec 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Bywaters

    conditional data/table display

    Steve Bywaters, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    434
  2. Alec S.
    Replies:
    10
    Views:
    10,229
    Alec S.
    Apr 16, 2005
  3. HANM
    Replies:
    2
    Views:
    735
    Joseph Kesselman
    Jan 29, 2008
  4. MikeL

    Conditional serialization of an element?

    MikeL, Feb 17, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    288
    MikeL
    Mar 14, 2005
  5. libsfan01
    Replies:
    5
    Views:
    159
    Evertjan.
    Jan 16, 2007
Loading...

Share This Page