How can I add methods to listview items? - c#

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

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();
}

How can I set a width limit on a table column?

I have a table containing data and in the first field it has translatable labels.
I would like to be able to set a width in the label column but make it so that if someone translates the labels and the text is longer, that is expands the column but up to a given point.
Example label column requirements:
Width: 200px;
Expandable: true;
Max Expanding: 300px;
Note: I am specifically asking how to enable this functionality but it must have a maximum width when expanding.
<table id="tblCustTypes" class="tblTop">
<tr>
<td class="auto-style1">
<asp:Label ID="lblCustType" runat="server" Text="Cust Type"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtCustomerType" runat="server" Width="20%" class="autosuggest" CssClass="autosuggest" OnChange="onSave();" OnTextChanged="txtCustomerType_TextChanged" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblTempCustType" runat="server" Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="lblDescription" runat="server" Text="Description"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDescription" runat="server" Width="35%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="lblApplyVAT" runat="server" Text="Apply VAT"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="rblApplyVAT" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected="True" Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="lblProduceInvoices" runat="server" Text="Produce Invoices"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="rblProduceInvoices" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected="True" Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="lblPurchaseSale" runat="server" Text="Purchase/Sale"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="rblPurchaseSale" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="P">Purchase</asp:ListItem>
<asp:ListItem Selected="True" Value="S">Sale</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="lblTerms" runat="server" Text="Terms (Days)"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTerms" runat="server" Width="5%"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Label ID="lblLastUpdated" runat="server" Text="Last Updated"></asp:Label>
</td>
<td>
<asp:Label ID="lblLastUpdatedVal" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
The comments provided the solution. What browser are you using? The min-width property isn't compatible with some older browsers, including IE 8.
I found that putting this in my .aspx file (or relevant content control) did the trick.
<style>
.tblTop > tbody > tr > td {
min-width:50px;
max-width: 100px;
border: 1px solid black;
}
</style>
<table class="tblTop">
<tr>
<td>Hello</td>
<td>Goodbye</td>
</tr>
<tr>
<td>Some very long string that will extend to the maximum width that I set and force the words to wrap around.</td>
<td>Goodbye</td>
</tr>
</table>
If you're using the server-side control (not sure why you would here):
<asp:Table CssClass="tblTop"> . . .
I used the selectors (>) so that the properties wouldn't apply to the radio-button list control and other ASP controls that might create a table within a table cell.
Edit: After re-reading your question, I see that you only wanted the first column to change. In that case, using the :first-child property is the way to go, but you will still want selectors so that your radio-button lists don't set their columns with that style. The code would be as follows:
<style>
.tblTop > tbody > tr:first-child {
min-width:50px;
max-width: 100px;
border: 1px solid black;
}
</style>
Usually to force a width within a table , you use table-layout:fixed; Explanation on http://www.w3.org/wiki/CSS/Properties/table-layout .
It should work too with max-width: DEMO
basic CSS for a max-width of 80px:
table {
table-layout:fixed;
}
table tr :first-child {
max-width:80px;
}

How do I Limit asp:Repeater in asp.NET?

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

linkbutton are not posting back in asp.net

I have many linkbutton placed on a webform everything working correctly til yesterday. But now now my ajax extenders are not working and linkbuttons are posting back.
some of my code is
<table width="460px">
<tr>
<td colspan="2"
style=" color: #C48239; font-style: normal; font-size: 18px; font-family: 'Bookman Old Style'; text-align:center;height:22px;">
Manage Your Profile</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lnk_per_pro" runat="server" CssClass="linkButton"
PostBackUrl="~/User/Edit_profile.aspx">Edit Personal Profile</asp:LinkButton> </td>
<td>
<asp:LinkButton ID="lnk_par_pro" runat="server" CssClass="linkButton">Edit Partner's Profile</asp:LinkButton></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lnk_con_det" runat="server" CssClass="linkButton"
PostBackUrl="~/User/Edit_profile.aspx">Edit Contect Details</asp:LinkButton></td>
<td>
<asp:LinkButton ID="lnk_add_photos" runat="server" CssClass="linkButton">Add Photos</asp:LinkButton></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lnk_hob" runat="server" CssClass="linkButton"
PostBackUrl="~/User/Edit_profile.aspx">Edit Hobbies and Interests</asp:LinkButton></td>
<td>
<asp:LinkButton ID="lnk_del_pro" runat="server" CssClass="linkButton">Remove/Delete Profile</asp:LinkButton></td>
</tr>
</table></center>
</div>
</div><!-- div 2 end-->
<div id="div3">
<div id="inbox1"><b>About Myself</b>
</div>
<div id="inbox2">Partner Prefrence
</div>
</div>
<div id="div4">
<div id="indiv4"><b>Basics Information</b>
<div id="edit4">
<asp:LinkButton ID="lnk_edit" runat="server" CssClass="link"
PostBackUrl="~/User/Edit_profile.aspx"> Edit</asp:LinkButton> <img src="images/edit.png" alt="" />
</div>
</div>
<div id="indiv24">
<div id="lt"><table class="style1" >
<tr>
<td style="color: #666666; width: 180px">
Age</td>
<td style="width: 180px">
<asp:Label ID="lbl_age2" runat="server" Text="Label"></asp:Label>
</td></tr><tr>
<td style="color: #666666; width: 180px">
Mobile Number</td>
<td>
<asp:Label ID="lbl_mob2" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td style="color: #666666; width: 180px">
Height</td>
<td style="width: 180px">
<asp:Label ID="lbl_height2" runat="server" Text="Label"></asp:Label>
</td></tr><tr>
<td style="color: #666666; width: 180px">
Email Id</td>
<td>
<asp:Label ID="lbl_eml2" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td style="color: #666666; width: 180px">
Marital Status</td>
<td style="width: 180px">
<asp:Label ID="lbl_mar_sts2" runat="server" Text="Label"></asp:Label>
</td></tr>
</table></div>
a link is shown on hover of linkbutton
javascript:webforms_DoPostbackWithOption:(NewPostBackoption("","false",true,"false"));
Do you have any validation controls inside application , check once by setting the Cause validation = false to link button control .
Some times your system has changed to old time(like one year back) also the link buttons won't redirect
There may be validation controls inside application , check once by setting the Cause validation = false to link button control .

binded value for a control outside a repeater

Just for curiosity can i give a control that is outside a repeater a binneded value
for example :
<asp:Repeater ID="topicRepeater" runat="server">
<ItemTemplate>
<table border="0" id="bodyTable" runat="server" style="width: 100%; height: 100%;">
<tr>
<td align="left" valign="top" style="padding-top: 6px; padding-right: 30px; padding-left: 5px; width: 50px;">
<img src='App_Themes/WebPortalTheme/images/ProfilePicSmall/<%# Eval("ProPic") %>'
style="width: 50px; height: 50px;" />
<div runat="server" id="username" style="padding-left: 8px; color: #002D79;">
<%# Eval("UserName") %></div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<telerik:RadTextBox ID="EditedReplyTxt" Text='<%# Eval("msg_text")%>' runat="server" Width="300px" TextMode="MultiLine" Height="100px"></telerik:RadTextBox>
Yes you can, read this example
http://msdn.microsoft.com/fr-fr/library/4hx47hfe(v=vs.80).aspx

Categories

Resources