How use dynamic image URl for Image button - asp.net

I have a Image button on the aspx page that is server side button.
Now I want to get image url for this from my code class. but this is not working.
<asp:ImageButton ID="total" runat="server" ImageUrl='<%=myappnamespace.Utility.GetImageURL("chckout_p.png")%>'>
Function GetImageURl is public in Utility calls and return the full image path.
If I used this for the following then its working fine
<img src='<%=myappnamespace.Utility.GetImageURL("chckout_p.png")%>'>
So what is wrong with the server side controls.

You can't use <%= codeBehindData.Here %> on server controls. You can use
<asp:ImageButton ID="total" runat="server" ImageUrl='<%# myappnamespace.Utility.GetImageURL("chckout_p.png") %>'>
Notice the <%#
Then call Page.DataBind() or total.DataBind() on Page_Load

Related

If statement inside Repeater control

Although there are several questions on this topic, I haven't found any satisfactory answers.
I have a Repeater that needs to display complex content. An IF statement is required within the template. I can't move this to the code-behind as I need to have server controls and user controls registered within the repeater. Here is what I need:
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<%# If CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
The above throws a compiler error. Any idea on how to achieve this? I need to evaluate the IsFix field in the If statement.
I would put each set of content within a server side panel, then on ItemDataBound event, set the visibility of one panel to true, and the other to false.
If visibility is set server side, then the front end content never even gets rendered.
Remove # from the code nuggets which is having if statement:-
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<% CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
Edit:
Try with conditional operator (I am not sure about the syntax in VB.NET, please check):-
<%# If(CBool(Eval("IsFix")), "Do Something", "Else do something" %>
It works perfectly!!
<%# If(CBool(Eval("bitExtemporaneo")) = True, "<img src=""Imagenes/Extempo.png"" alt=""Extemporaneo""/>", "")%>
Remove # from the code where is having if statement.
Then use the code like this.
<%
object objIsFix = ((System.Data.DataTable)rep.DataSource).Rows[_nIndex]["IsFix"];
if (bool(objIsFix)) {%>
<%-- HTML content including server and user controls --%>
<%}Else{%>
<%-- HTML content including server and user controls --%>
<%}%>
'_nIndex' is defined in the cs file

asp.net imagebutton new window

I have a Telerik Grid. In that I have the following code in an .aspx page. What I want to happen is that when the user click on the imagebutton control it launches a new window to show that image. Note, I have seen some code which use Postback but they are blocked by popup blockers. The control's image is set via codebehind but that should not matter for this question. Here is my code. Thanks!
<asp:ImageButton ID="prod_image_main" runat="server" AlternateText="Product Main Image"
Height="500PX" Width="540PX" />
Instead of using an asp:ImageButton control why not just use a link? That way you can simply set the target attribute of the link to _blank and have it open in a new window.
Something like this:
<img src="wherevertheimageis.jpg" />
Try to open a new window with a javascript function:
function OpenW() {
window.open('NewForm.aspx', '', "height=200,width=200");
}
And in your imageButton set the onclientclick:
<asp:ImageButton ID="prod_image_main" runat="server" AlternateText="Product Main Image"
Height="500PX" Width="540PX" onclientclick="OpenW()" />
And load the image at the onload of NewForm.
Based on Mike Evan's answer here is what works for me.
ASPX FILE:
<asp:HyperLink runat="server" ID="hpl_mainimg" Target="_blank">
<asp:Image ID="prod_image_main" runat="server" AlternateText="Product Main Image"
Height="500PX" Width="540PX" /> </asp:HyperLink>
Code Behind C#
prod_image_main.ImageUrl = File.Exists(Server.MapPath("images_products/" + rdr["image_1"].ToString())) ? "images_products/" + rdr["image_1"].ToString() : "images_missing/NotAvailable_RS.jpg";
prod_image_main.ToolTip = rdr["itemtitle"].ToString();
hpl_mainimg.NavigateUrl = prod_image_main.ImageUrl.ToString();

Video Embede code in Div panel? In Asp.net?

In Web application, i am using datalist control to bind the Embed videos to a <div> dynamically. They are coming good, but when we click on video in the datalist they are start to play. I dont want that, I would like to play the particular video in a popup, and make a non action on that video [div which contain embeded video]. I want to make that div enable false.
<asp:DataList ID="DtlstVideos" ToolTip="Click On Video Title" CellPadding="5" CellSpacing="5" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" OnItemCommandXSSCleaned="DtlstVideos_ItemCommand">
<ItemStyle />
<ItemTemplate>
<div id="divVideos" runat="server"><%# Eval("photos") %></div>
<asp:LinkButton ID="lnkVide" CommandName="Click" runat="server" Text='<%# Eval("videotitle") %>' ToolTip="Play" CommandArgument='<%# Eval("id") %>' Font-Bold="true" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
This is the current code, when a user clicks on the LinkButton a popup will fire and the video will play, but when click on div the video is playing in datalist only, is there any solution?
Id am not sure if I did understand you correct but I hope this might help.
If you are using youtub, you'll be able to access a preview picture of the movi in different-sizes.
The image is accessble via a certain convention like this:
http://img.youtube.com/vi/VIDEO_ID/#.jpg
Find details here: http://www.reelseo.com/youtube-thumbnail-image/
So what I would do is display only the image in the datalist (maybe with some div overlay which looks like a player)... and then on click pass the movi ID to your popup and then embed and play the movie in that popup...div or new window whatever you want..
HTH
Well, I would not use that <div> as you are using rather I would make use of the ImageButton that will act as the video image as when clicking you can fire your command and hooking up a javascript event on it you can fire up the popup when you want.
Let's imagine that you can use jQuery with this:
<asp:DataList ID="DtlstVideos" ToolTip="Click On Video Title" CellPadding="5" CellSpacing="5" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" OnItemCommandXSSCleaned="DtlstVideos_ItemCommand">
<ItemTemplate>
<asp:ImageButton id="divVideos" CssClass="img-video" runat="server" CommandArgument="<%# Eval("id") %>" data-video-id="<%# Eval("id") %>" CommandName="Click" ImageUrl="<%# Eval("photos") %>" />
<%# Eval("videotitle") %>
</ItemTemplate>
</asp:DataList>
and with jQuery you can simply hook up the click event to fire up your pop up and your command
$(function() {
$(".img-video").on("click", function() {
var url = 'playVideo.aspx?id=' + $(this).attr("data-video-id");
window.open(url, "Video Player","menubar=0,resizable=0,width=650,height=450");
return true; // let the image button fire the command argument
});
});
But I would never let it fire the Command event here and I would hook up always in the playVideo.aspx page as there you do have the video id as well and you can do what you need in that page without needing to fire the command event on the video strip.

Inner image and text of asp:LinkButton disappears after postback

I have a link button:
<asp:LinkButton ID="LinkButtonPrint" runat="server" OnClick="OnPrint_Click">
<img src="img/print-icon.png" alt="" />
<asp:Literal runat="server" Text="<%$ Resources:PrintPage %>" />
</asp:LinkButton>
In code behind I add an onclick handler in Page_Load like this:
LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "')";
The rendered HTML is like this:
<a href="javascript:__doPostBack('ctl00$LinkButtonPrint','')"
id="ctl00_LinkButtonPrint" onclick="StartLoadTracking(this, 'Loading...');">
<img alt="" src="img/print-icon.png">Print page
</a>
If I click this button it is working OK (it will respond with a PFD file so no HTML is sent back to the browser), but if I click another button on the page (which makes a full postback) the LinkButtonPrint will not have the inner content, it will be rendered like this:
<a href="javascript:__doPostBack('ctl00$LinkButtonPrint','')"
id="ctl00_LinkButtonPrint" onclick="StartLoadTracking(this, 'Loading...');"></a>
If I remove the LinkButtonPrint.Attributes["onclick"] = ... line from Page_Load everything works fine (except my js function is not called, but that is normal).
What am I missing here?
EDIT
This is duplicate of
asp.net Link button image not visible after postback
but that one is not solved either :(
This problem is now known to Microsoft, and they have published an official statement and workaround on the Microsoft Connect issue 718810: LinkButton controls collection lost on postback.
Microsoft statement is:
Thank you for the feedback. This seems like an oddity with the fact that the LinkButton control allows either direct text content via the Text property or in markup, or sub-controls via the Controls collection or markup. The workaround is to use a placeholder as the root content for the control and place the desired mixed content in there, e.g.
<asp:LinkButton runat="server">
<asp:PlaceHolder runat="server">
This is some text.</br>
<asp:Label runat="server">This is a control</asp:Label>
</asp:PlaceHolder>
</asp:LinkButton>
I found the solution:
I had to add runat="server" to the <img> tag inside the <asp:LinkButton>:
<img src="img/print-icon.png" alt="" runat="server" />
Just a note to add to the solution above.
I found this specifically occuring when I had enabled=false (to show disabled style) my linkbutton and then caused a postback from the page via another control.
The .Text component of the linkbutton got wiped out after postback.
BUT by putting runat="server" on my .Text / html tags (em, span), they now are retained after postback.
the problem is that the Attributes["onclick"] = override the handler of the onclick operation.
2 thing you could try :
LinkButtonPrint.Attributes.Add("onclick", "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "');");
or
LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "');" + LinkButtonPrint.Attributes["onclick"];

How to make UpdatePanel inside ListView work?

I have a page with a listview that shows something like posts. On each post there should be a "rate box" which works similar to the "Like" button in facebook. The rate box is a User Control, that has an update panel inside it.
If I put the control with some random values in the page it works great - but when I put it inside the ListView, where it should be located, it won't work. The method is being called, but nothing happens.
I simplified the code a bit to make it easier to understand:
The "rate box" control:
protected void OnRateClick(object sender, ImageClickEventArgs e)
{
Rate++;
RateAmountLiteral.Text = Rate.ToString();
RateButton.Visible = false;
FeedbackLiteral.Visible = true;
rateButtonPanel.Update();
}
ascx:
<div class="rate_div">
<asp:UpdatePanel ID="rateButtonPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
Rate:
<asp:Literal ID="RateAmountLiteral" runat="server"></asp:Literal>
<asp:ImageButton ID="RateButton" runat="server" ImageUrl="icn_rate.png"
OnClick="OnRateClick" />
<asp:Literal ID="FeedbackLiteral" runat="server" Visible="false">Thanks for rating!</asp:Literal>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
aspx (using the control):
<asp:ListView ID="PostsView" runat="server" ItemPlaceholderID="itemPlaceHolder2"
<LayoutTemplate>
<div class="posts_div">
<asp:PlaceHolder ID="itemPlaceHolder2" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="post_div">
<div class="post_body">
<%# CurrentPost.Body %>
</div>
<UC:RatingBox id="RatingBox" runat="server"
PostID="<%# CurrentPost.ID %>"
Rate="<%# CurrentPost.Rate %>"/>
By: <a href="<%# CurrentPost.Author.LinkToProfile %>">
<%# CurrentPost.Author.DisplayName %>
</a> |
<%# CurrentPost.LiteralTime %>
</div>
</ItemTemplate>
</asp:ListView>
While debugging I noticed the controls in the method "OnRateClick" are empty and don't contain the right values. Please advice.
Also if you have any comments about the way I did things, don't hold yourself.
Thanks
There are a lot things that you may not have set up, I cannot tell from just the code snippet you have given. But make sure you do the following: -
1) Place a ScriptManager "" on the page.
If you are using master page in your application and your web page uses the master page, place script manager in master page. Or alternatively,you can also place script manager on specific web pages anyway.
2) Add a Triggers for the button RateButton in your Update panel.

Resources