Dynamic imageURL in asp.net c# - c#

At the moment i have this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="Drawing.Usercontrols.WebUserControl2" %>
<asp:Image ID="Image1" runat="server" alt="Image Not Found" ImageUrl="~/images/k8up7l1i.bmp"/>
in the webUserControl2.ascx page.
In WebUserControl2.ascx.cs page, all of the logic is found. An Image is generated there, and then saved to a path (C:\Work Drawing\Drawing\Drawing\images)
where:
string thisPathName = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/" + randomFileName);
My question is, can i call a method "getPathName()" from the asp page (where ImageUrl is, instead of a static URL). So each time i call it, the new image will be displayed in the browser with that pathname generated in the c# code?
Please notify me if the question was unclear.

the answer is Yea , you can .
let's assume you have this function in code behind:
public string getPathName(){
return "my_image_path.jpg";
}
by that , you can have this in the form :
<img alt="Image Not Found" src="<%=getPathName()%>"/>
and there you go . you get you'r image path from the code behind .
good luck .

You can't assign function call result to server control property (like ImageUrl) unless that control is within data-bound control like GridView or Repeater.
What you can do is to set ImageUrl in code behind e.g. in PageLoad event:
Image1.ImageUrl = getPathName();
or, if you want to do it from asp page and you don't need to reference Image1 from server, render it as html element:
<img alt="Image Not Found" src='<%= this.getPathName %>' />
getPathNmae must be 'protected' or 'public' for above to work.

Related

C# - I can't access my Master Page's img tag's src attribute from my code behind

<td>
<a runat="server" href="~/url.aspx">
<img src="<%= ResolveClientUrl("~/images/image1") %>" id="submissions"
border="0" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"></a></td>
When I try to run this code with runat="server" added to my img tag, I get a Parser Error that says "Server tags cannot contain <%...%> constructs." The C# code I tried in my code behind's Page_Load is:
if (Request.Url.AbsoluteUri.Contains("submissions"))
submissions.Attributes["src"] = "~/images/image3";
The goal is to highlight the part of the navigation bar that corresponds to the page the user is already visiting. The problem is that it doesn't allow me to access the img tag's src attribute.
Try
<img src='<%= ResolveClientUrl("~/images/image1") %>' id="submissions" ...
Pay attention to ' not "
If you need access to this tag from CodeBehind - the easiest way is to replace <img/> to <asp:Image runat="server" id="submissions"/> and in Master codebehind access by ID, but in child page - using
Image submissions = (Image)this.Master.FindControl("submissions");
Pay attention to right escaping here:
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl(\'~/images/image2\') %>',1)"

How to update Html control without runat="server"

I have an Html image which I need to update dynamically but without runat="server" my code behind cannot see it.
<img name="MDI" id="detailimg" src="" alt="blankimage.jpg" />
I can access it using Page.Request.Form["MDI"]; but not update it
I need to load the src image at run time so can someone please tell me how I can update the control in my code.
Do with inline syntax like below
<img name="MDI" id="detailimg" src="<%= getImageSource() %>" alt="blankimage.jpg" />
In codebehind
public string getImageSource()
{
return "urlpath/img.jpeg";
}
More about Inline Syntax
If you want to update via code behind, you have to set runat="server"
If inline is fine, use src="<%= something%>
Otherwise use javascript for example
as you know if you want to access an element/control from code behind,you should set runat="server" but there is 2 other ways to set properties from code behind
use inline syntax as other guys shows (<%= YourVoidToGetImgSrc %>
use Ajax and get value from code behind in your html page.

Asp.net ImageUrl Webforms

this is definitely an easy question but I still don't know what exactly it is for. Can anybody tell me what ImageUrl='<%# Eval("FileName") %>' means? I still don't get the idea why we need to include %#.
<%# Eval("FileName") %> is used in the context of binding data from a collection to a control. Probably the value for the imageurl is coming from a property of an object in the collection
For example, List<Photo> where Photo has a property of FileName. If you are binding that to a gridview, a repeater, etc, you'll access that property for each item in the collection when binding to such controls
Asp.net data binding overview
in this Line...
ImageUrl='<%# Eval("FileName") %>'
ImageURL the attribute of your asp:ImageButton control that is used to specify the Url of the Image File to be Used
Code between '<% and %>' tags are writtent to be Executed on the Server
'#' is used to specify that the result of server side execution will be bound hear
Eval KeyWord is Used To Evaluate the perticular Column Value (that you specify ("--hear--")) from The DataSourse
When you are using a Template Control like Repeater , GridView , etc. you are actually iterating in a list of data records, and <%# Eval("FileName") %> here means give me the value of the column named FileName.
Here we have used the Eval function which is used for one way databinding. FileName is the field name you are associating. Anything that's written inside <%# %> is parsed by asp.net engine before generating the webpage source which is pure client side script and html tags.
So Eval function is executed at server end by ASP.net engine.

Disable hyperlink in .NET

Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>

filling <img src="?"> dynamically in asp.net c#

Hi would like to add img source path dynamically as shown in the snippet below, but giving error. I know img_src_path adding syntax is correct. Unfortunately I don't know the solution, need help.
Environment:
ASP.net, c#
<% string a[0]="image/hello.jpg;"
{
String img_src_path= a[0].ToString();%>
<li><a href='"<%#img_src_path%>"'><img src='"<%#img_src_path%>"' alt="" title=""/></a></li>
<%}%>
/Shashi
The problem is that your image tag does not have a runat="server" attribute (you will also need to add an ID in order to do this).
You also need to change your server tags like this:
FROM:
<%# ... %>
TO:
<%= ... %>
Also, the proper way to do this in ASP.Net would be to use the Image server control.
<asp:Image id="Image1" runat="server"></asp:Image>
Then, you would set the NavigateUrl property.
replace your code with this:
"=" instead of "#"
the solution
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' alt="" title=""/></a></li>
and if you want to edit image src form code behind
solution 2:
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' id="myImage" runat="server" alt="" title=""/></a></li>
edit image source form codebehind:
myImage.src = "imagePage";
The basic error we are trying to workout is Server tags cannot contain <% ... %> constructs.

Categories

Resources