How to put eval string in function? - c#

My question is how can I put this
<%# Eval("about")%>
into this function
<% Utils.UserUtils.showNiceDesc(here goes string - "about") %>
In asp.net webforms?
Regards

This does what you want; it passes the result of Eval to a method.
<%# Utils.UserUtils.showNiceDesc(Eval("about")) %>

Use combination of single and double quotes.
'<% Utils.UserUtils.showNiceDesc(Eval("about")) %>'

Use like below
You should use ToString() function.
'<% Utils.UserUtils.showNiceDesc(Eval("about").ToString()) %>'

<tag text='<%# Utils.UserUtils.showNiceDesc(((YourDataItemClass)Container.DataItem).about) %>' />
or
<tag text='<%# Utils.UserUtils.showNiceDesc(DataBinder.Eval("about")) %>' />
Reference: http://msdn.microsoft.com/en-us/library/bda9bbfx(v=vs.100).aspx

Related

Eval method, how to bind more than one value?

I want to pass two values with a navigate URL using Eval method, but it doesn't take more than one value.
Here is my code
<asp:HyperLink ID="HyperLink1" runat ="server" Text='<%#Eval("ReportTitle") %>' NavigateUrl='<%# Eval("ReportId","GroupId","~/Groups/ReportPage.aspx?ReportId={0}&Group={1}")%>' > </asp:HyperLink>
But I have this error(Error3 No overload for method 'Eval' takes 3 arguments)
so how can i do what i want to?
Thanks
Try this:
<%# String.Format("~/Groups/ReportPage.aspx?ReportId={0}&Group={1}", DataBinder.Eval(Container.DataItem, "ReportId"), DataBinder.Eval(Container.DataItem, "GroupId"))%>
You might want to review this.
One way is:
<%# String.Format("{0} - {1}", Eval("Name1"), Eval("Name2")) %>

Display value of Resource without Label or Literal control

How do I display the value of a resource without a ASP.NET control, i.e. I want to avoid this:
<asp:Label text="<%$ Resources: Messages, ThankYouLabel %>" id="label1" runat="server" />
Instead I would prefer to do just this in my .aspx pages:
<%$ Resources: Messages, ThankYouLabel %>
... but I can’t, a parser error is thrown:
Literal expressions like '<%$ Resources: Messages, ThankYouLabel %>' are not allowed.
Use <asp:Literal runat="server" Text="<%$ Resources: Messages, ThankYouLabel %>" /> instead.
Use HttpContext.GetGlobalResourceObject instead:
<asp:Label text='<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>'
id="label1"
runat="server" />
It's not possible. you have to use atleast Literal, Another option is to use GetGlobalResurceObject, so that you can use directly in a page.
<%= GetGlobalResourceObject("Messages", "ThankYouLabel")%>
In code behind You can Use
`GetLocalResourceObject("YourKeyInLocalResource")`
and also
`GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey")`
and then use a simple aspnet variable in your Asp.net Markup like <%= Resourcevalue %>
The you can assign your resource value to your Aspnet Variable like
Resourcevalue = GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey").ToString();
Another method is :-
<asp:Label text='<%= Resources.Messages.ThankYouLabel %>'
id="label1"
runat="server" />

server tags in markup

Morning all I have frequently used the old
<asp:Label ID="lblWas" runat="server" Text='<%# XPath("FACEVALUE") %>'></asp:Label>
This type of thing. when I first came across it I loved it, i'm using it again today but not quite so simply.
I have a number of extra things I would like to achieve.
Apply formatting to the value. Like Text='<%# string.Format(XPath("FACEVALUE"), "{0:c}") %>'>
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/' + XPath("FACEVALUE")>Book</asp:LinkButton>
For option number 2 the URL is not as I would expect, and for number 1 I cannot get the syntax correct if it's even possible.
I have not been able to find something suitable in google. Hopefully what I am trying to achieve is obvious from the example :)
You can use the TemplateControl.XPath(string xPathExpression, string format) override:
<asp:Label Text='<%# XPath("FACEVALUE", "{0:c}") %>' />
<asp:LinkButton Text="..." PostBackUrl='<%# XPath("FACEVALUE", "/THEATRE/{0}") %>' />
As you can see, you do not need to use string.Format because you can pass the format directly into the XPath method!
I believe for #1, you have messed up the syntax, you want to use
Text='<%# string.Format("{0:c}", XPath("FACEVALUE")) %>'
or Text='<%# XPath("FACEVALUE", "{0:c}") %>'
For #2, you need to use data binding expressions
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='<%# "/THEATRE/" + XPath("FACEVALUE")%>'>Book</asp:LinkButton>
For the first thing it must be Text='<%# string.Format( "{0:c}",XPath("FACEVALUE")) %>'>
and for the second one it should be
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/ + <%# XPath("FACEVALUE") %>'>Book</asp:LinkButton>

HyperLink with NavigateUrl with Eval(). Where is the mistake?

First I was changing HyperLink.NavigateUrl in code-behind on Page_Load().
But after I decided to do it in design using Eval() method.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Eval("type"), Eval("id")) %>' Text="Refuse" />
or
<asp:HyperLink ID="urlRefuse" runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
where id and type - are variables from Request.
But it doesn't work. Only raw text 'Refuse' is shown. Where is my mistake? Thanks in advance.
this is working great
NavigateUrl='<%# Eval("type","~/Refuse.aspx?type={0}") %>'
This worked for me
NavigateUrl='<%# String.Format("{0}.aspx?ID={1}", DataBinder.Eval(Container.DataItem, "Category"), DataBinder.Eval(Container.DataItem, "Post_ID")) %>'
Try and ViewSource in your browser, what's being rendered to the client in your href? Is it what you expected?. If you are trying to use variables from the request collection you can't use Eval, you need to use the Request query string parameters.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
Try this:
HttpUtility.UrlEncode(Eval("type")
Try this one:
<asp:HyperLink ID="HyperLink2" runat="server" onclick='<%# String.Format("AcceptUser({0},{1})",Eval("UserId"), Eval("TsId")) %>' NavigateUrl="javascript:void(0)" Visible='<%# (bool)Eval("CanDelete") %>'>Accept</asp:HyperLink>
Try this it worked for me:
Eval("type").ToString()

How to 'bind' Text property of a label in markup

Basically I would like to find a way to ddo something like:
<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label>
I know I could set it from code behind (writing lId.Text = MyProperty), but I'd prefer doing it in the markup and I just can't seem to find the solution.
(MyProperty is a string property)
cheers
You can do
<asp:Label runat="server" Text='<%# MyProperty %>' />
And then a Page.DataBind() in the codebehind.
Code expressions are an option as well. These can be used inside of quotes in ASP tags, unlike standard <%= %> tags.
The general syntax is:
<%$ resources: ResourceKey %>
There is a built-in expression for appSettings:
<%$ appSettings: AppSettingsKey %>
More info on this here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
Leave the markup as is and make a call to Page.DataBind(); in your code behind.
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>
since asp.net tags do not allow <% %> constructs, you cannot use Text="<%= MyProperty %>".
<div> <%=MyProperty"%></div>
Call lID.Databind() from code-behind
When you use <%# MyProperty %> declaration you need to databind it, but when using <%= MyProperty %> you don't (which is similar to just writing Response.Write(MyProperty).
You can do this:
<asp:Label ID="lblCurrentTime" runat="server">
Last update: <%=DateTime.Now.ToString()%>
</asp:Label>

Categories

Resources