ASP.NET Server Tags - c#

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}") %>'

Related

Replacing a content in HyperLink with Eval

I am trying to replace a content in NavigateUrl with Eval Content.
My aspx code is:
<asp:TemplateField HeaderText="Info">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Eval("RNum") %>' NavigateUrl='<%# AreaID == "249" ? "http://google.com" : "http://someadress/login.main?req={0}" %>' />
</ItemTemplate>
<ItemStyle CssClass="border_right" />
</asp:TemplateField>
Here, I want to replace {0} inside a NavigateUrl with the <%# Eval("RNum") %> value.
I tried replacing it but it didnt worked.
Can anyone help me out ?
I don't see where are you formating or doing the concatenation, but this should do the job:
<asp:HyperLink runat="server" Text='<%# Eval("RNum") %>'
NavigateUrl='<%# AreaID == "249" ?
"http://google.com" :
"http://someadress/login.main?req=" + Eval("RNum") %>' />

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>

The server tag is not well formed - hyperlink databound

I have a formatting issue with my hyperlink, it works OK with the text part along so I know it's an issue with the JavaScript but don't know what the problem is.
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hypCustType" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>'
NavigateUrl="javascript:sendval('<%# DataBinder.Eval(Container.DataItem, "CustType") %>');">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
'The problem is, you have closed the string in the NavigateUrl Property. You should use ' or \" inside of inline code to not end the string.
So you should try this:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hypCustType" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>'
NavigateUrl="javascript:sendval('<%# DataBinder.Eval(Container.DataItem, \'CustType\') %>');">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
replace the asp:hyperlink with a normal html tag link:
<a href="javascript:sendval('<%# DataBinder.Eval(Container.DataItem, "CustType") %>');">
<%# DataBinder.Eval(Container.DataItem, "CustType") %>'</a>
Try it like this:
<asp:HyperLink ID="hypCustType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>' NavigateUrl='<%# "javascript:sendval(\"" + DataBinder.Eval(Container.DataItem, "CustType") + "\");" %>'></asp:HyperLink>

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") %>'

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