Print Page | Close Window

Conditional data in Datagrid

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=6125
Printed Date: 29 March 2026 at 4:25am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Conditional data in Datagrid
Posted By: fouroeight
Subject: Conditional data in Datagrid
Date 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.



Replies:
Posted By: Bunce
Date 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.


Posted By: fouroeight
Date 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



Posted By: Bunce
Date 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.


Posted By: Bunce
Date 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.


Posted By: fouroeight
Date 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



Posted By: Diep-Vriezer
Date 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..


Posted By: fouroeight
Date 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



Posted By: fouroeight
Date Posted: 03 October 2003 at 11:01am

Sorry for the short rant. This is my first .NET project. I understand how bad "spaghetti code" is, and that Functions, etc. help make .NET much better. Its just frustrating going through the learning curve.

Still looking for some explanations/samples of using .CreateControl(). Can't find much, even in MSDN articles, that relates to .NET anyway. Any help?

lawrence



Posted By: Diep-Vriezer
Date Posted: 03 October 2003 at 11:05am

Uhhhm, I'm also experimenting, but I thought this is the right way .

The tooltip says: "Forces the creation of the control, including the creation of the handle and any child control's". Still searching, so work in progress



-------------
Gone..



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net