I have List view – (
name: ListViewName
), with some labels inside it, the ID (name) of the label:
Biglbl
.. big because its include some links and titles.
there is one more label in name:
lblCatName
which return the category name.
I want to check each list view item – what is name (let’s say – “TEST”) and hide or display some labels or change HyperLink navigateUrl.
aspx:
<asp:ListView ID="ListViewName" OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
<ItemTemplate>
<asp:Label Visible="true" ID="lblCName" Text='<%# Eval("CatName") %>' runat="server" > </asp:Label>
<asp:Label runat="server" id="lblCName" Visible="false" />
<div style=".....">
...............
<asp:HyperLink ID="HyperLink4" runat="server"
NavigateUrl='<%# "bla.aspx?Id=" + Eval("id").ToString() %>'>
</asp:HyperLink>
<asp:HyperLink ID="HyperLink7" runat="server"
NavigateUrl='<%# "bla.aspx?Id=" + Eval("CatId").ToString() %>'> <img src="images/btblue.png" /> </asp:HyperLink>
</asp:Label>
</div>
</ItemTemplate>
</asp:ListView>
I have the C# code behind:
protected void ContactsListView_ItemDataBound(object sender,ListViewItemEventArgs e)
{
foreach (ListViewItem item in ListViewName.Items)
{
Biglbl = (Label)e.Item.FindControl("Biglbl");
Label lblCName = (Label)e.Item.FindControl("lblCatName");
HyperLink Link = (HyperLink)e.Item.FindControl("HyperLink4");
HyperLink Link2 = (HyperLink)e.Item.FindControl("HyperLink7");
if (lblCatName.ToString() == "TEST")
{
Biglbl.Visible = true;
Link.NavigateUrl = "blabla";
Link2.NavigateUrl = "blabla";
}
else
{
Biglbl.Visible = false;
Link.NavigateUrl = "aaaa";
Link2.NavigateUrl = "aaaa";
}
}
}
I don’t know how to use the foreache for my needs here.
What i need to do (i think the if and the findcontrol’s are fine.. i need to do what to change in the foreac.
EDIT: I see the question now shows you using
ItemDataBound. This will pass you eachListViewItemas it is bound so there is no need to do a for each loop.You can simply cast the sender to that type