change li class in codebehind - c#

Is there any way to add/remove a class to an HTML li tag from C# codebehind? for example, here's my code.
<li>
<asp:Panel ID="pnlClipboardEbulletin" runat="server">
<asp:Label ID="lblClipboardEbulletin" runat="server" />
</asp:Panel>
</li>
<li class="noborder">
<asp:Panel ID="pnlClipboard" runat="server">
<img src="../Images/Clipboard.jpg" alt="Clipboard" />
<asp:HyperLink runat="server" ID="lnkClipboard" Text="Clipboard" NavigateUrl="~/displayMyList.aspx"></asp:HyperLink>
</asp:Panel>
</li>
in the codebehind, I have to toggle the visibility of pnlClipboard based on other data. If pnlClipboard = false, I want to set the class of the first li to class="noborder"

Give your li tag the runat="server" attribute and an id.
<li runat="server" id="myli">
Now in your code behind you can use it like any control
myli.Attributes["class"] = "myClass";

Add runat="server" and ID attribute to first and you will be able to access it from codebehind
markup:
<li runat="server" id="firstItem">
<asp:Panel ID="pnlClipboardEbulletin" runat="server">
codebehind:
firstItem.Attributes["class"] = "noborder";

Related

Need to add 'for' attibutes to labels inside a ListView control of an ASP.NET page

I have something like this
<asp:ListView ID="lvOptions" runat="server"">
<LayoutTemplate>
<p>Select an option<span>*</span></p>
<div class="row">
<asp:Literal runat="server" ID="groupPlaceHolder"></asp:Literal>
</div>
<div class="row">
<div class="w100">
<label>
Other types:</label>
<asp:DropDownList ID="ddlOtherTypes" runat="server" DataTextField="Description" DataValueField="Id"
AutoPostBack="true" Enabled="false"
</asp:DropDownList>
</div>
</div>
</LayoutTemplate>
<GroupTemplate>
<asp:Literal runat="server" ID="itemPlaceHolder"></asp:Literal>
</GroupTemplate>
<ItemTemplate>
<span class="wrap-w50">
<asp:RadioButton runat="server" ID="rdbOption" AutoPostBack="true" />
<label>
<%# Eval("Description") %></label>
</span>
</ItemTemplate>
</asp:ListView>
And I got here two labels, one fot the Other types DropDownList, and another for each RadioButton.
For this labels, I need to add a "for" attribute, and I need the clientID but, if I try to write the attibute like:
for="<%= ddlOtherTypes.ClientID %>"
for="<%= rdbOption.ClientID %>"
I got the error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Any suggestions please?
Thank you in advance.

How to AsyncPostBackTrigger for the LinkButton in the Repeater

In my page, I have 2 repeaters.In First repeater i have a LinkButton.On Item Command of first repeater link button i am populating second repeater.I am trying to avoid postback on each click but the UpdatePanel can not avoid post back.
Here is code.aspx
<asp:ScriptManager runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater ID="rptIndvidualButtons" runat="server" onitemcommand="rptIndvidualButtons_ItemCommand">
<ItemTemplate>
<ul>
<li class="button">
<asp:LinkButton ID="lnkBtnIndvidual" runat="server" Text='<%#Eval("PackageType")%>'></asp:LinkButton>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
</div>
<div style="clear:both">
<asp:Repeater ID="rptIndvidual" runat="server">
<ItemTemplate>
<div runat="server" id="divBronzeIndvidual">
<ul class="pricing-table" id="ulBronze">
<li class="title">
<div class="item-title">
<asp:Label ID="lblIndvPkgName" runat="server" Text='<%#Eval("PackageName")%>'></asp:Label>
</div>
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rptIndvidualButtons" />
</Triggers>
</asp:UpdatePanel>
From above code can any one help me??
try to call RegisterAsyncPostBackControl for the linkbuttons in rptIndvidualButtons.itemDataBound event
var lnkBtnIndvidual = (LinkButton)e.Item.FindControl("lnkBtnIndvidual")'
ScriptManager1.RegisterAsyncPostBackControl(lnkBtnIndvidual);

ASP.NET outputting <br> tags?

I'm working on a website built in .net and its outputting linbreaks after each span for some reason. The code thats outputting the line breaks is
<asp:DataList ID="DL_Items" runat="server" DataKeyField="ProductID" oneditcommand="DL_Items_EditCommand"
oncancelcommand="DL_Items_CancelCommand"
onupdatecommand="DL_Items_UpdateCommand"
onitemcommand="DL_Items_ItemCommand"
onitemdatabound="DL_Items_ItemDataBound"
ondeletecommand="DL_Items_DeleteCommand" RepeatLayout="Flow">
<ItemTemplate>
I've tried looking around google for better solutions to this and all I could find was a way to generate the code in a table which i do not want. Is there a reason why the above code is generating line breaks, <br>
Compete itemtemplate
<ItemTemplate>
<div class="EC_Item_Left_Panel">
<ul class="EC_Item_Left_UL">
<li>
<asp:Image ID="IM_PIC" runat="server" ImageUrl='<%# Eval("path") %>' /></li>
<li>
<asp:LinkButton ID="BT_Edit" CssClass="ecom_edit_tbn" CommandName="Edit" runat="server">Edit Item</asp:LinkButton></li>
<li class="deleteRow">
<asp:LinkButton ID="BT_Delete" ToolTip='<%# Eval("ProductName") %>' runat="server" CssClass="deleteButton ecom_remove_tbn" Text="Delete" ForeColor="Red"></asp:LinkButton>
<asp:Button ID="deleteCommand" runat="server" CausesValidation="false" CommandName="Delete" CssClass="deleteCommand" style="display:none" />
</li>
<%--<asp:LinkButton CssClass="ecom_remove_tbn" ID="BT_Remove" CommandName="Delete" runat="server">Remove</asp:LinkButton>--%>
<li><asp:LinkButton CssClass="ecom_link_tbn" ID="BT_Link" runat="server" CommandName="linkproduct">Linked Items</asp:LinkButton></li>
</ul>
</div>
<div>
<div class="Item_Right_Panel">
<ul class="EC_Item_Top_UL">
<li><label>ProductID </label><span><%#Eval("ProductID")%></span></li>
<li><label>Enabled </label><span><%#Eval("Enabled")%></span></li>
<li><label>Title </label><asp:Label ID="LB_ProductTitle" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label></li>
<li><label>Product Code </label><span><%#Eval("ProductCode")%></span></li>
<li><label>Category </label><span><%#Eval("Category")%></span></li>
<%--Callum--%>
<li><label>Price</label><span><%#Eval("Sale_Price")%></span></li>
<li><label>Subcategory</label><span><%#Eval("Subcat")%></span></li>
<li><label>Designer</label><span><%#Eval("DesignerName")%></span></li>
<li><label>Range</label><span><%#Eval("Range")%></span></li>
<li><label>Height</label></li>
<li><label>Length</label></li>
<li><label>Width</label></li>
<li><label>Description </label><span><%#Eval("Description")%></span></li>
</ul>
<ul class="EC_Item_Bottom_UL">
<li><label>KeyFieldName </label><span><%#Eval("KeyFieldName")%></span></li>
<li><label>ItemField1Name </label><span><%#Eval("ItemField1Name")%></span></li>
<li><label>ItemField2Name </label><span><%#Eval("ItemField2Name")%></span></li>
<li><label>ItemField3Name </label><span><%#Eval("ItemField3Name")%></span></li>
<li><label>ItemField3Name </label><span><%#Eval("ItemField3Name")%></span></li>
<li><label>Include File </label><span> <%# IncludeTitle(DataBinder.Eval(Container.DataItem, "include"))%> </span></li>
</ul>
</div>
</div>
</ItemTemplate>
The DataList creates the br's(See documentation of Flow layout at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeatlayout%28v=VS.100%29.aspx).
If you don't want br's or any extra markup between elements, I'd suggest you to use a Repeater instead.

replacing strings

here i need to replace content "says..." :
<asp:DataList ID="dlProductReviews" runat="server" RepeatDirection="Vertical" RepeatColumns="1">
<ItemTemplate>
<div>
<span class="content">says... </span>
</div>
</ItemTemplate>
</asp:DataList>
You can use the data binding tags here:
<asp:DataList ID="dlProductReviews" runat="server" RepeatDirection="Vertical" RepeatColumns="1">
<ItemTemplate>
<div>
<span class="content"><%# GetContent() %></span>
</div>
</ItemTemplate>
</asp:DataList>
Then in your code behind create a protected method called GetContent()(or you could use a property) in the code behind that returns a string, in your case a string from the resources. You could also place the code directly in the <%#...%> tags.

Can I Number a GroupTemplate or ItemTemplate?

I would like to use a GroupTemplate to separate a list of items into groups. However, I need each Group to be numbered sequentially so I can link to them and implement some JS paging. I'm binding to an IEnumerable
Here's some pseudo code. I would like the output to look like this:
<a href="#group1">Go to Group 1<a>
<a href="#group2">Go to Group 2<a>
<a href="#group3">Go to Group 3<a>
<ul id="group1">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group2">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group3">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
Is this easy to do in a ListView, using GroupTemplate and ItemTemplate?
<asp:ListView ID="lv" runat="server" GroupPlaceholderID="groupPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<GroupTemplate>
<ul id="<!-- group-n goes here -->">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</GroupTemplate>
<ItemTemplate>
<li>Item</li>
</ItemTemplate>
</asp:ListView>
I can get the number of groups to do the links at the top from the Datasource and basic math, but how do I get id="groupN" number into the template?
I don't think you can DataBind the id, so I'd probably either use a hidden field, have JQuery count them up, or use the CssClass. You can use Container.DataItemIndex to get your number.
Edit: By just changing the ul to be runat="server", ASP.NET will generate a unique id for you in it's infamous INamingContainer format. That will include an index as well, though it'll be something like lv_ctrl0_group, and is an implementation detail.
You could hook a handler to the ul's Init event and append a number to it, making it something like lv_ctrl0_group1. I don't think you can get rid of the prepended INamingContainer stuff very easily, and this would probably break any IPostDataHandler controls.
<script runat="server">
void Group_Init(object sender, EventArgs e) {
((Control)sender).ID += groupId++.ToString();
}
int groupId = 0;
</script>
<asp:ListView id="lv" runat="server" GroupItemCount="3">
<LayoutTemplate>
<asp:PlaceHolder ID="groupPlaceHolder" runat="server" />
</LayoutTemplate>
<GroupTemplate>
<ul id="group" runat="server" oninit="Group_Init">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"/>
</ul>
</GroupTemplate>
<ItemTemplate>
<li>Item</li>
</ItemTemplate>
</asp:ListView>
In your aspx file:
<GroupTemplate>
<ul id='<%# "group"+GroupNumber %>'>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</GroupTemplate>
In your code behind (assuming C#):
int _GroupNumber=0;
protected string GroupNumber
{
get { return (++_GroupNumber).ToString(); }
}
The solution from Keltex above would work with a small change; use <%= instead <%#,
<%# wouldnt work because GroupTemplate doesnt support databinding

Categories

Resources