C# - Iterate Through A Datalist To Set The Visibilty Of A Control
Feb 10, 2011
I have a datalist and a hyperlink control that holds a url. A user creates this datalist through an admin website and not all of the hyperlinks have a link in them. The issue is how to hide the link that doesn't have a url in the database field.
code for the datalist:
<asp:DataList ID="dtlPromoEvents" runat="server" CellPadding="2" CellSpacing="2" RepeatColumns="1" RepeatDirection="Vertical" RepeatLayout="Table" >
<ItemTemplate>
<asp:Label ID="lblPromoHeading" runat="server" Text='<%# Eval("eventHeading") %>' Font-Size="12pt" ForeColor="#FFFF00" />
<br />
<br />
<asp:Label ID="promoDate" runat="server" Text='<%# Eval("eventDate", "{0:D}") %>' Font-Size="11pt" ForeColor="#33FF00" /> <span style="color: #33ff00; font-size: 12pt">@</span> <asp:Label ID="lblPromoTime" runat="server" Text='<%# Eval("startTime", "{0:t}") %>' Font-Size="11pt" ForeColor="#33FF00" />
<br />
<br />
<asp:Label ID="lblPromoDetails" runat="server" Text='<%# Eval("eventDetails") %>' Font-Size="11pt" />
<br />
<br />
<br />
<asp:HyperLink ID="eventLink" Text="Check this out!" CssClass="linkEvent" Visible="false" runat="server" Target="_blank" ToolTip="click to go check out what's happening!" NavigateUrl='<%# Eval("eventLink") %>' />
<br />
<br />
<br />
<hr style="width: 480px; height: 1px; background-color: #ff9900; border-color: #ff9900" />
<br />
</ItemTemplate>
</asp:DataList>
In the code behind I set the datasource with code and now I am stumped on how to reach into the items and control the visibility of the 'check this out' link based upon whether the db field has a link in it or not.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtlPromoEvents.DataSource = LoadEvents();
//filter through the events and turn on visibility for the events that have a live link.
dtlPromoEvents.DataBind();
}
}
public static DataSet LoadEvents()
{
DataSet eventInfo = new DataSet();
string connectionString = ConfigurationManager.AppSettings["ConnectBeerGeeksDb"];
using (SqlConnection selectConnInfo = new SqlConnection(connectionString))
{
SqlDataAdapter adapterInfo = new SqlDataAdapter("SELECT [eventDate], [startTime], [eventHeading], [eventDetails], [eventLink] FROM [promoEvent] WHERE (eventDate + 1 > GETDATE()) ORDER BY eventDate", selectConnInfo);
selectConnInfo.Open();
eventInfo.Clear();
adapterInfo.Fill(eventInfo);
selectConnInfo.Close();
}
return eventInfo;
}
Adding the refined code that works on this url: [http://beergeekspub.com/events.aspx][1]
protected void dtlPromoEvents_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Hyperlink control in the current DataListItem.
HyperLink eLink = (HyperLink)e.Item.FindControl("eventLink");
// Check if a URL exists, if not then hide the control
if (string.IsNullOrEmpty(eLink.NavigateUrl))
{
eLink.Visible =false;
}
}
}
also the page load databinds the datalist -
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtlPromoEvents.DataSource = LoadEvents();
dtlPromoEvents.DataBind();
}
}
View 1 Replies
Similar Messages:
Jul 27, 2010
I've searched and searched bu can't find anything exactly on point and it seems so simple.I have a gridview (combination of boundfields & templatefields) that is in a user control. I want to set a property (bool) to show or hide certain columns right from the very start. I've tried: GridView1.Columns(5).Visible = false; but I get "Gridview.Columns cannot be used like a method"There has to be a relatively easy way to change this in code-behind. What am I missing? And where should I do it? Page_load, Prerender, Databound?
View 7 Replies
Jun 29, 2010
Here is my problem. I need to display an html table with 9 cells in each row, how can I do it without writing every cell's html code ?I'd need something like that :
<table>
<tr>
<asp:repeater runat="server" datasource="[0..9]">
[code]...
View 1 Replies
Dec 4, 2010
how do I use the datalist "DL_Pro_Result" to find the child datalist "DL_Gro_Result" in C#?
For example in the following code, dlii value is null, even though dli != null.
DataList dli = (DataList)Page.FindControl("DL_Pro_Result");
DataList dlii = (DataList)dli.FindControl("DL_Gro_Result");
<div id="ProList">
<asp:DataList ID="DL_Pro_Result" runat="server">
<HeaderTemplate>
<table id="T_Pro_Result_Header" runat="server">
<tr>
<td>
<asp:Label ID="L_Pro_Result_Header" runat="server"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table id="T_Pro_Result_Item" class="table" runat="server">
<tr>
<td>
<asp:Label ID="L_Pro_Result_Item" runat="server"></asp:Label>
<asp:Button ID="B_Pro_Result_Item_1" OnClick="B_Pro_Result_Item_1_Click"/>
</td>
</tr>
<tr>
<td>
<asp:DataList ID="DL_Gro_Result" runat="server">
View 1 Replies
Dec 6, 2010
Finding DropDownList Control Within DataList Control?
[Code]....
[Code]....
<asp:LinkButton ID="AddBtn" runat="server">Add Committee</asp:LinkButton>
</FooterTemplate>
</asp:DataList>
View 1 Replies
Sep 2, 2010
Am not sure why I cannot access my Label control which was inside the Panel and the Panel is inside the DataList
[Code]...
but when I tried using "lbl" in Page_Load, it seems it cannot find the control?
[Code]....
that code returns Object reference not set to an instance of an object.
[Code]....
output:
panel does not exist
View 2 Replies
Jan 7, 2011
how i can find a DataList which is inside the DataList control? Its giving the Error Object not set to an Instance. i am finding the control this way :
DataList dl =((DataList) (DataList1.FindControl("DataList2")));
View 2 Replies
Mar 22, 2012
How to use ajax rating control with datalist control ....
View 1 Replies
Sep 2, 2010
I am wondering if MVC offers a handy function to iterate through all areas. I know I can search the sub-directories of Areas to achieve this. I am just curious.
View 3 Replies
Jun 21, 2010
Is it possible to iterate through an unordered list like the following in codebehind?
[Code]....
I was hoping to iterate through all the li elements of the ul checking whether NavigateUrl is equal to the current url. If it is, I was going to add a CssClass to give it a different appearance.
I think I know how to compare it to the page's url and to how to add the css class. I'm just not quite sure how you iterate through li items using a HTML Generic control.
View 5 Replies
Apr 5, 2010
I have a section of a form that I need to handle differently from the rest of form results. In the section that needs special handling I need to iterate over 3 form fields that have the same name. They have to have the same name, I can't change it. The section of the form I am referring to looks something like this:
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
Using C# I try something like this:
I try to handle it like this:
int i;
for (i = 1; i <= Request.Form["Color"][i]; i++)
{
colorName.Text += Request.Form["Color"];
}
Which leads to the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
How should I be handling form fields with the same name?
View 4 Replies
Feb 5, 2010
Say I have a class:
public class TestClass
{
public String Str1;
public String Str2;
private String Str3;
public String Str4 { get { return Str3; } }
public TestClass()
{
Str1 = Str2 = Str 3 = "Test String";
}
}
Is there a way (C# .NET 2) to iterate through the Class 'TestClass' and print out public variables and attributes?
Remeber .Net2
View 4 Replies
Oct 13, 2010
I have List (Of Report). Report has 90 properties. I dont want write them each and every propertiy to get values for properties. Is there any waht to get propeties values from the List
Ex:
Dim mReports as new List(Of Reports)
mReport = GetReports()
For each mReport as Report In mReports 'Here I want get all properties values without writing property names next
View 4 Replies
Jun 1, 2010
Does anyone know how I can iterate through textboxes on a usercontrol and put the values into a list. This is what I have tried so far...
[Code]....
[Code]....
View 2 Replies
Oct 20, 2010
As I understand the datalist can display recordsets horizontally instead of just vertically or in a paginated fashion?
Is there any other data controls that can do this like a formview or does only a datalist have that capability?
View 3 Replies
Jan 6, 2010
I have an html table in an aspx page (C#) that has columns like
1.CheckBox 2.Text 3.Text 4.TextBox
I want to iterate through the table one row at a time and process (run a stored procedure based on column2) based on whether the checkbox is checked or not. How will I be able to do that?
View 4 Replies
Apr 5, 2010
I have a Telerik RadGrid with a GridTemplateColumn that contains a checkbox, as follows:
<telerik:GridTemplateColumn HeaderText="MINE" UniqueName="MyTemplateColumn">
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
I want to set the box to be "checked" based on a value read from the database. I could handle the ItemDataBound event and read the database when each row is bound, but that results in n lookups. Instead, I want to handle DataBound, and then set all the values at once. So, in that method, I want code like this:
[code]....
View 2 Replies
Sep 20, 2010
I have an asp website, and a dataset with a datatable. When I check I after configuring it it shows all the data. But when I run the code I got an exception System.Data.ConstraintException was unhandled by user code Message="Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." I want to figure it out with iterating datarow.
[Code]....
View 1 Replies
Mar 31, 2010
I have a multi section form which the user can jump between sections to complete. Each section has its own validation group associated with it. I want to create a summary page which itterates through each of the validation groups and outputs if it is valid or not by changing an associated text.The problem that I am having is that once one validation group fails validation all subsequent sections also report as failing - presumably as the page.isvalid is still holding the fact that a previous group has failed
View 1 Replies
Oct 26, 2010
I've created a wallboard application to display outstanding support calls for my ICT department. I've bound a number of gridviews to sqldatasources which execute a stored procedure. This is automated via asp.net ajax controls and partially refreshes the page/data every 30 seconds.
At the moment, when the number of records in the gridview goes over 9, the gridview automatically pages and shows the number of pages in the bottom right hand corner. The helpdesk can then VNC to the box which controls the screen and manually click to see what's on the next page.
What I am after is a way to programmatically (using the c# code-behind file) changing the current displayed page after 10/15 seconds or so, obviously if this is possible in the scope of the gridview. I trailed using javascript (and failed at jquery) of scrolling the gridview within a div, however this didn't work as expected.
Can anyone point me in the right example. I can't find anyone else querying this functionality via a quick Google.
Gridview Code:
<asp:GridView ID="GridView1" ShowHeader="False" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
GridLines="None" CellPadding="2" Font-Size="35pt" AllowPaging="True" PageSize="9">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="ASSIGNEES" HeaderText="ASSIGNEES" SortExpression="ASSIGNEES">
<ItemStyle Width="32%" Wrap="false"/>
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title">
<ItemStyle Width="53%" Wrap="false"/>
</asp:BoundField>
</Columns>
</asp:GridView>
SqlDataSource Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FPConnectionString %>" SelectCommand="HDMonitoringOutstandingToday" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Link to printscreen of wallboard (not allowed to post images, damn newbie) >>> http://cl.ly/b48b88d9e5f9b7fa43ab
View 2 Replies
Oct 11, 2010
User stored his/her profile picture in "Shared%20Pictures/Profile%20Pictures/bhind8ball.bmp" path in SSP.
How to get this List item using this url? Or How to iterate SharePicture/ProfilePicture to get this List item using SPQuery?
View 1 Replies
May 16, 2010
I'm trying to read all filenames from a specified (server- not client) folder, and insert all the filenames into a javascript Array. It should behave like the Directory.GetFiles method in ASP.NET C#. I created a new array, and I just need the loop method (Javascript or jQuery) to iterate in the specific folder, and insert all the filenames into the array.
View 2 Replies
Sep 2, 2010
I want to know that how i can add the Linkbutton in Datalist in ItemTemplate at run time in the DataBound Event.
View 2 Replies
Feb 23, 2011
i have 6 textboxes which i want to iterate.
they are however in a TD in a TR in a TABLE in a PANEL etc.
the only way i've figured out to iterate them is in this way:
this.Controls[0].Controls[3].Controls[7].Controls
that's not only errorprone, but also hard to come up with.
but this.FindControl (to find one by name) doesn't work either, does findcontrol also only search in the direct child, and not the whole hierarchie?
so basicly what i'm looking for is to iterate ALL controls in the page, no matter in which level of the hierarchie, to check if it's a textbox.
View 1 Replies
Jul 20, 2010
I want to select many rows from my SQL Server database and combine them in a certain manner. Currently, I've been using the following method to get these rows:
[code]....
View 1 Replies