asp.net c# conditional within data repeater - asp.net

I want to do something very simple, only display an asp:image when I have value (i.e. not NULL) for the current DataItem.
As below, Image1 should only be output when there is a value for Image1 (i.e. Eval("Image1")) which is a field in the current DataItem.
<asp:PlaceHolder ID="NewsPlaceHolder" runat="server">
<asp:Repeater ID="NewsRepeater" runat="server">
<ItemTemplate>
<div class="newsItem">
<h3><%# Eval("Title") %></h3>
<div class="images">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>' />
<asp:Image ID="Image2" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),2) %>' />
</div>
<div class="content"><%# Eval("Content") %></div>
</div>
<br class="clear" />
</ItemTemplate>
</asp:Repeater>
</asp:PlaceHolder>
Is there some simple conditional statements I can use in the ASPX page? Many thanks!

You can try to check if it is null from codebehind
<asp:Image ID="Image1" runat="server" visible='<%# HasData(Eval("image")) %>' ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>'
C#
public bool HasData(object img)
{
if(img!=null) {return true;}
return false;
}

# aspx page.
<div class="images">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>' Visible = '<%#(Container.DataItem != null) ? true: false) %>' />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),2) %>' Visible = '<%#(Container.DataItem != null) ? true: false) %>' />
</div>

Related

ASP.Net image set ImageURL to external site

I am struggling to set ImageURL in a asp:image control. I am setting it to a URL from another site, at the moment I have several versions of the image trying to get one to work bellow is my code and the output.
Function used in images bellow
Function BuildLogoPath() As String
Return ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg"
End Function
ASP.net Code in page
<asp:Image ID="imgFav" runat="server" ImageUrl='<%: ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %>' /><br />
<asp:Image ID="Image2" runat="server" ImageUrl='<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image3" runat="server" ImageUrl='<% ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image4" runat="server" ImageUrl='<% Response.write(ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg") %>' />
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# BuildLogoPath() %>' /><br />
<asp:Image ID="Image5" runat="server" ImageUrl='<%= BuildLogoPath() %>' />
<asp:Image ID="Image6" runat="server" ImageUrl='<% BuildLogoPath() %>' />
<asp:Image ID="Image7" runat="server" ImageUrl='<% response.write(BuildLogoPath()) %>' />
<%= ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %><br />
<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>
Output:
<img id="ContentPlaceHolder1_imgFav" src="<%:%20ConfigurationManager.AppSettings("WebPath")%20&%20"img/favicon.ico"%20%>"><br>
<img id="ContentPlaceHolder1_Image2" src="<%=%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image3" src="<%%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image4" src="<%%20Response.write(ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg")%20%>">
<img id="ContentPlaceHolder1_imgLogo" src=""><br>
<img id="ContentPlaceHolder1_Image5" src="<%=%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image6" src="<%%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image7" src="<%%20response.write(BuildLogoPath())%20%>">
http://office.logma.biz/onefit.com.jamie/img/favicon.ico<br>
http://office.logma.biz/onefit.com.jamie//img/main/logo.jpg
As you can see the code is working fine when not not in the image control.
The simple answer is that you can't use <% and %> inside <asp: .. /> tags. You have two options:
Set it programmatically from code-behind:
imgFav.ImageUrl = Me.BuildLogoPath()
Render a normal <img> tag in the HTML instead:
<img src='<%= BuildLogoPath() %>' />

How to get commandargument of button inside listview using javascript in asp.net

I am working on an asp.net application in which i have a ListView in which there is a linkbutton. i want to get commandargument of the button which is inside the listview. I have used following code:
<asp:ListView ID="ListView5" runat="server" OnItemDataBound="ListView5_ItemDataBound">
<ItemTemplate>
<li>
<asp:Image runat="server" Style="width: 50px; height: 50px;" ID="Image1" ImageUrl='<%#Eval("Image") %>' />
<asp:HiddenField ID="lblWallID" runat="server" Value='<%#Eval("Wall_ID") %>'></asp:HiddenField>
<p>
<asp:LinkButton ID="linkID" runat="server" CommandArgument='<%#Eval("UID") %>'>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("Full_Name") %>'></asp:Label>
</asp:LinkButton>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Wall_Content") %>'></asp:Label>
</p>
<p>
<asp:Label ID="lblTime" CssClass="time-text" runat="server" Text='<%#Eval("TimeAgo") %>'></asp:Label>
</p>
<p>
<asp:LinkButton ID="btnClick" runat="server" Text="Comment" OnClientClick="SaveCom(this);" CommandArgument='<%#Eval("Wall_ID") %>' CommandName="Comment"></asp:LinkButton>
</p>
</li>
</ItemTemplate>
</asp:ListView>
The Javascript function is:
<script type="text/javascript">
function SaveCom() {
var val = $(this).commandargument();
</script>

using NameValueCollection how can I just get textbox contrl values?

How do I collect the values of JUST the text box controls?
Is there a way to specify this in ASP.NET 4.0?
ArrayList alAnswerList = new ArrayList();
int iCount = 0;
NameValueCollection frmCollect = Request.Form;
iCount = frmCollect.Count;
for (int i = 0; i <= iCount; i++)
{
alAnswerList.Add(frmCollect.GetValues(i));
}
I am trying to avoid hardcoding an exact index value to start getting values at.
Thanks!
Update: Control Code
<div id="Layer1" runat="server" style="overflow-x: hidden; overflow-y: hidden; padding: 0px 50px 0 50px">
<div id="buttonWrapper">
<asp:Label runat="server" ID="lblStartHunting">text</asp:Label>
<asp:Button runat="server" ID="btnStart" Text="Start" OnClick="btnStart_Click" BorderColor="WhiteSmoke" />
<br />
<br />
<br style="clear: both;" />
<asp:Label runat="server" ID="lblName">Name / Team Name to be added:</asp:Label>
<asp:TextBox runat="server" ID="txtName" Width="280px" BorderColor="WhiteSmoke"></asp:TextBox>
</div>
<div id="huntHeaderWrapper"><div id="huntHeaderPoints"><strong>Points</strong></div><div id="huntHeaderQuestion"><strong>Question</strong></div><div id="huntHeaderAnswer"><strong>Answer</strong></div></div>
<asp:DataList ID="dlHunt" runat="server" Width="740px" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<div id="huntDivPoints">
<asp:Label ID="pointsLabel" runat="server" Text='<%# Eval("points") %>' />
</div>
<div id="huntDivQuestion">
<asp:Label ID="questionLabel" runat="server" Text='<%# Eval("question") %>' />
</div>
<div id="huntDivAnswer">
<asp:TextBox ClientIDMode="Static" ID="huntAnswerText" runat="server" Width="240px" BorderColor="WhiteSmoke"></asp:TextBox><br />
<span style="color: #395069;">Hint: <asp:Label ID="hintLabel" runat="server" Text='<%# Eval("hint") %>' /></span>
</div>
</ItemTemplate>
<ItemStyle BackColor="White" BorderColor="#5885a3" BorderWidth="1" BorderStyle="Solid" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connection_private %>" SelectCommand="SELECT [id], [question], [hint], [answer], [points] FROM [table]"></asp:SqlDataSource>
<asp:Button ID="btnShowAnswers" runat="server" Text="Submit Answers" BorderColor="WhiteSmoke" OnClick="btnShowAnswers_Click" /> <asp:Label runat="server" ID="lblSubmit"> text</asp:Label>
</div>
Inside btnShowAnswers_Click, add the following code:
var answers = new List<string>();
if(dlHunt.Items.Count > 0)
{
foreach(DataListItem item in dlHunt.Items)
{
var textBox = (TextBox)item.FindControl("huntAnswerText");
answers.Add(textBox.Text);
}
}
// At this point you have your list of answers and can handle as you see fit
Try this:
// You might not be able access the controls directly here.
//As it might be on a master page,or Simple Page or a
//Content Page or an User Control. Either you have to use FindControl()
//or NamingContainer
foreach(Control c in Page.Controls)
{
if(c is TextBox)
{
var val = ((TextBox)c).Text; // or any other properties of textbox
}
}

ASP.NET C#, Access div element from nested Listview Code Behind

I'm trying to get the (div id="imgGallery" runat="server") element from nested Listview to modify the class name programatically.
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="VersionId" runat="server" Text='<%# Eval("VersionId") %>' Visible="false" />
<asp:Label ID="MenuContent" runat="server" Text='<%# Eval("ContentText") %>' />
<br />
<asp:ListView ID="ListView2" runat="server" ItemPlaceholderID="itemPlaceHolder2">
<ItemTemplate>
<div id="imgGallery" class="images" runat="server">
<asp:Image ID="Image1" ImageUrl='<%# Eval("ImageUrl") %>' runat="server" ToolTip="Text" />
</div>
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder2"></asp:PlaceHolder>
</LayoutTemplate>
</asp:ListView>
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</LayoutTemplate>
<ItemSeparatorTemplate>
<br />
<hr />
<br />
</ItemSeparatorTemplate>
on my code behind I tried the code below which doesn't work... and I tried different ways but can't find solution.
ListView listView2 = (ListView)ListView1.FindControl("ListView2");
HtmlGenericControl mydiv = (HtmlGenericControl)listView2.FindControl("imgGallery");
mydiv.Attributes.Clear();
You sholuld work with inner element of databound controls on databinding stage or iterating through row collection (called databind method before)

how to find the control OfferID?

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="OfferID" GroupItemCount="2" >
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="0" style="">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td runat="server" style="">
<div id="wrapper">
<div id="ResImage">
<div id="slideshow">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval ("Image1") %>' Width="250px" Height="190px" CssClass="active" />
<asp:Image ID="Image5" runat="server" ImageUrl='<%# Eval ("Image2") %>' Width="250px" Height="190px" />
<asp:Image ID="Image4" runat="server" ImageUrl='<%# Eval ("Image3") %>' Width="250px" Height="190px" />
</div>
</div>
<div id="ResDesc">
<asp:Label ID="lblDesc" runat="server" Width="290px" Height="190px" BackColor="White" Text='<%# Eval("Offer") %>'></asp:Label>
</div>
<div id="ResPrice1">
<asp:Label ID="lblValue" runat="server" Text="Value" CssClass="ResValue"></asp:Label>
<asp:Label ID="lblDiscount" runat="server" Text="Discount" CssClass="ResDiscount"></asp:Label>
<asp:Label ID="lblYouPay" runat="server" Text="You Pay" CssClass="ResYouPay"></asp:Label>
<div id="ResPrice2">
<asp:Label ID="lblValueAmt" runat="server" Text='<%# Eval("Value") %>' CssClass="ResValueAmt"></asp:Label>
<asp:Label ID="lblDiscountAmt" runat="server" Text='<%# Eval("Discount") %>' CssClass="ResDiscountAmt"></asp:Label>
<asp:Label ID="lblYouPayAmt" runat="server" Text='<%# Eval("YouPay") %>' CssClass="ResYouPayAmt"></asp:Label>
</div>
<asp:Label ID="lblRestaurantName" runat="server" Text='<%# Eval("RestaurantName") %>'></asp:Label><br />
<asp:LinkButton ID="lnkGetCoupon" runat="server">Get Discount Coupon</asp:LinkButton>
</div>
<div id="HowItWorks">
<asp:Label ID="lblHowItWorks" runat="server" Text="How It Works?" Font-Bold="True" Font-Size="Small" ForeColor="Red"></asp:Label>
<ul>
<li><asp:Label ID="Label3" runat="server" Text="1.Click on the 'Get Discount Coupon' button" Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label4" runat="server" Text="2.Get a print of your Voucher and carry it during your visit to the outlet." Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label5" runat="server" Text="3.Show your Voucher and pay the amount directly to the merchant. " Font-Size="10px"></asp:Label></li>
</ul>
</div>
<asp:Label ID="OfferID" runat="server" Text='<%# Eval("OfferID") %>' Visible="false"></asp:Label>
</div>
</td>
</ItemTemplate>
How to find the label control with the id=OfferID...how to use findcontrol here??
i want to find the OfferID of the row on which i click...i have a linkbutton lnkGetCoupon..when i click on the link button...i want to pass the OfferID in the query string to the next page.
i am a new user so they do not let me post answer to my own question
heres the answer...
i added CommandArgument='<%# Eval("OfferID") %> to the link button.
<asp:LinkButton ID="lnkGetCoupon" CommandArgument='<%# Eval("OfferID") %>' runat="server">Get Discount Coupon</asp:LinkButton>
and used the ListView1_ItemCommand
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
Dim offer As String
offer = e.CommandArgument.ToString()
Dim url As String = "~/RestaurantDedicatedPage.aspx?offerID=" + offer
Response.Redirect(url, True)
End Sub
You don't need the Label at all, you can get the OfferID from the DataKeys collection.
First, add a CommandName to your LinkButton:
<asp:LinkButton ID="lnkGetCoupon" runat="server" CommandName="GetCoupon">Get Discount Coupon</asp:LinkButton>
Then use it in the ItemCommand handler:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "GetCoupon")
{
ListViewDataItem item = (ListViewDataItem)e.Item;
int index = item.DataItemIndex;
string offerID = ((ListView)sender).DataKeys[index]["OfferID"].ToString();
Response.Redirect("yourUrl" + offerID);
}
}
Attach to the ListView.ItemCommand event and within that event you can search on the Item in the ListViewCommandEventArgs to find the control you need to alter.
Update your ListView in your ASPX to hook up the ItemCommand event:
<asp:ListView ... OnItemDataBound="ListView1_ItemCommand">
<ItemTemplate>
...
<asp:LinkButton id="lnkGetCoupon" CommandName="View" CommandArgument="<%# Eval("OfferID") %>" />
...
</ItemTemplate>
</asp:ListView>
The ItemCommand event will be fired when a Button or LinkButton (or some other button-esque control) is clicked. To handle this event, add the following
code to your *.aspx.cs (code-behind) file:
protected void ListView1_ItemDataBound(object sender, ListViewCommandEventArgs e)
{
//Check if the lnkGetCoupon button was clicked.
if (string.Equals("View", e.CommandName))
{
//Get the offerID from the CommandArgument.
int offerID = int.Parse(e.CommandArgument);
//Perform your logic using the offerID
}
}

Resources