How to make If statements in Databound ListView - asp.net

I have a ListView with many advanced controls and html tags. ListView is bound to collection of profiles when first profile in collection is current profile. current profile has few differences from other profiles ie. flash embed, js and some other stuff. I can access inside of my ListView Container.DataIndex property which gives me 0 as first item in index but i'm unable to use inline If statements like so
<% If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
this is because i must place pound to access databound item but neither this
<%# If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
How can i make inline If ?

Try this:
<% if (DataBinder.Eval(Container, "DataItemIndex")) { %>
do stuff
<% } else { %>
do other stuff
<%} %>
Here is a small summary of the inline aspx tags:
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-%283c25242c-3c253d2c-3c252c-3c252c-etc%29.aspx
But i would recommend to use ListView.ItemDataBound. It is less error-prone and more readable in codebehind.

Did you mean IIF? IIF - Returns one of two objects, depending on the evaluation of an expression.

Related

Using a page variable as an if statements conditon within a control

I have the following problem:
I have the variable $GiftID on my page.
I want to cycle through all of my gift objects using my function getGifts().
When the $ID of the gift is equal to the $GiftID of the page then I want something to happen.
Here is an example of my code:
$GiftID
<% control getGifts %>
<% if CurrentPage.GiftID = ID %>This is it!<% end_if %>
<% end_control %>
Using $CurrentPage.GiftID works when printing inside the control, but how on earth do I access it from within the if statement?
I am using SS 2.9
I have not used ss2.9 yet, but as far as I know you can not do <% if Top.GiftID = ID %> in any 2.x version, you can not compare 2 variables, you can only compare with static vaules. (but it is possible in 3.0)
So you have to do it on php side, if you want to only display the slected gift object, then:
if GiftID is actually the DB field for the has_one relation of Gift then you can just do <% control Gift %> and it will scope the Gift object with the GiftID
If you really have GiftID saved as DB field or otherwise, then can do
public function getGift() { return DataObject::get_by_id('Gift', $this->GiftID); }
both ways you can do <% control Gift %> and it will scope it
If you want to list all gifts and mark the current gift then you need to do it on php side (foreach the set of objects and set a flag on the current object)
You should be able to access the current page with Top:
<% control getGifts %>
<% if Top.GiftID = ID %>This is it!<% end_if %>
<% end_control %>

script tags in html

I'm having difficulties google what's the difference between the following script tags:
<%# ... %>
<% = ... %>
<% ... %>
Can someone help?
These tags also may be ASP.NET tags.
Here are links with the information about each tag:
<%# ... %> Data-Binding Expression Syntax
<%= ... %> Displaying from ASP.NET
<% ... %> Embedded Code Blocks in ASP.NET Web Pages
i don't know about the 1st one
but 2 and 3 was the jsp tag,
2 one is the expression tag like when you want to get value from jsp variable then you can use this tag
for e.g.
String arg = "Pratik"
now you want use this in jsp page anywhere
Hello <%= name %> ////// it will print on web page as Hello Pratik
the 3rd one is the script tag
when you want write block of jsp,java code you can write within this tag
for ex.
<%
String name="";
name = "abc";
out.println(name);
%>

MVC Html Layout C# code formatting

I insert into asp.net mvc views C# logic that manages layout like the following:
<% if (Model.People.Count > 0 ) { %>
<% foreach (var person in Model.People) { %>
...
<% }} else { %>
<span class="error">Sorry, no people</span>
<%} %>
I try to minimize <% %> code placing "{" symbol on the same line as it's condition (java-style). Html layout looks more clear to me after that.
Do you apply C# formatting rules to <% %> html injections "}" should be on a new line or manage layout in different way?
Thank you in advance!
Its totally up to you, whatever you find is more readable and maintainable.
The less inline server blocks you have the better though (in terms of preventing run-time code compilation errors).

How can i call class from aspx file?

i have a class which is in App_Code/Kerbooo.cs i want to call that class's method from aspx file (not from code behind) is it possible? if it is, how can i do? thank you very much already now.
If the method is static, then the following should work within the aspx page:
<% Kerbooo.Method1(...) %>
If the method is not static, then you'll need an instance of Kerbooo:
<%
var kerbooo = new Kerbooo();
kerbooo.Method1(...)
%>
First, import the namespace that your code in App_Code uses:
<%# Import Namespace="MyNamespace" %>
If your code isn't in a namespace yet, it's a good idea to put it in one.
Next, you can call your code either with <% code; %> or <%= code %>, depending on whether you want to write the results to the output stream or not.
Data binding, as in <%# %>, requires a little extra work, as do expressions in <%$ %>
You can use <% %> and put your code in between (if you want to write stuff out <%= %> is a short cut for response.write but you need to do this outside of the <% %>
<%
var bob = new Kerbooo();
..do stuff with class
%>
you can mix and match (this does lead to spaghetti code so be carefull)
e.g looping
<table>
<%
var bob = new Kerbooo();
foreach(var thing in bob.GetThings())
{
%>
<tr><td><%=thing.StuffToWrite%><td></tr>
<%}%>
</table>
And your method should be public if your aspx does not inherit from a class in codebehind

Issue with asp:ContentPlaceHolder and code blocks

When a content placeholder contains any code blocks it reports that the control collection is empty.
For instance:
MasterPage.aspx
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
APage.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain text content.
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content." %>
</asp:Content>
This will render the following:
Plain text content. Code block content.
Content1: 1
Content2: 0
Why is the master page's ContentPlaceHolder.Controls collection empty?
I want to check whether the ContentPlaceHolder has been populated (see also this question) but can't if it contains any <%= blocks.
Does anyone know a way around this?
As promised, I said I would take a look. Sorry I never uploaded last night, long day and needed to hit the hay!
So, I was checking out the ContentPlaceHolder.Controls collection differences between how they are populated. I noticed that when the code block is used, it flips to read only. At any other point, it will simply be empty or populated.
I therefore decided to throw in an extension method to check it for us:
ContentPlaceHolderExtensions.cs
public static class ContentPlaceHolderExtensions
{
public static bool ContainsControlsOrCodeBlock(this ContentPlaceHolder placeHolder)
{
if (placeHolder.Controls.Count > 0)
return true;
return placeHolder.Controls.IsReadOnly;
}
}
And then check this in the master page:
Site.Master
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<asp:ContentPlaceHolder ID="Content3" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
<div>Content3: <%= Content3.Controls.Count %></div>
<div>Content1 (Ex. Meth.): <%= Content1.ContainsControlsOrCodeBlock() %></div>
<div>Content2 (Ex. Meth.): <%= Content2.ContainsControlsOrCodeBlock() %></div>
<div>Content3 (Ex. Meth.): <%= Content3.ContainsControlsOrCodeBlock() %></div>
As proof-of-concept, I then added a content page:
Index.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain Text Content
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content" %>
</asp:Content>
And all rendered as expected (I believe)..
TBH, while it is not perfect.. I don't think we can get much more elegance in this situation. I am not sure how other control collections are set up in these different scenarios, so I only bolted on to the ContentPlaceHolder control.. Other templated controls may or may not work the same.
Thoughts?
You can download the project from here:
http://code.google.com/p/robcthegeek/source/browse/#svn/trunk/stackoverflow/964724
Too much for a comment, here's the full code that I finally got working (adapted from #Rob Cooper's answer):
public static bool HasContent( this ContentPlaceHolder placeHolder )
{
if ( placeHolder.Controls.Count > 0 )
{
LiteralControl textBlock;
ContentPlaceHolder subContent;
foreach ( var ctrl in placeHolder.Controls )
if ( (textBlock = ctrl as LiteralControl) != null )
{ //lit ctrls will hold any blocks of text
if ( textBlock.Text != null && textBlock.Text.Trim() != "" )
return true;
}
else if ( (subContent = ctrl as ContentPlaceHolder) != null )
{ //sub content controls should call this recursively
if ( subContent.HasContent() )
return true;
}
else return true; //any other control counts as content
//controls found, but all are empty
return false;
}
//if any code blocks are used the render mode will be different and no controls will
//be in the collection, however it will be read only
return placeHolder.Controls.IsReadOnly;
}
This includes two extra checks - firstly for empty literal controls (which occur if the page includes the <asp:Content tags with any whitespace between them) and then for sub-ContentPlaceHolder which will occur for any nested master pages.
The controls collection is empty because when <%= %> script tags are present, literal controls are not added to the control tree. However, server controls will still get added. So:
<asp:Content ID="Content2" ContentPlaceHolderID="Content2" Runat="Server">
<%= "Code block content." %>
<asp:GridView runat="server" ID="gvTest" />
</asp:Content>
<div>Content2: <%= Content2.Controls.Count %></div>
will return
Content 2: 1
Rick Strahl has a great article that explains this behavior:
To make code like this work, ASP.NET
needs to override the rendering of the
particular container in which any
script code is hosted. It does this by
using SetRenderMethodDelegate on the
container and creating a custom
rendering method ...
Rather than building up the control
tree literal controls, ASP.NET only
adds server controls to the control
tree when <% %> tags are present for a
container. To handle the literal
content and the script markup, ASP.NET
generates a custom rendering method.
This method then explicitly writes out
any static HTML content and any script
expressions using an HTML TextWriter.
Any script code (<% %>) is generated
as raw code of the method itself.
Unfortunately I can't think of any elegant solution to this conundrum.

Resources