| Author |
Topic Search Topic Options
|
fouroeight
Newbie
Joined: 02 October 2003
Status: Offline
Points: 6
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
Bunce
Senior Member
Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
fouroeight
Newbie
Joined: 02 October 2003
Status: Offline
Points: 6
|
Post Options
Thanks(0)
Quote Reply
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
|
 |
Bunce
Senior Member
Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
Bunce
Senior Member
Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
fouroeight
Newbie
Joined: 02 October 2003
Status: Offline
Points: 6
|
Post Options
Thanks(0)
Quote Reply
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
|
 |
Diep-Vriezer
Senior Member
Joined: 06 August 2003
Location: Netherlands
Status: Offline
Points: 831
|
Post Options
Thanks(0)
Quote Reply
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..
|
 |
fouroeight
Newbie
Joined: 02 October 2003
Status: Offline
Points: 6
|
Post Options
Thanks(0)
Quote Reply
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
|
 |