ASP.Net repeater control - using conditional statements - asp.net

I am trying to use an if statement inside of a repeater control and receiving an InvalidOperationException on the if.
What I'm trying to do is run a block of code inside the repeater only if the current item has a UserType that is set to Admin.
<asp:Repeater ID="rptSingleValueHeaders" runat="server">
<ItemTemplate>
<% if (Eval("UserType").ToString() == "Admin") { %>
<div>
do stuff here
</div>
<% } else { %>
<div>
do other stuff
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
My datasource is defined on the aspx.cs and contains a property named UserType which is of type string. Let me know if I need to provide any more details. Thank you.

You could use server side visibility:
<ItemTemplate>
<div runat="server" visible='<%# (Eval("UserType").ToString() == "Admin") %>'>
I show this HTML
</div>
<div runat="server" visible='<%# (Eval("UserType").ToString() != "Admin") %>'>
I show this other HTML
</div>
</ItemTemplate>

Related

Repeater control: How to access dataitem property in itemtemplate

In Repeater control I can access the dataitem property in item template using <%# Eval("IsAvailable") %>,I want to apply if condition on IsAvailable and toggle showing div using <% %> syntax,how do I achieve that?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<% if([IsAvailable]){ %>
<div>test1</div>
<% }else{ %>
<div>test2</div>
<% }%>
</ItemTemplate>
</asp:Repeater>

ASP.NET Linkbutton not getting dynamic value for commandargument value

<%
foreach (training t in traininglist)
{
%>
<tr>
<td><%=t.TrainingId%></td>
<td><%=t.TrainingName%></td>
<td>
<asp:LinkButton runat="server" ID="EditBtn"
Text="Edit" OnCommand="editbtn_OnCommand"
CommandArgument='<%# t.TrainingId %>' CommandName="edit" />
</td>
</tr>
<% } %>
where,
training is the class and traininglist is List<training> defined in Page_Load() function in codebehind.
I am trying to call the
public void editbtn_OnCommand(object sender, CommandEventArgs e)
{
String myeid = e.CommandArgument.ToString();
....
}
Here, myeid is not getting value but always shows <%# t.TrainingId %>
i have already tried all other options like <%: t.TrainingId %> or <%=t.TrainingId %>
The output of Tag "<%= %>" is more likely similar to use Response.Write in your code. so these tags are used to display the value to the response object.
That's why,as per my understanding, You can't used these tags to set commandargument property of controls unless you are using DataBinding. If you are using DataBinding then these tags "<%= %>" are used to set the property of controls.
Because you are here looping through each item in list on html table, my suggestion is to use GridView or Repeater and then Bind through your List Object. if you are using this way, you can get rid of unwanted formatting issues of html tables also.
Refer http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
If you want to use repeater then you can use these specific tags, and this should be your code(not actual code, just sample one)
<asp:Repeater id="myRepeater" runat="server" >
<ItemTemplate>
<div>
<asp:LinkButton runat="server" id="EditBtn" CommandName="edit"
CommandArgument="<%#Container.DataItem.TrainingId %>" Text="Edit"
OnCommand="editbtn_OnCommand" />
</div>
</ItemTemplate>
</asp:Repeater>

ListView.DataItem is showing null

Listview binding code
<asp:Content ID="Content3" ContentPlaceHolderID="leftColumnPlaceHolder" runat="server">
<asp:ListView ID="lvQuestions" runat="server" OnItemDataBound='lvQuestions_ItemDataBound'>
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
<asp:Button ID="btnSubmitAnswers" runat="server" Text="Submit Answers" OnClick="btnSubmitAnswers_Click" />
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Container.DataItemIndex + 1 %>:<%# Eval("Body") %>
</div>
<asp:RadioButtonList ID="rdlAnswers" runat="server" DataSource='<%#Eval("ExamAnswer") %>' DataTextField='Body' DataValueField="AnswerId">
</asp:RadioButtonList>
</ItemTemplate>
</asp:ListView>
</asp:Content>
While fetching the listview items on submit button click..like below,
we are getting qsnItem.DataItem as NULL.
foreach (ListViewDataItem qsnItem in lvQuestions.Items)
{
}
Please suggest what is going wrong here.
The DataItem of all databound web-controls in ASP.NET are null on postbacks when you don't DataBind the control again, which is unnecessary when ViewState is enabled(default).
So you can use the controls in your templates to get the values:
foreach (ListViewDataItem qsnItem in lvQuestions.Items)
{
RadioButtonList rdlAnswers = (RadioButtonList)qsnItem.FindControl("rdlAnswers");
}
If you need to old values you need to load them from database or use the ListViewUpdatedEventArgs.OldValues Property.

Add div conditionally says The name container does not exist in the current context

I want to add HTML div conditionally in rotator control. I am using the following code but it says "The name container does not exist in the current context" at DataBinder.Eval(Container.DataItem, "COL_ID")
<telerik:RadRotator ID="rtrList" runat="server" Width="830px" Height="100px"
FrameDuration="10" RotatorType="ButtonsOver" ScrollDuration="450" WrapFrames="true"
ItemWidth="100" ItemHeight="80" >
<ItemTemplate>
<asp:HiddenField ID="hdfId" runat="server" Value='<%# Eval("COL_ID") %>' />
<div id="div" runat="server" title='<%# Eval("NAME") %>' class="widget_item">
<span>
<%# Eval("TITLE") %>
</span>
</div>
<%if (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COL_ID")) % 2 == 0)
{%>
<div></div>
<%} %>
</ItemTemplate>
</telerik:RadRotator>
Please guide me where I am wrong. as I am using DataBinder.Eval & Container.DataItem for the first time.
Regards,
Kash
The error implies that this data item does not exist in the item bound to the control.
For example. If you were binding to a table, the table must have a column called "Col_ID" or this will throw an error.
Can you add your code for binding the control to the data source?
Use this syntax instead of the if:
<%# (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COL_ID")) % 2 == 0) ? "<div></div>" : "" %>

Insert a User Control within an ASP:Repeater

I need to insert a user control into a repeater
<asp:Repeater runat="server" ID="rptAdditionalPages">
<ItemTemplate>
<div id="<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).DivID %>" class="tab_content">
<h1><%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Title %></h1>
<%# ((ftj.com.AdditionalPageForProductDetail)Container.DataItem).Body %>
<uc:EnrollmentMethod ID="EnrollmentMethod2" runat="server" />
</div>
</ItemTemplate>
</asp:Repeater>
When the user control is inserted with this method the code behind cannot find EnrollmentMethod2. Is it possible to add user controls in repeaters?
You can't find it because it's nested in the repeater. You will need to dig into the repeater to find it.
foreach (RepeaterItem item in Repeater1.Items)
{
EnrollmentMethod control = (EnrollmentMethod )item.FindControl("EnrollmentMethod2");
}

Resources