Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional DataGrid Column 2

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
I would like to include a column in my data grid that will display a link to "myPage.aspx" if the DataItem value is "new". However, if the DataItem value is not "new" then I want to simply display the DataItem value.

I have tried the following code which does not work.

<asp:TemplateColumn>
<ItemTemplate>
<TABLE>
<TR>
<TD>

<%#
if Container.DataItem(&quot;status&quot;) = &quot;new&quot; then
<a href=&quot;myPage.aspx&quot;>Test</a>
else
Container.DataItem(&quot;status&quot;)
end if
%>

</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>

What is the proper method to achieve my goal? Game Over, Man!
 
Code:
<%# returnConditional(Container.DataItem(&quot;status&quot;)) %>

~~~~~~

protected function returnConditional(arg as object) as string
  dim output as string
  if arg = &quot;new&quot; then
    output = &quot;<a href=&quot;&quot;myPage.aspx&quot;&quot;>Test</a>&quot;
  else
    output = arg
  end if
  return output
end function
:)
paul
penny1.gif
penny1.gif
 
Is there a good link for material on the subject of inline code for ASP.Net (i.e. <%# code %>)? I have several ASP.Net books, but none really address the issue. The focus is on code-behind. I am interested in learning when to use it and proper syntax. Game Over, Man!
 
Paul, your amazing. Don't know what we'd do here without ya. Thanks. Great solution.
 
Jabba, you won't see much on it because there really isn't that much to say...

Pretty much, you're either going to use the straight
Container.DataItem(&quot;field&quot;)

or else the method that I've shown.

Maybe I'm missing your question, though, so if so, please post back.

:)
paul
penny1.gif
penny1.gif
 
From what I can gather from other forums as well, I believe you are right. I am not disappointed, however. I think the code-behind method makes a lot of sense. Game Over, Man!
 
Oh yes...

It's infinitely more elegant and manageable than the traditional spaghetti code methods of asp classic.

As far as deployment, consider this:

Let's say that you have a project with 5 directories, multiple subdirectories in each, and say... 3 pages on the root. Just for example.

Ok. Now, you need to go into these pages and make coding changes. Maybe the name of your main db has changed from &quot;almostReadyToKillClients&quot; to &quot;definitelyReadyToKillClients&quot;.

Ok, then. Using code-behind pages, you simply go through and make your changes to the code pages, and only the code pages. Test it... everything looks good.

Now, in asp classic, you would have had to deploy all the little individual pages where you made coding changes, right? Well, with the code-behind method, the only thing you have to release to make the change on your live site is the project .dll itself.

In fact, you don't even have to have the .vb or .cs files on the live server at all... All you need are the UI .aspx pages, and the /bin folder w/ your .dll.

Personally, I think this is a great design pattern, if for no other reason than what I've mentioned here... and there are many many other reasons as well.

:)
paul
penny1.gif
penny1.gif
 
I tested your suggestion and it worked beautifully. It has opened a whole new arsenal for me. Thanks :) Game Over, Man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top