Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Conditional data in Datagrid
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Conditional data in Datagrid

 Post Reply Post Reply Page  12>
Author
fouroeight View Drop Down
Newbie
Newbie


Joined: 02 October 2003
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote fouroeight Quote  Post ReplyReply Direct Link To This Post Topic: Conditional data in Datagrid
    Posted: 02 October 2003 at 8:03pm
I have looked through many sites as well as several ASP.NET books, and am unable to find a solution to this. In a datagrid, I have a checkbox. It works fine. However, I'd like to put a condition on it. If a field ContestClosed <> "" then the text CLOSED appears, and you do not see a checkbox to select. I used to just loop through this in ASP, but you cannot put conditional statements inside a datagrid in ASP.NET.
 
I have found conditional statements that will let you return text, or the value of a field, but I cannot figure out to make it either show the checkbox or the text CLOSED.
 
Any suggestions?
 
 
Thanks.
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 3:19am

A template column can return anything.  If you place a custom function in that column you can return a checkbox or a simple label at runtime, depending on whatever criteria you specify.

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
fouroeight View Drop Down
Newbie
Newbie


Joined: 02 October 2003
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote fouroeight Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 6:12am

Oh my God, you are right!! My only issue now is getting it to recognize my DataBinder statement. It is usually preceded by <%#, but with it being in an IF statement, not sure how to do that.

My IF statement reads

<% IF DataBinder.Eval(Container.DataItem, "GuardClosed") = "" Then %>

Thanks for help on this. I have really been pulling my hair out on this one.

 

lawrence

Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 6:34am

Sorry, I'm in a bit of a rush at the mo, so will get back to you in detail in a couple of days if no-one else does.

Basically, you don't perform the IF statement from within the HTML itself.  From memory you pass a reference of the current row, to a function that you call in your template column.

You evaluate an IF statement in your function, and return your control.

Sorry I can't provide more at the moment, but if you search on google you should find some code references in the meantime.

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 6:39am

This is the basic jist:

<asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Status">
  <ItemTemplate>
    <%#ShowStatus(Container.DataItem( "online"))%>
  </ItemTemplate>
</asp:TemplateColumn>

Function ShowStatus(val)
  If val = 0 Then
    ShowStatus = "Offline"
  Else
    ShowStatus = "Online"
  End If
End Function

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
fouroeight View Drop Down
Newbie
Newbie


Joined: 02 October 2003
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote fouroeight Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 8:55am

I understand your Function code, but what I need to return is not just two text alternatives. This certainly doesn't work:

Function ShowStatus(val)
      If val = "True" Then
                ShowStatus = <asp:Label ID="lblClosed" Text='Closed' Runat="Server" />
      Else
                  ShowStatus = <asp:CheckBox ID="chkSelect" Runat="Server" />
        End If
End Function

How do I return either a checkbox or a label?

lawrence

Back to Top
Diep-Vriezer View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 August 2003
Location: Netherlands
Status: Offline
Points: 831
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diep-Vriezer Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 9:02am

You have to create a new control using the .CreateControl() command, wich creates a certain control at a location.

I used it from a code-behind file, but it should also work with regular pages, I guess...

Gone..
Back to Top
fouroeight View Drop Down
Newbie
Newbie


Joined: 02 October 2003
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote fouroeight Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 9:22am

WOW! This was MUCH easier to do in ASP. It just seems that ASP had much better flow from a logic standpoint than ASP.NET does. I could just code straight down and everything worked. Now I'm pulling up functions that have to add different commands, etc. - all just to do a simple If ... Then statement.

Oh well, time to learn something new. Can you give me any examples of .CreateControl() command code? Not sure how to refer to it.

 

lawrence

Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.