If statement inside Repeater control - asp.net

Although there are several questions on this topic, I haven't found any satisfactory answers.
I have a Repeater that needs to display complex content. An IF statement is required within the template. I can't move this to the code-behind as I need to have server controls and user controls registered within the repeater. Here is what I need:
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<%# If CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
The above throws a compiler error. Any idea on how to achieve this? I need to evaluate the IsFix field in the If statement.

I would put each set of content within a server side panel, then on ItemDataBound event, set the visibility of one panel to true, and the other to false.
If visibility is set server side, then the front end content never even gets rendered.

Remove # from the code nuggets which is having if statement:-
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<% CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
Edit:
Try with conditional operator (I am not sure about the syntax in VB.NET, please check):-
<%# If(CBool(Eval("IsFix")), "Do Something", "Else do something" %>

It works perfectly!!
<%# If(CBool(Eval("bitExtemporaneo")) = True, "<img src=""Imagenes/Extempo.png"" alt=""Extemporaneo""/>", "")%>

Remove # from the code where is having if statement.
Then use the code like this.
<%
object objIsFix = ((System.Data.DataTable)rep.DataSource).Rows[_nIndex]["IsFix"];
if (bool(objIsFix)) {%>
<%-- HTML content including server and user controls --%>
<%}Else{%>
<%-- HTML content including server and user controls --%>
<%}%>
'_nIndex' is defined in the cs file

Related

WHy am I unable to add text along with <%# Container.DataItem %> in repeater in user control

I have a User Control which is dynamically placed by CodeBehind as follows:
Dim myControl As Control = CType(Page.LoadControl("~/Controls/mainMenu.ascx"), Control)
If InStr(Request.ServerVariables("url"), "/Login.aspx") <= 0 Then
mainMenu.Controls.Add(myControl)
End If
As per an example from my previous question on here.
Within this Control is a repeater which calls a database to generate values.
My Repeater mark-up is as follows
<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">
<ItemTemplate>
<p id='locationQuestions' title='<%# Container.DataItem %>' runat='server'></p>
</ItemTemplate>
</asp:Repeater>
The example above works fine, but I want to be able to prepend text to <%# Container.DataItem %> in the title attribute of that <p> to print to the browser like this is some text DATA_ITEM_OUTPUT
When I try to do that though, it prints this is some text <%# Container.DataItem %> exactly like that, ie, turning <%# Container.DataItem %> into text, NOT the value from the repeater code.
It was working fine before I made it into a dynamically inserted control, so I am thinking I might have something being generated in the wrong order, but given that it works without any prepended text, I am stumped to fix it!
I'm new to .net and using vb.net, please could someone point me in the right direction?
It would help to see the broken version of your code, but I gather you're doing this:
<p id='locationQuestions' title='This is some text <%# Container.DataItem %>' runat='server'></p>
If that's the case, try it like this:
<p id='locationQuestions' title='<%# "This is some text " & Container.DataItem %>' runat='server'></p>
Following Statement include the server side code in your page code.
<%# Container.DataItem %>
When you Server side code on the page with some manipulation then put
it in single quote and code as you do code behind.
E.g.
Text = '<%# "Employee Name:" & Eval("EmpName") %>'
so you title markup code should be like this:
title='<%# "This is Title " & DataBinder.Eval(Container.DataItem, "Name") & "Text." %>'
Best of all, the Databinder.Eval syntax is the same for VB and C#.
check this link for more details.

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

How to make UpdatePanel inside ListView work?

I have a page with a listview that shows something like posts. On each post there should be a "rate box" which works similar to the "Like" button in facebook. The rate box is a User Control, that has an update panel inside it.
If I put the control with some random values in the page it works great - but when I put it inside the ListView, where it should be located, it won't work. The method is being called, but nothing happens.
I simplified the code a bit to make it easier to understand:
The "rate box" control:
protected void OnRateClick(object sender, ImageClickEventArgs e)
{
Rate++;
RateAmountLiteral.Text = Rate.ToString();
RateButton.Visible = false;
FeedbackLiteral.Visible = true;
rateButtonPanel.Update();
}
ascx:
<div class="rate_div">
<asp:UpdatePanel ID="rateButtonPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
Rate:
<asp:Literal ID="RateAmountLiteral" runat="server"></asp:Literal>
<asp:ImageButton ID="RateButton" runat="server" ImageUrl="icn_rate.png"
OnClick="OnRateClick" />
<asp:Literal ID="FeedbackLiteral" runat="server" Visible="false">Thanks for rating!</asp:Literal>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
aspx (using the control):
<asp:ListView ID="PostsView" runat="server" ItemPlaceholderID="itemPlaceHolder2"
<LayoutTemplate>
<div class="posts_div">
<asp:PlaceHolder ID="itemPlaceHolder2" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="post_div">
<div class="post_body">
<%# CurrentPost.Body %>
</div>
<UC:RatingBox id="RatingBox" runat="server"
PostID="<%# CurrentPost.ID %>"
Rate="<%# CurrentPost.Rate %>"/>
By: <a href="<%# CurrentPost.Author.LinkToProfile %>">
<%# CurrentPost.Author.DisplayName %>
</a> |
<%# CurrentPost.LiteralTime %>
</div>
</ItemTemplate>
</asp:ListView>
While debugging I noticed the controls in the method "OnRateClick" are empty and don't contain the right values. Please advice.
Also if you have any comments about the way I did things, don't hold yourself.
Thanks
There are a lot things that you may not have set up, I cannot tell from just the code snippet you have given. But make sure you do the following: -
1) Place a ScriptManager "" on the page.
If you are using master page in your application and your web page uses the master page, place script manager in master page. Or alternatively,you can also place script manager on specific web pages anyway.
2) Add a Triggers for the button RateButton in your Update panel.

nested repeater pass value in header template

I have a nested repeater and i want to pass value in its header. Here is my code so far..
The main problem is the id of the control in header template is also coming from code behind.
<asp:Repeater ID="RptrProgCategory" runat="server">
<ItemTemplate>
<asp:Repeater ID="RptrPrograms" runat="server">
<HeaderTemplate><input type="hidden" id="<%= questvalue%>"/></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "cat") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I want value in questvalue from code behind. Any idea how to achieve this?
Edit: I wanted to put this value in a DataTable and bind that value in Repeater bcoz i want output like this may be <%# DataBinder.Eval(Container.DataItem, "questvalue") %> instead of <%= questvalue%>..but in tht case i am not able to find the control
Category1(id of hidden field )
subcat1
subcat2
subcat3
Category2(id of hidden field)
subcat4
subcat5..and so on..
Repeater mainRepeater = this.Page.FindControl("RptrProgCategory") as Repeater;
Repeater nestedRepeater = mainRepeater.FindControl("RptrProgCategory") as Repeater;
You can then do a FindControl in nestedRepeater for questValue.
Add a runat='server' to questvalue so that you can access it in code behind.
I am writing this from memory, syntax might not be correct but it should get you off in the right direction.
set the id of the control in the repeater to something like mycontrolId - and then on OnItemDataBound - (or even OnItemCreated) use findcontrol("mycontrolId") - and then change the id of the control to your questvalue param.

ASP.NET: How to convert <A> or HtmlAnchor to static text?

i have a repeater that will output a series of items:
<asp:repeater ... runat="Server">
<itemtemplate>
<%# GetItemText %>
<itemtemplate>
<asp:repeater>
But some items will not have an associated link, so i don't want them to be clickable. i tried making it a runat=server HtmlAnchor, and set the htmlAnchor.Disabled = true for the items are should not actually have a link - but they can still be clicked (it just makes the text gray)
i know how i'd do it in the olden days:
<% If IsLink Then %>
<A href="<% =GetItemLink%">
<% End If %>
<% =GetItemText %>
<% If IsLink Then %>
</A>
<% End If %>
But that's the messy mixing code and html ASP way. What's the ASP.NET way?
Use an <asp:HyperLink > control, which displays the text normally if no link is supplied.
Edited to include example:
<asp:repeater ... runat="Server">
<itemtemplate>
<asp:HyperLink ... runat="server" NavigateUrl="<%# GetItemLink(...) %>"> <%# GetItemText %></asp:HyperLink>
<itemtemplate>
<asp:repeater>
In the above example, the anchor tag will be rendered to html regardless, but if the NavigateUrl attribute is an empty string, there will be no href at all and every browser I've ever used renders the text in a manner similar to spans (so look out for custom styles on <a >'s).

Resources