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.
Related
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.
I have a class that just have defined url constants, let's say it is called Urls. I am trying to access a constant in that class inside of a a LoginStatus tag as follows:
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="Redirect" LogoutPageUrl= <%= Urls.logout %> CssClass="myButton" />
and obviously it isn't working. I know I can hardcode the string inside of the field, but I am trying to avoid that if possible. Thanks for the insight!
Try this
LogoutPageUrl="<%# Urls.logout %>"
The # is used to "bind" values to server side properties whereas the = is the same as a Response.Write() which in this case will just write some text to the markup
I have a Repeater inside a Repeater, how can a use the code below:
<input type="hidden" value='<%# Container.ItemIndex %>' />
pointing to the first repeater?
This question is similar; although it talks about accessing a property from the <HeaderTemplate>, it feels like it should work from the <ItemTemplate>.
So try <%# ((RepeaterItem)Container.Parent.Parent).ItemIndex %>
If this doesn't work, you may need more .Parents. Try attaching an ItemDataBound handler to the inner repeater temporarily, and use the fact that the RepeaterItemEventArgs Item property returns the same object as Container gives in the aspx. So basically evaluate e.Item.Parent, e.Item.Parent.Parent etc. until you find the one that is another RepeaterItem. Then use the same number of .Parents in your aspx.
From MSDN: How To Display Hierarchical Data by Using Nested Repeater Controls
The article is a few years old but the content is what you're looking for.
I have ASP.NET GridView in my web app and would like to bind two fields into one column and use string formatting. I am providing example below, is it possible to implement into GridView?
I have two data fields Name and DefaultParam and would like to display these fields into one GridView column.
Like this
Name=DefaultParam
If DefaultParam value is empty I would like to show only Name value and do not include =
I was using Repeater and code below to achieve this but now decided to move data display to GridView
<%#Eval("Name")%>
<%# (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):"" %>
You can use a TemplateField and put your logic in there:
<asp:TemplateField HeaderText="Header">
<ItemTemplate>
<%#Eval("Name") + (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):""%>
</ItemTemplate>
</asp:TemplateField>
Another option would be to use a Property on your object to do that logic for you behind the scenes and just use that as a BoundField, but you didn't mention what the object is your binding is.
You can simple write server-side code between <%# ... %> as you write in code behind. just put it in '' (between single quotes) .
<asp:Lable id="lblxx" runat="server"
Text='<%# Eval("Name") + (!string.IsNullOrEmpty(Convert.ToString(Eval("DefaultParam")))) ? "= " + Eval("DefaultParam"):"" %>' />
Follow this tutorial link to know that how to Custom Formatting Based Upon Data using Template Fields.
I currently have a repeater control and inside the itemtemplate I have a usercontrol. This usercontrol renders correctly, but I am trying to assign a dataitem to a property in the repeater control.
<asp:Repeater ID="Repeater1" DataSourceID="EntityDataSource" runat="server">
<ItemTemplate>
<uc1:Request ID="Request1" runat="server" RequestId='<%# Eval("RequestId") %>' />
</ItemTemplate>
RequestId is just an Int32. It just doesn't assign it.
I can put the eval outside of the usercontrol just in the itemtemplate and it correctly outputs the right id.
If I remove the whole eval and just type a number in then it works fine.
Any help appreciated.
[UPDATE] : Issue Solved
I was using an EntityDataSource and this automatically binded to the repeater. It printed out all the information from the database on the screen without any codebehind. But when I put in the code behind Repeater1.DataBind(); it then started to work.
I don't know why, but hey it's solved. It now successfully passes the value through. I imagine it has something to do with the page lifecycle.
If you just bind with repeater collection of int, you need use this:
<uc1:Request ID="Request1" runat="server" RequestId='<%# Container.DataItem %>' />
And don't forget to call DataBind() for repeater or for Page where there is a repeater control.
Are you missing a ' at the end?
change following:
RequestId='<%# Eval("RequestId") %> />
to
RequestId='<%# Eval("RequestId") %>' />
You can implement this by using Repeater control's ItemDataBound event, so that you can set the dynamic property for the control.