Add special characters after Eval in ASP.NET - c#

How can I add the & character after an eval in ASP.NET C#
I have this line
<asp:Image ID="img1" runat="server" CssClass="item" ImageUrl="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data-src='<%# "http://www.example.com/images/"+Eval("ThumbnailFile")+"&width=200" %>'
AlternateText='<%# Eval("Title") %>' />
and I get the link like this http://www.example.com/images/thumbnail.jpg&width=200
but I need the link to be http://www.example.com/images/thumbnail.jpg&width=200

You could try using HttpUtility.HtmlDecode in your data-src attribute:
data-src='<%# HttpUtility.HtmlDecode("http://www.example.com/images/"+Eval("ThumbnailFile")+"&width=200") %>'

Related

How to give if else condition in asp.net c# inside grid template?

I have written like below lines of code
<asp:TemplateField HeaderText="Marketing Document / URL" SortExpression="DocumentActualName">
<ItemTemplate>
<%# (String.IsNullOrEmpty(Eval("DocumentActualName").ToString() ) ? %>
<asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "ProductDocument") %>'
CommandName="Download" CausesValidation="false" Text='<%# Eval("DocumentActualName") %>'> </asp:LinkButton>
<% : %>
<a id ="lnkUrl" runat="server" href='<%# Eval("URL") %>' Text='<%# Eval("URL") %>'></a>
</ItemTemplate>
</asp:TemplateField>
It's not working. Please help
You need to bring the databinding tags (<%#) outside of the Page.ResolveUrl method, and use single quotes around the href attribute:
href='<%#Page.ResolveUrl(Eval("URL"))%>'
<a href='<%# String.IsNullOrEmpty(Eval("File").ToString()) ? Eval("URL") : Eval("File") %>'> URL </a>

Server tag : The server tag is not well formed

I got this error and I don't know what's wrong with my code...here it is:
<asp:LinkButton runat="server" ID="lnkbtnPDFPreview" Text="Preview"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,"productID") %>'
CommandName="<%# DataBinder.Eval(Container.DataItem,"documentID") %>">
</asp:LinkButton>
You are using " in the attribute value delimiter and inside the attribute:
CommandName="<%# DataBinder.Eval(Container.DataItem,"documentID") %>"
Change the outer delimiter to ' as already done for CommandArgument:
CommandName='<%# DataBinder.Eval(Container.DataItem,"documentID") %>'
I suspect it's this line:
CommandName="<%# DataBinder.Eval(Container.DataItem,"documentID") %>"
You did the right thing here!
CommandArgument='<%# DataBinder.Eval(Container.DataItem,"productID") %>'

ASP.NET Server Tags

When I use:
<asp:Label id="lbCatId" runat="server" Text='<%# Eval("VideoId") %>' Visible="true" />
I get:
<span id="ctl00_mainContent_ucCoverFlow_rptVideos_ctl00_lbCatId">5</span>
However when I use:
<asp:Image runat="server" href='Play.aspx?VideoId=<%# Eval("VideoId") %>' ID="iThumbnailFileName" CssClass="content" />
I get:
<img id="ctl00_mainContent_ucCoverFlow_rptVideos_ctl01_iThumbnailFileName" class="content" href="Play.aspx?VideoId=<%# Eval("VideoId") %>"
I would like to know why C# isn't generating the 'VideoId' like it is in the first example.
You are printing the string of the command you want to execute instead of executing it.
Use :
'<%# "Play.aspx?VideoId=" + Eval("VideoId") %>'
Instead of :
'Play.aspx?VideoId=<%# Eval("VideoId") %>'
Try using the ImageUrl property instead. The href attribute probably doesn't know how to interpret databinding syntax.
<asp:Image ID="Image1" runat="server" ImageUrl='Play.aspx?VideoId=<%# Eval("VideoId") %>' ...>
Have you tried:
<asp:Image runat="server" href='<%# "Play.aspx?VideoId=" + Eval("VideoId") %>' ID="iThumbnailFileName" CssClass="content" />
Try to change:
href='Play.aspx?VideoId=<%# Eval("VideoId") %>'
to
href='<%# "Play.aspx?VideoId=" + Eval("VideoId") %>'
'<%# Eval("VideoId","Play.aspx?VideoId={0}") %>'

ASP.NET server tags are not well formed

I want to use some server tags in OnClientClick of LinkButton
<asp:LinkButton ID="lnkbtnID" OnClientClick='<%# "window.open('SomePage.aspx?Id=" + Eval("reqID") + "');" %>' runat="server" Text='<%# Eval("reqID")%>'>
</asp:LinkButton>
how to write this, it is giving the error that Server tags are not well formed.
So how to write this correctly?
you can do this in code behind as well
lnkbtnID.Attributes.Add("onclick","window.open('SomePage.aspx?Id="+reqID+"');");
lnkbtnID.Text=reqID;
How about that
<a class="NewslinkButton" onclick="myRef = window.open(''+'<%# "SomePage.aspx?NewsID="+Eval("reqID") >','mywin','left=20,top=20,toolbar=0,resizable=1,scrollbars=1,height=300');myRef.focus()">
<asp:Literal ID="l1" runat="server" Text='<%# Eval("reqID")%>'></asp:Literal></a>

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()

Categories

Resources