Can I have asp:Repeater inside asp:Table? - c#

I want to create asp:Table where I can add rows using asp:Repeater like:
<asp:Table runat="server" ID="tblRepeater">
<asp:Repeater runat="server">
<ItemTemplate>
<asp:TableRow runat="server">
<asp:TableCell runat="server" Text=""></asp:TableCell>
<asp:TableCell runat="server" Text=""></asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:Repeater>
</asp:Table>
But when I try to do this It gives me error:
System.Web.UI.WebControls.TableRowCollection must have items of type 'System.Web.UI.WebControls.TableRow'.
'asp:Repeater' is of type 'System.Web.UI.WebControls.Repeater'.
Am I doing somethig wrong or it is impossible to use Repeater in Table?

I'm not sure if your example is slimmed down or represents your actual use case, but in this case you'd be better of using either a GridView which will generate a table structure based off of a given Data Source, or creating the <table> structure yourself and then placing a repeater inside it:
<table>
<asp:Repeater runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" /></td>
<td><asp:TextBox runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

Related

Display repeater data horizontally

In continuation to my last question, I was suggested to use Repeater for displaying data fetched from SQL Db in horizontal layout.
https://stackoverflow.com/questions/25447351/asp-net-c-sharp-customized-gridview/25458682#25458682
As per suggestion I implemented a repeater control. The data was displayed in horizontal layout but only second column fetched from the DB.
How to get the first column too:
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<asp:Literal ID="litRowStart" runat="server"></asp:Literal>
<td>
<asp:Label ID="lblExpID" runat="server" Text='<%#Eval("Exp_ID") %>' Font-Bold="true" />
</td>
<asp:Literal ID="litRowEnd" runat="server"></asp:Literal>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litRowStart1" runat="server"></asp:Literal>
<td>
<asp:Label ID="lblExpAmt" runat="server" Text='<%#Eval("Amt_Allocated") %>' Font-Bold="true" />
</td>
<asp:Literal ID="litRowEnd1" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
Create your repeater like this:
<table>
<tr>
<asp:Repeater ID="repTest" runat="server">
<ItemTemplate>
<td>
<div>
<b>
<%#Eval("NameofColumnYOuWantToShowAsHeader") %>
</b>
</div>
<div>
<%#Eval("YourColumnValue") %>
</div>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</table>
For more information about repeater:
Repeater Insert, Update, Delete in asp .net
Try this one using AlternateingItemTemplate:
<asp:Repeater ID="RepDetails" runat="server">
<ItemTemplate>
<td>
<asp:Literal ID="litRowStart" runat="server"></asp:Literal>
<asp:Label ID="lblExpID" runat="server" Text='<%#Eval("Exp_ID") %>' Font-Bold="true" />
<asp:Literal ID="litRowEnd" runat="server"></asp:Literal>
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td>
<asp:Literal ID="litRowStart1" runat="server"></asp:Literal>
<asp:Label ID="lblExpAmt" runat="server" Text='<%#Eval("Amt_Allocated") %>' Font-Bold="true" />
<asp:Literal ID="litRowEnd1" runat="server"></asp:Literal>
</td>
</AlternatingItemTemplate>
</asp:Repeater>
Assuming that you're getting 2 rows in your datasource.

Textbox inside Repeater not making UpdateProgress to trigger

I have following code in .aspx
<asp:UpdatePanel ID="upnGrid" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<table width="100%">
<tr>
<td align="right" width="25%">
<asp:Label ID="lbl1" Font-Bold="true" Font-Size="16px" Text=' <%# DataBinder.Eval(Container.DataItem,"Name") %>'
runat="server" />
</td>
<td>
<asp:HiddenField ID="hdn1" Value='<%# DataBinder.Eval(Container.DataItem, "Length") %>'
runat="server" />
</td>
<td>
<asp:TextBox ID="txt1" Font-Size="16px" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'
AutoPostBack="true" Width="100px" OnTextChanged="txt1_TextChanged" runat="server" onkeydown="javascript:return OntxtEnter(event);"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<%--<Triggers>
<asp:AsyncPostBackTrigger ControlID="rpt1"/>
</Triggers>--%>
</asp:UpdatePanel>
code for UpdateProgress is at the end of aspx -
<asp:Panel ID="UpdateProgressPanel" runat="server">
<center>
<asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="upnGrid"
DynamicLayout="true">
<ProgressTemplate>
<img src="../Images/loading.gif" alt="">
</ProgressTemplate>
</asp:UpdateProgress>
</center>
</asp:Panel>
<ajaxtoolkit:modalpopupextender id="ModalProgress" runat="server" targetcontrolid="UpdateProgressPanel"
popupcontrolid="UpdateProgress2" dropshadow="false" backgroundcssclass="modalBackground">
</ajaxtoolkit:modalpopupextender>
As suggested by some answers I made AsyncPostBackTrigger to be registered in code behind, like -
var control = e.Item.FindControl("txt1");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
I am not getting, why it is not working.
Please suggest.
Edit
I tried putting only required textbox in UpdatePanel, It is showing following error
'System.Web.UI.Control' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)
Try modifying your UpdateProgress control as shown below:
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server">
<asp:ImageButton ID="ibProgress" runat="server" ImageUrl="~/Images/progress.gif"
Visible="true" />Loading...
</asp:Panel>
</asp:Panel>
</ProgressTemplate>
</asp:UpdateProgress>
You cannot use it in such a way. Because inside the repeater the TextBox ID is different.You have to follow different mechanism.

Displaying search results in a Website

I am working on a website where i need to search for items from database and display it in page.I am using Datalist for displaying all the items .Now i need to display all the items according to some category.For example if i search for a two-wheeler it may have many categories or manufacturer's.Now I want to display it separately according to the categories.I can use many datalist to do that,but it will slow down the site.Instead which control can i use to do this.I searched the web but got no idea.So please give me sugessions to go about it.Any sugessions will be appreciated.
Code:
<asp:DataList ID="Dlitems" runat="server" RepeatDirection="Horizontal" RepeatColumns="4"
CellPadding="0" CellSpacing="15">
<ItemTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="ImgbtnProductImage" CssClass="imgdisp" ImageUrl='<%# Eval("ImagePath") %>'
CommandArgument='<%#Eval("ProductCode") %>' runat="server" CommandName="cmdView" />
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td width="20px"></td>
<td>
<img src="../Database/images/inr.jpg" alt="addtocart" />
<asp:Label ID="lblproductprice" runat="server" Text='<%# Eval("Price") %>' CssClass="lbl" />
</td>
<td>
<asp:ImageButton ID="imgbtnaddtocart" runat="server" Height="25px" Width="25px" ImageUrl="~/Database/images/addtocart.JPG"
ToolTip="Add to Cart" CommandArgument='<%#Eval("ProductCode")+","+ Eval("ProductName")+","+ Eval("ImagePath")+","+ Eval("Price")+","+ Eval("LongDescription") %>'
CommandName="addtocart" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
You can take benefit of gridview.
Bind you gridview data.
And letter merge the rows.
For an example you can use the below link
http://www.codeproject.com/Articles/34337/How-to-merge-cells-with-equal-values-in-a-GridView
Edit 1
Some links
How to display group data separately using DataList in ASP.NET?
http://aspnettuts.wordpress.com/2010/10/21/asp-net-repeater-group-recordsdata-delete-selected-rows/
You should go for a Repeater control.
Example on Repeater
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:ImageButton ID="ImgbtnProductImage" CssClass="imgdisp" ImageUrl='<%# Eval("ImagePathForTwoWheeler")%> + <%# Eval("ImagePathForCategory") %>'
CommandArgument='<%#Eval("ProductCode") %>' runat="server" CommandName="cmdView" />
</ItemTemplate>
</asp:Repeater>

How to add/read items in ASP.NET

I have a set textboxes where the user enters something and I use them when they click Submit. I need to add a button that lets them add another identical set of textboxes on the fly, enter data into those and then when the submit button is pressed I need to grab the value from both sets. The amount of added sets of items can be unlimited.
What's the best way to handle something like this in ASP.NET/C#?
I've looked around and have basically only gotten that you can try to use JQuery on the front end which leaves the backend a mess or you can try to use a GridView and rebind it every time with new items which can get messy.
Any better/easier way to do this?
Thanks for the help.
Use a Repeater control for the first set of text boxes, and have it repeat for each extra listing.
Here's my comment control repeater. It's a little arcane, as it was the first control I ever made in asp.net, but it should get you going.
<asp:Repeater runat="server" ID="repeater"
onitemdatabound="repeater_ItemDataBound"
>
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<ItemTemplate>
<tr id="trContents" runat="server"><td>
<table id="tableComment" runat="server"
width="100%" style="border-color:Black; border-width:1px; border-style:solid;background-color:#EEEE99">
<tr><td id="tdHeader" runat="server"
style="border-color:Black; border-width:1px; border-style:solid;padding: 5px">
<table width="100%" id="tableHeader" runat="server">
<tr>
<td>
<asp:Label ID="headerText" runat="server"
Text="<%# GetHeader((CommentRecord)Container.DataItem) %>" />
</td>
<td style="text-align:right"> <%-- OnClientClick="aspnetForm.target ='_blank';" --%>
<asp:Button ID="btnEdit" runat="server" Text="Edit"
PostBackUrl="<%$ AppSettings:TextEditor %>"
/>
<asp:Button ID="btnReply" runat="server" Text="Reply"
PostBackUrl="<%$ AppSettings:TextEditor %>"
/>
</td>
</tr>
</table>
</td></tr>
<tr><td style="border-color:Black; border-width:1px; border-style:solid;background-color:#FFFFF0;padding: 10px">
<%# ((CommentRecord)Container.DataItem).Text %>
</td></tr>
<tr><td>
<table width="100%"><tr>
<td style="text-align:left">
<asp:Button ID="btnDelete" runat="server" Text="Delete"
CausesValidation="False" />
</td>
<td style="text-align:right">
<asp:Label ID="footer" runat="server" Text="<%# GetFooter((CommentRecord)Container.DataItem) %>" />
</td>
</tr></table>
</td></tr>
</table>
<asp:PlaceHolder ID="placeHolder" runat="server" />
</td></tr>
</ItemTemplate>
</asp:Repeater>
What I usually do in these cases is have Javascript that handles the client-side dynamic creation of additional textboxes, then before I submit the values, have all the data collected into a JSON object and save the JSON string into a <asp:HiddenField> control that ASP.NET can read and parse. This isn't a perfectly clean approach, but it is the best thing I have found so far.
I would create the textboxes using jQuery (assigning them a unique id) and then get the values on the server from the Request.Form key/value collection. This would be a rather simple solution.
I would recommend going the GridView way, and bind it to a DataSource object, e.g. ObjectDataSource. Clicking the add button should add an empty row to the data source, and this empty row will automatically be drawn by the GridView. When everything works you can wrap in an UpdatePanel to achieve smooth refresh when adding rows.

Listview with the same data over several columns

I have a list that contains names.
I would like to create a listview that will display these name, three to a row.
I am unsure how to accomplish this.
You should use a GroupTemplate. Here's an example from 4GuysFromRolla.com
<asp:ListView ID="ProductList1" runat="server"
DataSourceID="ProductDataSource"
GroupItemCount="3" ItemPlaceholderID="itemsGoHere"
GroupPlaceholderID="groupsGoHere">
<LayoutTemplate>
<p>
<asp:PlaceHolder runat="server" ID="groupsGoHere"></asp:PlaceHolder>
</p>
</LayoutTemplate>
<GroupTemplate>
<ol>
<asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
</ol>
</GroupTemplate>
<ItemTemplate>
<li><%#Eval("ProductName")%></li>
</ItemTemplate>
</asp:ListView>

Categories