set header in repeater to visible.false - asp.net

using a repeater to display a list, if the list returns empty the table will be blank, so I have displayed a message 'table is empty' but i also want to set the visibility of the table header to false, is there a property for tis?
repeater.header or something?
Thanks
EDIT: for those who cant program in the dark
<asp:Repeater id="rptSelectedUtilities" runat="server">
<HeaderTemplate>
<table class="detailstable FadeOutOnEdit">
<tr>
<th style="width:200px;">Utility</th>
<th style="width:200px;">Contacted</th>
<th style="width:200px;">Comment</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th style="width:200px;"><%# Eval("Name") %></th>
<th style="width:200px;"><asp:CheckBox ID="chkMyCheck" runat="server" Checked='<%# Convert.ToBoolean(Eval("Checked")) %>'/></th>
<th style="width:200px;"><%# Eval("Comment") %></th>
</tr>
<asp:Label id="labelTableEmpty" runat="server" Text="There are currently no items in this table." />
</ItemTemplate>
<FooterTemplate>
<asp:Label id="labelTableEmpty" runat="server" Text="There are currently no items in this table." />
</table>
</FooterTemplate>
</asp:Repeater>
thanks for any help

Alright, let's change this up a little. We are going to make the repeater invisible overall and then add another label to markup and make it visible when necessary. Replace the repeater code with this:
<asp:Repeater id="rptSelectedUtilities" runat="server">
<HeaderTemplate>
<table class="detailstable FadeOutOnEdit">
<tr>
<th style="width:200px;">Utility</th>
<th style="width:200px;">Contacted</th>
<th style="width:200px;">Comment</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th style="width:200px;"><%# Eval("Name") %></th>
<th style="width:200px;"><asp:CheckBox ID="chkMyCheck" runat="server" Checked='<%# Convert.ToBoolean(Eval("Checked")) %>'/></th>
<th style="width:200px;"><%# Eval("Comment") %></th>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
and then after the repeater add this (you can of course change the wording):
<asp:Label id="labelTableEmpty" runat="server" Text="There are currently no items in this table." />
and then in OnPreRender in the web form we're going to write some code:
protected override void OnPreRender(EventArgs e)
{
if (rptSelectedUtilities.Items.Count == 0)
{
rptSelectedUtilities.Visislbe = false;
labelTableEmpty.Visible = true;
}
else
{
rptSelectedUtilities.Visislbe = true;
labelTableEmpty.Visible = false;
}
}

Related

Gridview is loading multiple times

I have a gridview which is working fine (i.e. it is loading 4 rows) with bound controls. this grid view has 4 rows:
Ex: <asp:BoundField HeaderText="Classification" DataField="ClassType" />
but when I changed the gridview with itemtemplate then my gridview is loading 4 times
Structure:-
Gridview
- template field
-- Item template
<%# Eval("ClassType")%>
on code behind I am loading this via: (on page_load)
gvResultSet.DataSource = ds.Tables[0];
gvResultSet.DataBind();
Code
<asp:GridView ID="gvResultSet" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="tb">
<thead>
<tr>
<th>
Classification
</th>
</tr>
</thead>
<tbody>
<tr class="record">
<td>
<%# Eval("ClassType")%>
</td>
</tr>
</tbody>
</table>
</ItemTemplate>
</asp:TemplateField>
Set gridview's property AutoGenerateColumns="false". This will solve your issue.
Update
My Recommendation is to use Repeater Control.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table class="tb">
<thead>
<tr>
<th>
Status
</th>
<th>
Name
</th>
<th>
Start Time
</th>
<th class="date">
End Time
</th>
<th>
MAX Date found
</th>
<th>
Classification
</th>
<th class="last">
Read Description
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr class="record">
<td>
<div class="toggle enabled">
</div>
</td>
<td class="overflow">
<%# Eval("Name")%>
</td>
<td class="overflow">
12/23/2014 6:20:47
</td>
<td>
12/23/2014 6:27:21
</td>
<td class="date">
12/23/2014
</td>
<td>
<%# Eval("ClassType")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
It would be much simpler this way
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CssClass="tb">
<Columns>
<asp:TemplateField HeaderText="Classification">
<ItemTemplate><%# Eval("ClassType")%></ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="record" />
</asp:GridView>
If you want header inside th, do this after DataBind
gvResultSet.DataBind();
gvResultSet.HeaderRow.TableSection = TableRowSection.TableHeader;

ASP.NET Get Gridview Label Text in Javascript

I have muliple rows in my grid for hours. I have a common label for the same lblFromTime.
I need to get the label text bound in each row from javascript.
Could someone offer me a solution?
I tried the following code but it doesn't return the text.
var gvRowCount = ('<%= gvTimeSlots.Rows.Count%>');
for (i = 0; i <= gvRowCount; i++)
{
var fromTime = document.getElementById('ContentHead_MainContent_gvTimeSlots_lblFromTime_i');
alert(fromTime.value);
}
I tried out a sample like this
ASPX Code
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="check">
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%#Eval("iso") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
the Gridview generated the following HTML -
<table cellspacing="0" rules="all" border="1" id="gv" style="border-collapse:collapse;">
<tr>
<th scope="col">check</th>
</tr>
<tr>
<td>
<span id="gv_ctl02_lbl">ab</span>
</td>
</tr>
<tr>
<td>
<span id="gv_ctl03_lbl">ab</span>
</td>
</tr>
<tr>
<td>
<span id="gv_ctl04_lbl">ab</span>
</td>
</tr>
</table>
as you can see for my case the span id started from 2.
so i tried the following javascript
for(i=2;i<5;i++) console.log(document.getElementById('gv_ctl0'+i+'_lbl').innerHTML);
and the result was
ab
ab
ab
I tried it out in chrome and firefox(firebug). both are working fine.

How to group one row values in Listview

I have a dynamic Listview which is bind to three different tables with one to many relations i.e. one table row may contain many rows in other table. When i run my application i get this output.
But i want to get Listview in this format although this image has been edited using Photshop.
Here is Listview HTML.
<asp:ListView runat="server" ID="LV_ViewQuestion" DataKeyNames="UID, Question_ID">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No Surveys.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="SURVEY_TYPELabel" runat="server" Text='<%# Eval("Survey_Type")%>' />
</td>
<td>
<asp:Label ID="SURVEY_TITLELabel" runat="server" Text='<%# Eval("Survey_Title") %>' />
</td>
<td>
<asp:Label ID="Question_TextLabel" runat="server" Text='<%# Eval("Question_Text")%>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Option_Text")%>' />
</td>
<td>
<asp:LinkButton runat="server" ID="lb_DelQuestion" Text="Delete" CommandArgument='<%# Eval("Question_ID")%>' CommandName="XDelQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_AddMoreQuest" Text="Add Question" CommandArgument='<%# Eval("UID")%>' CommandName="XAddAnotQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_Publish" Text="Publish" CommandArgument='<%# Eval("UID")%>' CommandName="XPublishSurvey" CssClass="GeneralInput" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" class="nobordered" style="width: 580px;">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">Type</th>
<th id="Th2" runat="server">Title</th>
<th id="Th6" runat="server">Question</th>
<th id="Th4" runat="server">Options</th>
<th id="Th3" runat="server" style="width: 200px;">Actions</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" ButtonCssClass="GeneralButton" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
You can accomplish this hiding the cells in the ItemDataBound event of the ListView. Your code should look like this:
first add three global properties in your page
string type = string.Empty;
string title = string.Empty;
string question = string.Empty;
Then add the OnItemDataBound event to your list view
protected void LV_ViewQuestion_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label SURVEY_TYPELabel = (Label)e.Item.FindControl("SURVEY_TYPELabel");
Label SURVEY_TITLELabel = (Label)e.Item.FindControl("SURVEY_TITLELabel");
Label Question_TextLabel = (Label)e.Item.FindControl("Question_TextLabel");
if (SURVEY_TYPELabel.Text == type && SURVEY_TITLELabel == title &&
Question_TextLabel == question)
{
SURVEY_TYPELabel.Visible = false;
SURVEY_TITLELabel.Visible = false;
Question_TextLabel.Visible = false;
// Do the same for all the other control in cells you need to hide
}
else
{
type = SURVEY_TYPELabel.Text;
title = SURVEY_TITLELabel.Text;
question = Question_TextLabel.Text;
}
}
}

Can't access Subitems in ListView while looping

I am trying to loop over a ListView with a foreach statement, but I can't seem to get the Subitems of item. No success with a For statement either.
IntelliSense doesn't propose it on both ways.
Code Behind:
protected void btnNext_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in ListView1.Items)
{
item. *(here a should get the Subitems)*
}
}
ASPX
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
<LayoutTemplate>
<table>
<tr>
<th>Customer</th>
<th>Item No</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("CustomerName") %>
</td>
<td>
<%# Eval("Item") %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
you have to change your aspx page as below
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
<LayoutTemplate>
<table>
<tr>
<th>Customer</th>
<th>Item No</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerName" Text='<%# Eval("CustomerName") %>' runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblItem" Text='<%# Eval("Item") %>' runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Now you have to use for each loop as below in code behind file
string strProductNames = string.Empty;
foreach (ListViewItem item in ListView1.Items)
{
Label lblCustomerName= (Label)item.FindControl("lblCustomerName");
// strProductNames = strProductNames + lblCustomerName.Text + "<br/>";
// you can get values in lblCustomerName.Text. use this value as per your requirement
}
Hope this will helps you..happy coding
you should use loop throug listview.items
for (int j = 0; j < this.listView1.Items.Count; j++)
{
ListViewItem item =
(ListViewItem)this.listView1.ItemContainerGenerator.ContainerFromIndex(j);
}
Get data being bound to ListView on DataBound event
you would treat the code inside of your loop the same way that it would be handled inside of the listView1_ItemDataBound event.

Hide ListView table header from the code behind

I want to hide a column of ListView based on the role from the code behind. Here's the mark-up and the code:
<asp:ListView ID="lvTimeSheet" runat="server">
<LayoutTemplate>
<table id="TimeSheet">
<thead>
<tr>
<th id="thDelete" runat="server" Visible='<%# IsAdmin() %>'>
Select
</th>
</tr>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="cbMarkAsComplete" runat="server" onclick="selectMe(this)" Text=" Delete" />
</td>
</ItemTemplate>
</asp:ListView>
In the ListView layout template I have a <th> which has attribute id="thDelete" runat="server" Visible='<%# IsAdmin() %>' . In the code behind,
Public Function IsAdmin() As Boolean
If "user is admin" Then
Return True
Else
Return False
End If
End Function
But that column id="thDelete" is visible all the time. How do I go about hiding the column based on some condition from the code behind? Thank you for any input.
The tags with the attribute runat="server" can't allow inclusion <% %>. Try this:
<asp:ListView ID="lvTimeSheet" runat="server">
<LayoutTemplate>
<table id="TimeSheet">
<thead>
<% If IsAdmin() Then %>
<tr>
<th id="thDelete" runat="server">
Select
</th>
</tr>
<% End If %>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="cbMarkAsComplete" runat="server" onclick="selectMe(this)" Text=" Delete" />
</td>
</ItemTemplate>
</asp:ListView>
Please, try this:
<LayoutTemplate>
<table id="TimeSheet">
<thead>
<tr>
<th id="thDelete" runat="server" Visible='<%# If( IsAdmin().tostring()="True", "true", "false") %>'>
Select
</th>
</tr>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server" />
</tbody>
</table>
</LayoutTemplate>`

Resources