Getting control ids in a repeater - c#

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&nbsp
</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;
}

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

Find index value of repeater to open new webform and read data from SQL

How do I pass the ID value linked to a repeater row to open the values in a new webform with textfields which can then be edited and updated in repeater (SQL Stored procedure). I am new to repeaters and I managed to do this using a GridView but I'm not sure how to do it with a repeater.
What I did with the GridView c# which worked perfect in Default.aspx.cs:
if (dgvEmployees.SelectedIndex != -1)
{
indexId = dgvEmployees.SelectedRow.Cells[1].Text;
Response.Redirect("About.aspx?&Id=" + indexId);
}
What is received in About.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(Request.QueryString["Id"]))
{
idNum = Convert.ToInt32(Request.QueryString["Id"]);
if(!IsPostBack)
{
FillEmployeeData();
}
}
}
This is the repeater code:
<asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound" Visible="true">
<HeaderTemplate>
<table class="table table-bordered" id="VersionsTable">
<tr>
<th>Actions
</th>
<th>Employee Number
</th>
<th>ID Number
</th>
<th>Employee Name
</th>
<th>Employee Surname
</th>
<th>Number of Dependants
</th>
<th>Race
</th>
<th>Gender
</th>
<th>Delete
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr style="cursor:pointer" onmouseover="style.backgroundColor='#E6E6E6'" onmouseout="style.backgroundColor=''" onclick="style.backgroundColor='#727A91'">
<asp:HiddenField ID="hIDfield" runat="server" Value='<%#Eval("Id")%>' />
<td>
<asp:button runat="server" OnClick="btnEdit_Click" CssClass="btn btn-default" Text="Edit"/>
</td>
<td onclick="PopupEdit('<%# Eval("EmployeeNumber") %>')">
<%# Eval("EmployeeNumber") %>
</td>
<td>
<%# Eval("IDNumber") %>
</td>
<td>
<%# Eval("employeeName") %>
</td>
<td>
<%# Eval("employeeSurname") %>
</td>
<td>
<%# Eval("numberOfDependants") %>
</td>
<td>
<%# Eval("RaceId") %>
</td>
<td>
<%# Eval("GenderId") %>
</td>
<td style="text-align:center">
<asp:CheckBox ID="chkDelete" CssClass="form-control" runat="server" />
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Thank you for your help!

If then Else in ASP.NET Repeater with Checkbox

Here is my ASP repeater code
<asp:repeater id="filterTable" runat="server">
<HeaderTemplate>
<br/><br/><br/>
<div class="table-responsive">
<table width="100%" cellspacing="0" id="table_odd" class="quickordertable table table-bordered table-striped table-condensed grid-table">
<tr>
<th style="width: 80px">
Details
</th>
<th style="width: 80px">
Filter
</th>
<th style="width: 30px">
Qty
</th>
<th style="width: 60px">
Include In Order
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Details")%></td>
<td><%#Eval("FilterType")%></td>
<td><%#Eval("Qty")%></td>
<td><asp:CheckBox ID="orderPartNumber" Checked="true" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
I have an ASP ItemTemplate with a field of <%#Eval("GenuineOnly")%>
If field GenuineOnly contains a value, (Or is not Null) Then I want to show this code
<td>GENUINE ONLY</td>
Else I want to show this code
<td><asp:CheckBox ID="orderPartNumber" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td>
I've tried a number of different ways but can't seem to get the syntax right! Any help would be appreciated.
You can achieve this using this way:
<td>
<asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length > 0 %>'>
<asp:CheckBox ID="orderPartNumber" Checked="true" runat="server" />
<asp:HiddenField runat="server" ID="hiddenPartNumber" Value='<%#Eval("PartNumber")%>' />
</asp:Panel>
<asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length == 0 %>'>
GENUINE ONLY
</asp:Panel>
</td>
I assume you can add Table/Td as per your need.
<asp:Repeater ID="rptr" runat="server" OnItemDataBound="rptr_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblGenuineOnly" runat="server" Text='<%#Eval("GenuineOnly") %>' />
<asp:CheckBox ID="orderPartNumber" runat="server"/>
<asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>' runat="server" />
</ItemTemplate>
Code behind
protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblPartNumber = (Label)e.Item.FindControl("lblGenuineOnly");
CheckBox chkPart = (CheckBox)e.Item.FindControl("orderPartNumber");
if (string.IsNullOrEmpty(lblPartNumber.Text.Trim())) {
//Display GENUINE ONLY in any label like lblPartNumber.Text = "GENUINE ONLY"
chkPart.Visible = false;
}
else
{
chkPart.Visible = true;
}
}

Asp.net syntax error for Command Argument in linkbutton

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

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