How to prevent ItemTemplate and AlternatingItemTemplate from creating newlines - asp.net

In aspx, using either a ListView, Repeater, or any of the other standard controls that are listed under the visual studio 2010 data toolbox: My data source is simply a set of records made up of ID, title, first_name, last_name, degree. I am trying to print them on a single line (going to a newline when the end of a line is reached, not necessarily by a new record), however, the ItemTemplate and AlternatingItemTemplate seems to print each record as a newline. Is there a way to get rid of that automatic newline? (I tried ItemSeparatorTemplate, but that didn't work)
I don't think that it is necessary, but I figured that I'll just give you the code:
<asp:ListView ID="ListView2" runat="server" DataSourceID="AccessDataSource2">
<ItemTemplate>
<a href='#<%# Eval("ID") %>' /><%# Eval("title")%> <%# Eval("first_name")%> <%# Eval("last_name")%> <%# Eval("degree")%></a>
</ItemTemplate>
<AlternatingItemTemplate>
<a href='#<%# Eval("ID") %>' /><%# Eval("title")%> <%# Eval("first_name")%> <%# Eval("last_name")%> <%# Eval("degree")%></a>
</AlternatingItemTemplate>
</asp:ListView>

Use Datalaist and make RepeatDirection="Horizontal".
<asp:DataList ID="datalist1" runat="server" RepeatDirection="Horizontal"></asp:DataList>

Related

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>

How to make this data control reusable for different content and data?

I'm using ASP.NET Web Forms, and I've got a Repeater control to display some data. Currently it looks like this:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<strong>Details:</strong> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<strong>Status:</strong> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
This list has hover behaviour, etc. and I'd like to reuse the same style of list across my project, just with different data (really just replacing the three lines with <strong> tags in). However, I obviously don't want to copypaste and change it every time it's used, so I thought I'd try and make it into a User Control:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<%= DataString %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And in my corresponding class in CodeBehind:
public string DataString { get; set; }
So my main aim was to do this:
<uc:MyUserControl runat="server" ID="blah" DataString="<strong>Date/Time:</strong><%# DataBinder.Eval(Container.DataItem, "date") %> ETC..." />
But it seemed that ASP.NET wanted to evaluate the <% %> in the string there and then, and I tried different ways to get around it such as this:
<% string theDataString = "<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %>"; %>
<uc:MyUserControl runat="server" ID="blah" DataString='<%= theDataString %>' />
But I ran into trouble again with the closing %> in theDataString, and even after replacing it with something like {0: %} (to escape the % sign: can't remember the exact syntax), which did escape it correctly, I discovered that you can't actually use the <%= %> inside an attribute, which was annoying.
My last-ditch attempt was to set DataString in my page's CodeBehind, and the HTML <strong> rendered correctly, but ASP.NET didn't evaluate the <%# %> and just spat it out literally. So I'm kind of stuck as I can't think of anything else... (by the way I'm still relatively new to ASP.NET)
I'm not even sure this is a good way to go about it - previously I thought about making it into a templated user control but this method seemed simpler and it was something I already knew how to do, or so I thought. So any help with this would be greatly appreciated, thanks!
EDIT: Just to be clear, my emphasis here is on the re-usability of this particular control - so I will be able to display different types of data records in this style of list (clickable, highlight behaviour etc).
If your goal is to have three lines appear as bold instead of normal text, then your best bet is to use something more geared towards website-display than something programmatic and heavy-handed like a dynamic server-side user control. For example:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<span class="displayMode">Date/Time:</span> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<span class="displayMode">Details:</span> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<span class="displayMode">Status:</span> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And then, programmtically show:
<style type="text/css">
.displayMode { font-weight: bold; }
/* and/or */
.displayMode { text-decoration: underline; }
</style>
Use the right tool for the job, in this case, I don't feel as though server-side code is the right way to accomplish this task. If, on the other hand, you need to change the content of the insides of your <li> tags, then yes you would have to do it in C#. However, if that were the case you could easily bind it to a different object or use a different control or inherit the control or any number of other methods. For your specific case, just use standard HTML manipulation techniques.

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.

How to get parent datasource Eval() inside a nested Repeater? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Accessing parent data in nested repeater, in the HeaderTemplate
I have a nested repeater being databound... let's say the top level repeater is an OrderRow and the inner repeaters are bound to LineItem rows from my DB. ASPX is something like this:
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%# Eval("OrderID") %>:<br/>
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%# ???? %> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
Now the button on the inner footer will be used to add a new line item... but the command argument needs to be the OrderID from the outer repeater, so we know which order to add to. Obviously a regular Eval() call won't work here, because it will have the inner repeater's DataRowView as a source [actually, it won't, since it's in the footer]. How do I get this value? Am I going to have to set this dynamically in the ItemDataBound event of the outside repeater?
Since the call are serial you can use the code behind to save the last order id and use it later.
Here is the idea.
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%#GetOrderID(Container.DataItem)%><br />
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%=cLastOrderID%> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
and on call behind
public int cLastOrderID = -1;
protected string GetOrderID(object oItem)
{
cLastOrderID = (int)DataBinder.Eval(oItem, "OrderID");
return cLastOrderID.ToString();
}
What I do here is that I call the GetOrderID to return the OrderID and I save it in a global value, and then later in the footer I use this global value. Hope this helps you.

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)

Resources