I am trying to use linkbutton inside of foreach in asp.net
I have below html.
Asp.net html:
<table border="1" style="grid-cell: inherit; border-spacing: inherit;">
<thead>
<tr>
<th>İlan ID
</th>
<th>İlan Yolu
</th>
<th>Eklenme Tarihi
</th>
<th>İlk Güncelleme Tarihi
</th>
<th>Güncelleme Aralığı
</th>
<th>Son Güncelleme Tarihi
</th>
<th>Aktifmi
</th>
<th>Detay Göster
</th>
</tr>
</thead>
<%foreach (var item in list)
{%>
<tr>
<td style="text-align: center">
<span><%= item.Id%> </span>
</td>
<td>
<span><%=item.DosyaAdi %></span>
</td>
<td style="text-align: center">
<span><%=item.EklemeTarihi %></span>
</td>
<td>
<span><%=item.IlkGuncellemeTarihi %></span>
</td>
<td style="text-align: center">
<span><%=item.GuncellemeAraligi %></span>
</td>
<td style="text-align: center">
<span><%=item.SonGuncelleme %></span>
</td>
<td style="text-align: center">
<input type="checkbox" class="chk" id="<%=item.Id %>" <%= item.Aktif ==true ? "checked='checked'" : "" %> />
</td>
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
</td>
</tr>
<% } %>
</table>
Question:
In part of linkbutton as below
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
If i use CommandArgument="<%=item.Id%>" it is not working (it displays syntax error here)
Where i miss inside of code for command argument in linkbutton side ?
Any help will be appreciated.
Thanks
<%= %> is equivalent to Response.Write, it outputs directly to the html markup. So it cannot write to server-side controls at all, since it does not know about them. In other words, what you are trying to do is impossible.
You should probably consider refactoring this foreach to Repeater, which gives control over controls as well as html:
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<td style="text-align: center">
<span><%# Eval("Id") %></span>
</td>
... same for other tds ...
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument='<%# Eval("Id") %>' CommandName="Detay">Detay</asp:LinkButton>
</td>
</ItemTemplate>
</asp:Repeater>
And don't forget to data bind it:
Repeater1.DataSource = list;
Repeater1.DataBind();
Related
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();
}
I'm trying to add different font styles and sizes to one of the fields below. I'm using a FormView and am extracting the data using <%Eval("fieldname")%>. For this question I'm looking at the 'Comments' Eval field.
The output for this field will look like this:
"- this is a comment.
Commented by John Doe 27/09/2016 16:58
-another comment.
Commented by John Doe 27/09/2016 16:59"
Now my question is how to change the font styles of both comment value (- this is a comment.) and the user who commented (Commented by John...).
Basically, I want the comment to standout than the commented by section.
I'd appreciate any help. Thanks in advance!
<asp:FormView runat="server" ID="fvReport" DataKeyNames="ReportId" DataSourceID="SqlDataSource2" BorderStyle="None">
<ItemTemplate>
<div class="body2">
<h4><%# Eval("Report_Type") %> Report ID No. <%# Eval("ReportId") %></h4>
<table>
<tr>
<th colspan="5">Shift Details</th>
</tr>
<tr style="border: solid .5px;">
<td>Staff Name:</td>
<td style="width: 285px">
<%# Eval("StaffName") %>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td style="width: 19%">Shift Type:
</td>
<td>
<%# Eval("ShiftName") %>
</td>
<td style="text-align:right;">Shift Date:</td>
<td>
<%# Convert.ToDateTime(Eval("ShiftDate")).ToString("dddd, dd MMMM yyyy") %>
</td>
</tr>
<tr>
<th colspan="4">Report</th>
</tr>
<tr>
<td colspan="4">
<%# Eval("Report") %>
</td>
</tr>
<tr>
<th colspan="4">Comments</th>
</tr>
<tr>
<td colspan="4">
<%# Eval("Comments") %>">
</td>
</tr>
<tr>
<td colspan="2"> </td>
<td colspan="2"></td>
</tr>
</table>
</div>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:FormView>
I'm assuming that comments is just a record(not like records set) and if you want to use same font for all comments
<tr>
<td colspan="4" style>
<span style="font-size:12px;....">
<%# Eval("Comments")
.ToString()
.Substring(
0,
Eval("Comments")
.ToString()
.IndexOf(" Commented by")) %>">
</span>
<span style="font-size:8px;....">
<%# Eval("Comments")
.ToString()
.Substring(
Eval("Comments")
.ToString()
.IndexOf(" Commented by")) %>">
</span>
</td>
</tr>
Hello in my ASP Dot Net c# website i have an html report is there.Now i want to take a printout of the same.So i used Javascript and its showing only the content as a raw information.So how can i display both content and its css .
Javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementsByClassName('widget-content')[0];
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
button Click
<input type="button" onclick="PrintDiv()" value="Print" />
HTML Content
<div class="widget-content">
<div class="invoice-content">
<div class="invoice-head">
<div class="invoice-meta">
<%--Invoice <span class="invoice-number">#96558 </span><span class="invoice-date">Date:
2012-07-15</span>--%>
</div>
<h5 style="margin-left: 40%; height: 20px; font-size: large">
Order Form</h5>
<div class="invoice-to">
<ul>
<li><span>Booking Date:<asp:Label ID="dispbookingDate" runat="server"></asp:Label></span>
<span>Name<asp:Label TextMode="MultiLine" runat="server" ID="dispName"></asp:Label></span>
<span>Address:<asp:Label TextMode="MultiLine" runat="server" ID="dispAddress"></asp:Label></span>
</li>
</ul>
</div>
<div class="invoice-from">
<ul>
<li><span>Order No.<asp:Label ID="dispOrderNo" runat="server"></asp:Label></span> <span>
Wedding Date:<asp:Label runat="server" ID="dispWeddingDate"></asp:Label></span>
<span>Malayalam Date:<asp:Label runat="server" ID="dispWeddingMalayam"></asp:Label></span>
</li>
</ul>
</div>
</div>
<div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="style1">
Description
</th>
<th class="style2">
Rs.
</th>
<th>
Ps.
</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="total-label" colspan="2">
Total:
</th>
<th class="total-amount">
<asp:Label ID="dispTotal" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Adavance:
</th>
<th class="total-amount">
<asp:Label ID="dispAvance" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Balance:
</th>
<th class="total-amount">
<asp:Label ID="dispBalance" runat="server"></asp:Label>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td class="style1">
Auditorium Rent
</td>
<td class="style2">
<asp:Label ID="dispRent" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Dining Hall Rent
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Kathir Mandapam
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Tables and chairs
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Electricity charge for water
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Luxuary Tax
</td>
<td class="style2">
<asp:Label ID="dispLTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Central Service Tax
</td>
<td class="style2">
<asp:Label ID="dispCTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<%-- <p class="amount-word">
Amount in Word: <span>
<asp:Label ID="dispAmountWord" runat="server"></asp:Label></span>
</p>--%>
</div>
<input type="button" onclick="PrintDiv()" value="Print" />
</div>
Have you tried:
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
EDIT: I guess you should also close the body tag in this line
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
Your code should be like this:
function PrintDiv() {
var divToPrint = document.getElementsByClassName('widget-content')[0];
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
popupWin.document.close();
}
</script>
You have to use only one document.write() then it will definitely work try it .... :D
Net c# website i have an html report is there.Now i want to take a printout of the same.So i used Javascript and its showing only the popup box ,not the content.how to solve this issue.
Javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('widget-content');
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
button Click
<input type="button" onclick="PrintDiv()" value="Print" />
HTML Content
<div class="widget-content">
<div class="invoice-content">
<div class="invoice-head">
<div class="invoice-meta">
<%--Invoice <span class="invoice-number">#96558 </span><span class="invoice-date">Date:
2012-07-15</span>--%>
</div>
<h5 style="margin-left: 40%; height: 20px; font-size: large">
Order Form</h5>
<div class="invoice-to">
<ul>
<li><span>Booking Date:<asp:Label ID="dispbookingDate" runat="server"></asp:Label></span>
<span>Name<asp:Label TextMode="MultiLine" runat="server" ID="dispName"></asp:Label></span>
<span>Address:<asp:Label TextMode="MultiLine" runat="server" ID="dispAddress"></asp:Label></span>
</li>
</ul>
</div>
<div class="invoice-from">
<ul>
<li><span>Order No.<asp:Label ID="dispOrderNo" runat="server"></asp:Label></span> <span>
Wedding Date:<asp:Label runat="server" ID="dispWeddingDate"></asp:Label></span>
<span>Malayalam Date:<asp:Label runat="server" ID="dispWeddingMalayam"></asp:Label></span>
</li>
</ul>
</div>
</div>
<div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="style1">
Description
</th>
<th class="style2">
Rs.
</th>
<th>
Ps.
</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="total-label" colspan="2">
Total:
</th>
<th class="total-amount">
<asp:Label ID="dispTotal" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Adavance:
</th>
<th class="total-amount">
<asp:Label ID="dispAvance" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Balance:
</th>
<th class="total-amount">
<asp:Label ID="dispBalance" runat="server"></asp:Label>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td class="style1">
Auditorium Rent
</td>
<td class="style2">
<asp:Label ID="dispRent" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Dining Hall Rent
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Kathir Mandapam
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Tables and chairs
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Electricity charge for water
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Luxuary Tax
</td>
<td class="style2">
<asp:Label ID="dispLTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Central Service Tax
</td>
<td class="style2">
<asp:Label ID="dispCTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<%-- <p class="amount-word">
Amount in Word: <span>
<asp:Label ID="dispAmountWord" runat="server"></asp:Label></span>
</p>--%>
</div>
<input type="button" onclick="PrintDiv()" value="Print" />
</div>
In your javascript you are searching for the div with id widget-content
your code: var divToPrint = document.getElementById('widget-content');
but in your html you have <div class="widget-content"> and it has no id, it only has a class.
So you have 2 options.
OPTION 1
Change class to id
OPTION 2
Change your javascript to search for the class like so
var divToPrint = document.getElementsByClassName('widget-content')
NOTE: this will return an array of elements with that class, whether theres only one or more.
So in order to select the one you want; assuming there is only 1 div with this class you do like so:
var divToPrint = document.getElementsByClassName('widget-content')[0]
I have the following snippet getting control ids in a listview at runtime. However ctrl is null when debugged in spite of the correct id being provided.
foreach (ListViewItem item in lvData.Items)
{
string uid = item.ClientID;
Control ctrl = lvData.FindControl(uid+"_lbUnattend");
ctrl.Visible = false;
}
my template:
<asp:ListView runat="server" ID="lvData" DataSourceID="odsEvents"
OnDataBound="lvData_DataBound"
onselectedindexchanged="lvData_SelectedIndexChanged">
<LayoutTemplate>
<table class="diary" cellspacing="0" width="600px">
<tr>
<th align="center">
Date:
</th>
<th align="center">
Time:
</th>
<th align="center">
Event:
</th>
<th align="center">
Location:
</th>
<th align="center">
</th>
</tr>
<tr runat="server" id="itemPlaceHolder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#Eval("Title")%>
</td>
<td width="200px" align="center">
<%# Eval("Address") %>
</td>
<td width="100px" align="center">
<asp:LinkButton ID="lbLogin" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbAttend" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbUnattend" runat="server" CommandName="unattend" Text="I want to unregister from this event" CssClass="button" />
See details 
</td>
</tr>
</ItemTemplate>
</asp:ListView>
where linkbuttons whith ids generated from lbUnattend are the ones I want.
Anyone pointout where the problem is?
I think what you're looking for is
foreach (ListViewItem item in lvData.Items)
{
Control ctrl = item.FindControl("lbUnattend");
ctrl.Visible = false;
}