How do I Limit asp:Repeater in asp.NET? - c#

I'm a beginner with ASP.net and now, I want to limit the item in my asp:repeater (It's a RSS feed)
This is my code :
<asp:Repeater ID="rssRepeater" runat="server">
<ItemTemplate>
<table style="border: solid 1px black; width: 500px; font-family: Arial">
<tr>
<td style="font-weight: bold">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("link")%>' Text='<%#Eval("title")%>'></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
<tr>
<td style="background-color: #C2D69B">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("description")%>'></asp:Label>
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
EDIT :
Code behind
try
{
WebResponse rep = rssReq.GetResponse();
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
DataSet ds = new DataSet();
ds.ReadXml(xtr);
rssRepeater.DataSource = ds.Tables[2];
rssRepeater.DataBind();
}
catch (Exception ex)
{
throw ex;
}
have you got a property to limit the display of items ?
Thank you for your help !

There is no properties in the repeater that controls the number of rows but you can control it from the DataSource or the SQL query that binds this repeater

Related

Accessing Label Controls from inside the Repeater

I am fairly new to ASP.NET and I am trying to add the number i created in the backend to be inserted into
the label text field in the front-end but Im not being able to access the Label (lblAmountNumber) inside the Repeater i guess it is not allowing me to access the ID=lblAmountNumber
and the name of my repeater is RpMember
I have my simple code down below
FRONT END
<asp:Repeater ID="RpMember" runat="server">
<HeaderTemplate>
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th style="min-width: 100px">
<asp:Label ID="Label2" runat="server" Text="MemberInfo"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label3" runat="server" Text="Amount Number"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label5" runat="server" Text="Loan Balance"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label1" runat="server" Text="Savings Balance"></asp:Label></th>
<th style="min-width: 100px" class="ButtonView">
<asp:Label ID="Label6" runat="server"></asp:Label></th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="text-align: center">
<td style="width: 100px; text-align: left; text-transform: capitalize"><%#Eval("Name") %>
<br />
<%#Eval("Phone") %>
<br />
<%#Eval("Address") %>
</td>
<td style="width: 100px"><asp:Label ID="lblAmountNumber" runat="server"></asp:Label></th></td>
<td style="width: 100px"><%#Eval("LoanBalance") %></td>
<td style="width: 100px"><%#Eval("SavingsBalance") %></td>
<td style="text-align: center; width: 100px" class="ButtonView">
<asp:LinkButton ID="ReportLinkBtn" runat="server" OnClick="ReportLinkBtn_Click" CommandArgument='<%# Eval("MemberId")%>' CssClass="btn btn-success" Text="Report Details" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
BACKEND
DataTable dt2 = objdalTransactionEntry.GetLoanTakerStartAmountByLoanTakerId(Sessions.Name.UserId, MemberId);
foreach (RepeaterItem item in RpMember.Items)
{
Label lab = item.FindControl("lblHishabNumber") as Label;
string yearForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string yearForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string dayForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("dd");
lab.Text = "Amount Number " + monthForHishabNumber + yearForHishabNumber.Substring(yearForHishabNumber.Length - 2) + dt2.Rows[0]["UserWiseID"].ToString();
}

unable to access table back color property

For some reason in the C# code behind for asp.net, i cannot call the table by it's ID to set it's back color property. I tried and and nothing seems to work
Here is my asp.net table tag:
<table id="ptbl" runat="server" cellpadding="2" width="640px" border="1">
here is something similar to what i what to do in the C# code behind but it does not recognize the id
ptbl.Attributes.Add("style", "background-color:red")");
Any ideas/suggestions?
Update: here is the code. There is a layout template in it so somehow it can't see the table id, but if I take that out of it then it sees it. What can i do. I need the listview to get the data
<asp:ListView ID="ListView1" runat="server" Style="color: white; font-weight: bold">
<LayoutTemplate>
<table id="ptbl" runat="server" cellpadding="2" width="640px" border="1" style="color: black; font-weight: bold">
<tr runat="server">
<th runat="server">Ps</th>
<th runat="server">P</th>
<th runat="server">T</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="90">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="Label1" runat="server"><%# Eval("Ps") %></asp:Label></td>
<td>
<asp:Label ID="Label2" runat="server"><%# Eval("P") %></asp:Label></td>
<td>
<asp:Label ID="Label3" runat="server"><%# Eval("T") %></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
Again the layout template is causing the issue. how can i use the listview and the table with the layout template. I know this has morphed a bit now , but any help would be appreciated.
You cannot access the ptbl table until DataBind() has been called on ListView1. After it has been called and there is data in the dataset, then you can reference the the ptbl table like this:
protected void Page_Load(object sender, EventArgs e)
{
var tbl = new DataTable();
tbl.Columns.Add("Ps", typeof(Int32));
tbl.Columns.Add("P", typeof(string));
tbl.Columns.Add("T", typeof(string));
var r = tbl.NewRow();
r[0] = 99;
r[1] = "Hey";
r[2] = "USA";
tbl.Rows.Add(r);
ListView1.DataSource = tbl;
ListView1.DataBind();
var ptbl = (HtmlTable)ListView1.FindControl("ptbl");
ptbl.Style.Add("background-color", "red");
}

C# Repeater displaying single listing 12 times

I have a repeater I'm using to display a sidebar nav type system on my page. It loads some data from a db for the display of the links. It should be showing a single listing in this case, however it shows the same listing a total of 13 times.
My code:
private void Load_Locations() //Loads a list of locations.
{
var query = from q in CurrentContext.LOC
join w in CurrentContext.int on q.key equals w.L_key
select new
{
Location = q.Location
};
rpt_locations.DataSource = query;
rpt_locations.DataBind();
}
^to pull the data from the database, works perfectly as I use the same snippet for other things.
<div class="Side_Menu" style="margin-left: auto; margin-right: auto; text-align: center">
<asp:Repeater ID="rpt_locations" runat="server" OnItemCommand="rpt_locations_ItemCommand">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="btn_location" runat="server" Text='<%#Eval("Location") %>' Font-Underline="False" OnClick="btn_location_Click"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td>
<asp:LinkButton ID="btn_location" runat="server" Text='<%#Eval("Location") %>' Font-Underline="False" OnClick="btn_location_Click"></asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
^Asp coding for the repeater.
Havn't expericned this issue before, pretty lost on how it's happening.

How can I add methods to listview items?

below is my listview code example, while the products are populating, I want to check every single one of them against my database whether it's quantity is 0, how can add a method in code behind to do this:
I tried doing this under the onselectedindexchanged event but got the 'Object reference not set to an instance of an object.' exception :
Appreciate any help given. Thanks!
using (XXXShop db = new XXXShop ())
{
var availability = from p in db.Products
where p.Quantity == 0
select p;
(ListView_Products.FindControl("litStatus") as Literal).Text = "Unavailable";
}
ListView code:
<asp:ListView ID="ListView_Products" runat="server" DataKeyNames="ProductID"
DataSourceID="EDS_ProductsByCategory" GroupItemCount="3"
onselectedindexchanged="Page_Load">
<EmptyDataTemplate><table runat="server"><tr><td>No data was returned.</td></tr></table></EmptyDataTemplate>
<EmptyItemTemplate><td runat="server" /></EmptyItemTemplate>
<GroupTemplate><tr ID="itemPlaceholderContainer" runat="server"><td ID="itemPlaceholder" runat="server"></td></tr></GroupTemplate>
<ItemTemplate>
<td runat="server">
<table border="0" width="295">
<%-- Product pics --%>
<tr>
<td style="vertical-align: middle; text-align: center;">
<a href='ProductDetails.aspx?productID=<%# Eval("ProductID") %>'>
<image src='Catalog/Images/Thumbs/<%# Eval("ProductImage") %>' width="195" height="270" border="0">
</a>
</td>
</tr>
<tr>
<%-- Product info, description, price...etc --%>
<td style="width: 250px; vertical-align: middle; text-align: center; padding-bottom: 5px;">
<b>Price: </b><%# Eval("UnitCost", "{0:c}")%>
</td>
</tr>
<tr>
<td style="width: 250px; vertical-align: middle; text-align: center; padding-bottom: 25px;">
<asp:Literal ID="litStatus" runat="server" />
</td>
</tr>
</table>
</td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server">
<tr ID="groupPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr runat="server"><td runat="server"></td></tr>
</table>
</LayoutTemplate>
</asp:ListView>
I haven't done that much with ASP.NET or used the ListView so i don't know if there might be better ways to do this, but you could add a method to your codebehind that can format your data the way you want. Like the following:
protected string FormattedQuantity(int quantity)
{
return quantity > 0 ? quantity.ToString() : "Unavailable";
}
You should then be able to use this formatter anywhere in your aspx file by doing the following:
<asp:Label ID="OccurrenceLabel" runat="server"
Text='<%# FormattedQuantity((int)Eval("Quantity")) %>' />
Check it in the ListView.ItemDataBound event

Export to Excel from c# .net

Exporting from c#.net
I am getting a problem I have a form that when I export to excel as a result in excel Any ideas why is this happening I am including the ASP code below.
<%# Page Language="C#" MasterPageFile="~/masterpages/Admin.master" AutoEventWireup="true" CodeFile="members-search-adv.aspx.cs" Inherits="masteradmin_members_search_adv" Title="LISA - Life Insurance Settlement" %>
<%# Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="sorttable.js"></script>
Go to Quick Search
<h1>Contact Database - Advanced Search</h1>
<asp:Label ID="lblSearchCriteria" runat="server" Text="" ForeColor="blue"></asp:Label>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="50%">
<table border="0" cellpadding="3" cellspacing="1" style="height: 214px; width: 101%;">
<tr>
<td class="form1" align="right"><strong>OpenSearch:</strong></td>
<td class="form2">
<asp:TextBox ID="txtSearch" runat="server" CssClass="fields" Width="245px" ></asp:TextBox><b>*</b> <br><b>*</b><small>Search By Company Name, First Name, Last Name, Tags, Comments</small></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Industry Segment:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlIndSegment" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Member Type:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlMemberType" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Member Rep only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxMemberRep" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Board Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxBoardOnly" runat="server" /></td>
</tr>
<tr>
<td colspan="2" align="right">
<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="/RadControls/images/bu_search.jpg" />
</td>
</tr>
</table>
</td>
<td> </td>
<td valign="top" width="50%">
<%--split the table here--%>
<table border="0" cellpadding="3" cellspacing="1"
style="width: 98%; margin-left: 0px;">
<tr>
<td class="form1" align="right"><strong>Suspended Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxSuspended" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Active Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxActiveOnly" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Allow Other Members To See My Info Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxAllowOtherMembers" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>State:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlState" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>By Special Level:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlSpecialLevel" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Sort by:</strong></td>
<td class="form2">
<asp:DropDownList ID="ddlSortBy" runat="server" CssClass="fields">
<asp:ListItem Value="CompanyName">Company</asp:ListItem>
<asp:ListItem Value="FirstName">First Name</asp:ListItem>
<asp:ListItem Value="LastName">Last Name</asp:ListItem>
<asp:ListItem Value="MemberCategoryId">Membership Type</asp:ListItem>
<asp:ListItem Value="IndustrySegmentId">Industry Segment</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td><asp:Literal ID="litTotalCount" runat="server" /></td><td align=left class="form3">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export To Excel</asp:LinkButton>
<small>* Click on the column to SORT</small></td></tr>
</table>
<asp:Repeater ID="rptList" runat="server">
<HeaderTemplate>
<table class="sortable" border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td align="left" valign="top" class="form1"><b>Company</b></td>
<td align="left" valign="top" class="form1"><b>First Name</b></td>
<td align="left" valign="top" class="form1"><b>Last Name</b></td>
<td align="left" valign="top" class="form1"><b>Email</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlCompany" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlFirstName" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlLastName" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlEmail" runat="server" CssClass="link"></asp:HyperLink> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"><br />
<UC:Paging Id="ctlPaging" runat="server" />
</asp:PlaceHolder>
<%-- <div align="center">
<br /><UC:Paging Id="ctlPaging" runat="server" />
</div>--%>
</asp:Content>
Sorry but this is a better explanation of the issue
Exporting from c#.net I am getting a problem I have a form that when I export to excel I ge the following <div></div> I have data in the datagrid but not in excel I am just getting <div></div>
And this is the function that I am using
void ExportToExcel3()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
I am populating the datagrid prior to the export. This is the line of code. It's funny I create a new webform just with this section of code and the export worked.
Do you think there's a problem with the datarepeater that I have in the page that is causing the conflict.
MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
this.GridView2.DataSource = list;
this.GridView2.DataBind();
That's not the sequence at all- it's just not how asp.net and the web work.
You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.
Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.
I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.

Categories

Resources