C# Repeater displaying single listing 12 times - c#

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.

Related

How to hide table rows in a DataList if column data returns null from SQL Server

I need to hide table rows in a DataList if column data returns null from SQL Server (for each individual column). I have it working successfully but this method will be very tedious as I have about 100 rows in my table. Surely there is a simpler way.
Here is my C# code:
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountStatus")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountStatus");
row.Visible = false;
}
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountName")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountName");
row.Visible = false;
}
}
Here is my webform markup:
<asp:DataList ID="DataListAccount" runat="server" OnItemDataBound="DataList1_ItemDataBound1">
<ItemTemplate>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
<tr id="rowAccountName">
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountName" runat="server" Text='<%# Eval("ACCOUNT_NAME") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
You can wrap the ItemTemplate contents with a PlaceHolder and use a Ternary Operator to set the visibility.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# !string.IsNullOrEmpty(Eval("ACCOUNT_STATUS").ToString()) ? true : false %>'>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
</asp:PlaceHolder>
But I would recommend you make sure you filter the source data of empty items. Something like
SELECT * FROM accounts WHERE account_status IS NOT NULL
just modify this code by adding multiple conditions
string value = Convert.ToString( row["MyColumn"]);
if (string.IsNullOrEmpty(value))

ASP.NET (C#) - ListView

In past, i worked on ListViews (.net 2.0) using a custom Template field but what i am trying to achieve here is the following
I am now working on .net 4.6
So basically a list which shows items like above and on mouse-hover few options show up as shown in the following screenshot
I also have to trigger those option to do different things -
How can I do that in asp.net, may I please have some code references.
Cheers
P.S.
This is a rough example of how i am creating the List Item Template (as requested)
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<AlternatingItemTemplate>
<table >
<tr>
<td ><asp:Image ID="image1" ImageUrl='<%# Bind("url") %>' runat="server" Width="98px" /> </td>
<td><h2><asp:Label ID="_label" runat="server" Text ='<%# Bind("title") %>'></asp:Label></h2><asp:Label ID="Label1" runat="server" Text ='<%# Bind("description") %>'></asp:Label></td>
</tr>
</table>
</AlternatingItemTemplate>
<EmptyDataTemplate>
No data was returned.
</EmptyDataTemplate>
<ItemSeparatorTemplate>
<br />
</ItemSeparatorTemplate>
<ItemTemplate>
<table >
<tr>
<td ><asp:Image ID="image1" ImageUrl='<%# Bind("url") %>' runat="server" Width="98px" /> </td>
<td><h2><asp:Label ID="_label" runat="server" Text ='<%# Bind("title") %>'></asp:Label></h2><asp:Label ID="Label1" runat="server" Text ='<%# Bind("description") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<ul id="itemPlaceholderContainer" runat="server" style="">
<li runat="server" id="itemPlaceholder" />
</ul>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
I can add any html formatting to this template e,g i can add ASP:button etc but i don't know how to trigger those to perform certain tasks.
One easy way to achieve your requirement is to keep those buttons there but invisible and show them up when the parent container is hovered. following as a quick sample
aspx
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr class="row-data">
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="PositionLabel" runat="server" Text='<%# Eval("Position") %>' />
</td>
<td>
<div class="btn-area">
<asp:Button runat="server" Text="Button1" />
<asp:Button runat="server" Text="Button2" />
</div>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
Name
</th>
<th runat="server">
Position
</th>
<th>
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
css
.btn-area
{
display: none;
}
.row-data:hover .btn-area
{
display: block;
}
code-behind
protected void Page_Load(object sender, EventArgs e)
{
ListView1.DataSource = new List<dynamic>() {
new { Name = "Andy", Position = "PG"},
new { Name = "Bill", Position = "SD"},
new { Name = "Caroline", Position = "Manager"}
};
ListView1.DataBind();
}
UPDATE
ListView ItemCommand can capture the postback by button pressed and CommandName makes you able to recognize which button fired it.
<asp:Button runat="server" Text="Button1" CommandName="c1" />
<asp:Button runat="server" Text="Button2" CommandName="c2" />
code-behind
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "c1")
{
// do something when button1 pressed
}
else if (e.CommandName == "c1")
{
// do something when button2 pressed
}
}

Repeater inside another repeater. access an event values

I have an repeater inside another repeater. The second repeater have an checkbox event "isChecked_OnCheckedChanged". My problem is there. When this event occurs I need to access the value of the variable "lblName" and also the value of the variable "lblID" in parent repeater.
<asp:Repeater ID="rptOne" OnItemDataBound="populateSecondRepeater" runat="server">
<HeaderTemplate>
<table s>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</td>
<td style="width: 15%; vertical-align: top;">
<asp:Repeater ID="rptTwo" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 85%;">
<img runat="server" src='<%# Eval("Img") %>' alt="#" id="img_flag" /></td>
<td style="width: 15%; padding-right: 40px">
<asp:Label ID="lblName" runat="server" Style="display: none;" Text='<%# Eval("IDName") %>'></asp:Label>
<asp:CheckBox ID="check" runat="server" AutoPostBack="True" Checked='<%# Eval("isChecked") %>' OnCheckedChanged="isChecked_OnCheckedChanged" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I can access the value of the variable "lblName", but how do I access the values ​​of the parent repeater?
protected void isChecked_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
RepeaterItem item = chk.NamingContainer as RepeaterItem;
if (item != null)
{
Label lbl = (Label) item.FindControl("lblName");
}
}
thank you.
There are multiple ways to do this. You can add something like ParentID property to your data model, set that to the value you want and bind that to a hidden field next to the lblName label.
Alternatively, use a <tr runat="server" id="rptOneRow"> for each item in rptOne, and jump up in isChecked_OnCheckedChanged from the RepeaterItem until you find such row. Then use FindControl to find the lblID.
Or see here how to access the data directly - Accessing parent data in nested repeater, in the HeaderTemplate.

Get Repeater Label in the current "row" being accessed

I have the following Repeater:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound"> <ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
I am trying to get the data in each Label (ex: Convert.ToString((Label)item.FindControl("Date")) ) but it is returning an empty string, what am I doing wrong:
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
var rdbList = item.FindControl("rbtVerified") as RadioButtonList;
switch (rdbList.SelectedValue)
{
case "1":
if (new LeaveLogic().AddLeaveEmployee(Convert.ToString((Label)item.FindControl("Date")), Convert.ToDouble((Label)item.FindControl("Hours")), Convert.ToString((Label)item.FindControl("AMorPM")), "Vacational Leave", Convert.ToInt32(Context.User.Identity.Name), Convert.ToString((Label)item.FindControl("Note")))
{
Response.Redirect(Request.RawUrl);
}
break;
I believe it's not working because you aren't finding the controls. FindControl will return null if it can't find the control, and Convert.ToString will return an empty string if the object value is null.
From what I can see, you are searching for the wrong string ID. So instead of Date, it should be lblDate.
If you are in debug build mode, keep in mind that ASP.NET loves to change your control names at runtime, so "lblDate" control might not actually be "lblDate". So you can try debugging on the browser and inspect your elements' IDs for their actual IDs.
Also, you probably want to do this instead if you want the label's actual data (notice the .Text):
((Label)item.FindControl("lblDate")).Text

Repeater unable to show textbox

I have a repeater trying to get multiple data to display.
which includes a few textboxes which will show the current setting.
take note that this acts like a 'edit info' page for multiple images at once.
i also have problem displaying the images from the database.
To make it simple :
my .cs code:
DataTable ChildImageDT = myImagesBAL.GetChildImageDT(userID, childID, display);
var userList = new List<Images>();
foreach (DataRow row in ChildImageDT.Rows)
{
var child = new Images()
{
DateTaken = DateTime.Parse(row["image_taken_dt"].ToString()),
PlaceTaken = row["image_taken_loc"].ToString(),
DetailedInfo = row["image_info"].ToString()
};
userList.Add(child);
}
Repeater1.DataSource = userList;
Repeater1.DataBind();
my .aspx code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table class="content_background">
<tr>
<td width= "10%">Date Taken:</td>
<td><asp:TextBox ID="txtName" Text="<%#Eval("DateTaken")%>" Visible="true" runat="server" Height="100px" Width="100px"></asp:TextBox></td>
</tr>
<tr>
<td width= "10%" bgcolor=aqua>Place Taken:</td>
<td bgcolor=blue ><asp:TextBox ID="txtPassword" Text="<%#Eval("PlaceTaken")%>" Visible=true runat="server" BackColor="White" Font-Size="Large" ForeColor="Fuchsia" Height=50px ></asp:TextBox></td>
</tr>
<tr>
<td width= "10%">Detailed Info:</td>
<td><asp:TextBox ID="TextBox1" Text="<%#Eval("DetailedInfo")%>" Visible=true runat="server" ></asp:TextBox></td>
</tr>
</table>
</ItemTemplate>
my output as shown:
note: that the output is in the "text: " but the whole text box doesnt appear.
You might be getting a "The server tag is not well formed." error.
Just change your Eval code to single quotes instead of a double quote e.g.
Text="<%# Eval("DateTaken") %>" // It's understood as string text
to
Text='<%# Eval("DateTaken") %>' // now understood as server side code.

Categories

Resources