Design View ListView ASP.NET Logic - c#

I am trying to input logic in the source view in Asp.Net ListView. The problem is that the program is writing on the screen false or true when executing "If (isItTrue(test))". Does anyone know how to solve this problem?
<%# test= Eval("testId")%>
<%
If (isItTrue(test)) Then
%>
<asp:Button ID="btnTest" runat="server" Text="Like" />
<%
Else
%>
<asp:Label runat="server" Text="hello" </asp:Label>
<%
End If
%>

You could use ItemDataBound to check informations like this and show or hide the controls using your condition. try something like this in your code behine:
protected void ListViewTest_ItemDataBound(object sender, ListViewItemEventArgs e)
{
// if it is data item
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// call your function
if (isItTrue("test"))
{
// show the button
e.Item.FindControl("btnTest").Visible = true;
}
else
{
// show the label
e.Item.FindControl("lblTest").Visible = true;
}
}
}
And in your Listview, you could do something like this, setting the event and adding the controls on the place holder
<asp:ListView ID="ListViewTest" DataSourceID="..." OnItemDataBound="ListViewTest_ItemDataBound" runat="server">
<LayoutTemplate>
<table>
<tr>
<th>Column Name</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #CAEEFF" runat="server">
<td>
<%-- both controls are here --%>
<asp:Button ID="btnTest" runat="server" Visible="false" Text="Like"></asp:Button>
<asp:Label ID="lblTest" runat="server" Visible="false" Text="hello"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

Are you sure it's not this line: <%# test= Eval("testId")%> that is writing true or false to the output?

Related

asp.net c# FindControl inside Reapater inside HTML table return null

I have a repeater inside the HTML table the problem is I'm trying to get label value on item date bound event but it always returns null I tried to use the same code without the HTML table its working fine
so is there a way to get value while I'm using an HTML table
<table class="table table-responsive">
<asp:Repeater ID="DtImages" OnItemDataBound="DtImages_ItemDataBound" runat="server">
<HeaderTemplate>
<thead>
<tr>
<th>Image</th>
<th>update</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td class="cart_product_img">
<img src='<%#"../../../Images/Shoping/"+ Eval("Image") %>' />
</td>
<td class="price">
<asp:ImageButton ID="btninImage" OnClick="PinImage_Click" CommandName="PinImage" ImageUrl="~/img/web/pinn.png" runat="server" />
</td>
<td class="qty">
<div class="qty-btn d-flex">
<div class="quantity">
<asp:ImageButton ID="UpdateImg" OnClick="UpdateImg_Click" CommandName="UpdateImage" ImageUrl="~/img/web/upload.png" runat="server" />
<asp:Label ID="lblProId" runat="server" Text='<%# Eval("ProID") %>' ></asp:Label>
</div>
</div>
</td>
</tr>
</tbody>
</ItemTemplate>
</asp:Repeater>
</table>
code behind
protected void DtImages_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
Label Pinned =(Label) e.Item.FindControl("lblProId") ;// its return null
// some codes
}
When the repeater data bind triggers, headers, alternating rows, and a data row all are feed to that routine.
So, you have to do this:
protected void DtImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) |
(e.Item.ItemType == ListItemType.AlternatingItem))
{
Label Pinned = (Label)e.Item.FindControl("lblProId");
}
}

hide column in an asp.net listview?

I have below list view , how can i hide column by code behind ?
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated">
<LayoutTemplate>
<table class="table table-striped table-bordered small">
<tr class="table-secondary">
<th id="BlockHeader" runat="server" style="white-space: normal;">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" />
</th>
</tr>
<asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="BlockStatus" runat="server">
<%# Eval("BlockStatus")%>
</td>
</tr>
</ItemTemplate>
After binding data with list view i tried below code behind but with this header text only hide but column still can still visible
if (groupOrBlockValue == 'W')
{
AuditLogListView.FindControl("BlockHeader").Visible = false;
AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;
//AuditLogListView.FindControl("BlockStatus").Visible = false;
}
Missing part of the list view?
With this:
<asp:ListView ID="LstMarks" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr style="">
<td><asp:Textbox ID="Course" runat="server" Text='<%# Eval("Course") %>' /></td>
<td><asp:Textbox ID="Mark" runat="server" Text='<%# Eval("Mark") %>' Width="30px"/></td>
<td>
<asp:CheckBox ID="DoneLabs" runat="server" Checked = '<%# Eval("DoneLabs") %>' Width="30px"/>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" class="table">
<tr runat="server" style="">
<th runat="server" >Course</th>
<th runat="server">Mark</th>
<th id= "LabW" runat="server" >Completed Lab Work</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<br />
<br />
<asp:Button ID="cmdHide" runat="server" Text="Hide Lab check box" OnClick="cmdHide_Click" />
So note in the layout, we added a "id" = LabW - that lets you hide the header.
so, a simple button that would toggle (hide/show) the lvColum, then this works:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from StudentCourses",
new SqlConnection(Properties.Settings.Default.TEST4)))
{
cmdSQL.Connection.Open();
LstMarks.DataSource = cmdSQL.ExecuteReader();
LstMarks.DataBind();
}
}
}
protected void cmdHide_Click(object sender, EventArgs e)
{
Control ctrHeader = LstMarks.FindControl("LabW");
ctrHeader.Visible = !ctrHeader.Visible;
foreach (ListViewItem lvRow in LstMarks.Items)
{
CheckBox ckBox = (CheckBox)lvRow.FindControl("DoneLabs");
ckBox.Visible = !ckBox.Visible;
}
}
So, we get this:
And clicking on the button, we get this:
And both the values changed - they persist - even when you click again (to toggle and show the hidden columns).
Edit: =====================================================
So, say this markup:
<asp:ListView ID="AuditLogListView" runat="server"
OnItemCreated="AuditLogListView_ItemCreated">
<LayoutTemplate>
<table class="table table-striped table-bordered small">
<tr class="table-secondary">
<th id="BlockHeader" runat="server" style="white-space: normal;">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="Hotel Name" />
</th>
</tr>
<asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="BlockStatus" runat="server">
<%# Eval("BlockStatus")%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
And code behind button to hide is this:
protected void Button1_Click(object sender, EventArgs e)
{
Control ctrHeader = AuditLogListView.FindControl("BlockHeaderLiteral");
ctrHeader.Visible = !ctrHeader.Visible;
foreach (ListViewItem lvRow in AuditLogListView.Items)
{
Control BlockStat = (Control)lvRow.FindControl("BlockStatus");
BlockStat.Visible = !BlockStat.Visible;
}
}
At which event have you written this code?
Required this code in the OnDataBound event. And in your code this event missing.
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated" OnDataBound="AuditLogListView_DataBound">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" /><asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
C# Code:
protected void AuditLogListView_DataBound(object sender, EventArgs e)
{AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;}

Repeater Linkbutton Onclick not firing

I am having problem with the LinkButton onclick event not firing.
I have checked the following posts and taken the precaution of Postback but still joy
repeater linkbutton not firing
Repeater's Item command event is not firing on linkbutton click
Here is my Code so far
<asp:PlaceHolder runat="server" ID="phOrders">
<asp:Repeater ID="rprOrders" runat="server" OnItemCommand="rprOrders_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
<asp:LinkButton ID="lnkOrderByDate" runat="server" Text="Date" CommandName="OrderDate" OnClick="lnkOrderByDate_Click"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByOrderNumber" runat="server" Text="Order Number"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByProductNumber" runat="server" Text="Product Number"></asp:LinkButton></th>
<th>Product Description</th>
<th>Size</th>
<th>QTY</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong><%# Eval("OrderDate") %></strong></td>
<td><%# Eval("OrderNumber") %></td>
<td><%# Eval("SKUNumber") %></td>
<td><%# Eval("OrderItemSKUName") %></td>
<td><%# Eval("mtrx_Code2") %></td>
<td><%# Eval("OrderItemUnitCount") %></td>
<td><strong><%# Eval("OrderItemStatus") %></strong></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="track-footer"></div>
</asp:PlaceHolder>
Code Behind
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
if (CMSContext.ViewMode == ViewModeEnum.LiveSite)
{
if(!Page.IsPostBack)
{
PopulateProductClass();
PopulateProduct();
PopulateDefaultViewOrders();
}
}
}
}
protected void lnkOrderByDate_Click(object sender, EventArgs e)
{
//Do Something
}
Any suggestions? I can't seem to figure it out?
Even the OnItemCommand="rprOrders_ItemCommand" wont fire either?
The LinkButton within your DataControl triggers the Method rprOrders_ItemCommand
Set a breakpoint there. If you have multiple LinkButton then you can read CommandName="OrderDate" Codebehind: (e.CommandName)
For passing values CommandArgument should be used.
use some thing like this
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
.cs
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("MyUpdate"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
For me the fix was to set EnableViewState="true" the the control tag of my ascx file.
E.G.
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="Settings.ascx.cs" Inherits="Foo.Bar.Settings" EnableViewState="true" %>
And my LinkButton looks like this
<asp:LinkButton ID="btnRemoveMedia"
runat="server"
class="icon mdi-delete"
OnCommand="btnRemoveMedia_OnCommand"
CommandArgument="<%# ((Tuple<int, string>) Container.DataItem).Item1 %>"
CommandName="Delete"
UseSubmitBehavior="false" />

Repeater shows wrong column content

I have a repeater to show an order confirmation. It is bound to a table generated with Entity Framework code first. The table contains the right information and the repeater shows all the information right except for the part where I show the totals which I paste below.
I believe that this part also works okay but quantity does not. For example if I add 5 products it will only show one, but if I put a break point and I run the code in debug mode I see that the value inserted in the Quantity table is 5 not 1, so the Quantity value on the table is inserted correctly, but the repeater reports 1.
Here is the code:
<asp:Repeater ID="rptConfirmOrder" runat="server">
<ItemTemplate>
<fieldset class = "OrderConfirmationFieldset"><legend class ="OrderDataLegend">Order Summary</legend>
<td align="left" width="60%" runat="server" id="Td25">
<asp:Label ID="lblQuantity" runat="server" Text="Quantity: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Quantity") %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td26">
<asp:Label ID="lblProductName" runat="server" Text="Product Name: " CssClass = "lblOrderConfirmation">
</asp:Label><%# Eval("ProductName" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td27">
<asp:Label ID="lblProductPrice" runat="server" Text="Product price: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("ProductPrice" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td28">
<asp:Label ID="lblSubtotal" runat="server" Text="Subtotal: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Subtotal" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td29">
<asp:Label ID="lblTotal" runat="server" Text="Total: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Total" ,"{0:c}" ) %>
<br />
</td>
</ItemTemplate>
</asp:Repeater>
Can Anyone help?
Thank you in advance!
Your repeater markup doesn't look right.
have it as follows:
<asp:Repeater ID="rptConfirmOrder" runat="server" OnItemDataBound="rptConfirmOrder_ItemDataBound">
<ItemTemplate>
// stuff
</ItemTemplate>
</asp:Repeater>
And if you have it that way, and this was a paste error, then forget the value in the database/entity etc.
You can verify accurately, what value is being bound by tapping into the row data bound event as follows..
protected void rptConfirmOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem as YOUR_ENTITY_TYPE;
Debug.Assert(5 == dataItem.Quantity);
}
}
I think you may need to add the < itemtemplate > tags in after the opening repeater and close it off before the closing repeater tag.

ASP.Net: Conditional Logic in a ListView's ItemTemplate

I want to show certain parts of an ItemTemplate based according to whether a bound field is null. Take for example the following code:
(Code such as LayoutTemplate have been removed for brevity)
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr>
<%
if (Eval("DownloadLink") != null)
{
%>
<td>
Link
</td>
<%
} %>
</tr>
</ItemTemplate>
</asp:ListView>
The above gives the following run-time error:
Databinding methods such as Eval(),
XPath(), and Bind() can only be used
in the context of a databound control.
So how can put some conditional logic (like the above) in an ItemTemplate ?
What about binding the "Visible" property of a control to your condition? Something like:
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr runat="server" Visible='<%# Eval("DownloadLink") != null %>'>
<td>
<a href='<%#Eval("DownloadLink") %>'>Link</a>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
To resolve "The server tag is not well formed." for the answers involving visibility, remove quotes from the Visible= parameter.
So it will become:
<tr runat="server" Visible=<%# Eval("DownloadLink") != null ? true : false %>>
I'm not recommending this as a good approach but you can work around this issue by capturing the current item in the OnItemDataBound event, storing it in a public property or field and then using that in your conditional logic.
For example:
<asp:ListView ID="MusicList" OnItemDataBound="Item_DataBound" runat="server">
<ItemTemplate>
<tr>
<%
if (CurrentItem.DownloadLink != null)
{
%>
<td>
Link
</td>
<%
} %>
</tr>
</ItemTemplate>
</asp:ListView>
And on the server side add the following code to your code behind file:
public MusicItem CurrentItem { get; private set;}
protected void Item_DataBound(object sender, RepeaterItemEventArgs e)
{
CurrentItem = (MusicItem) e.Item.DataItem;
}
Note that this trick will not work in an UpdatePanel control.
If you have 2 different structure that are to be rendered according to a condition then use panels
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr>
<asp:Panel ID="DownloadNull" runat="server" Visible="<%# Eval("DownloadLink") == null %>" >
<td> Album Description BlaBlaBla <img src="../images/test.gif"> </td>
</asp:Panel>
<asp:Panel ID="DownloadNotNull" runat="server" Visible="<%# Eval("DownloadLink") != null %>" >
<td> Album Description BlaBlaBla <img src="../images/test.gif">
<a href='<%# Eval("DownloadLink")' >Download</a>
.....
</td>
</asp:Panel>
</tr>
</ItemTemplate>
</asp:ListView>

Categories