Replacing a content in HyperLink with Eval - c#

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

Related

Get redirect to another page from Hyperlink button in gridview

I have one gridview in which i'm binding a List of Website names like
www.google.com
www.facebook.com
www.gmail.com
www.google.com
www.facebook.com
www.gmail.com
Now, I want to redirect to www.google.com when i click on this
I used this one but its not working
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Website", #"{0:hh\:mm}") %>' Text='<%# Eval("Website", #"{0:hh\:mm}") %>' Target="_blank"></asp:HyperLink>
You should include "http://" before your redirect link.Please go through the below example,
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Website","http://{0}") %>' Text='<%# Eval("Website") %>' Target="_blank"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
OR
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Website") %>
</ItemTemplate>
</asp:TemplateField>
Make sure the NavigateUrl has http:// or https:// in it, otherwise the browser will think it is a link in the same domain.
NavigateUrl='<%# "https://" + Eval("Website", #"{0:hh\:mm}") %>'
Or if some website do have that http in them but others don't, you can check for that first
NavigateUrl='<%# Eval("Website").ToString().Contains("http") ? Eval("Website") : "https://" + Eval("Website") %>'

how can i make a gridview rows clickable

i have a gridview that i filled it with filenames from a path, know, i want the filenames to be clickable so i can download them and i don't know how, can someone help me.
code-behind (file is the gridview ID)
string path = "//the path";
string[] Fname = Directory.GetFiles(path).Select(Path.GetFileName).ToArray();
file.DataSource = Fname;
file.DataBind();
the web page
<asp:GridView ID="file" runat="server" style="border:hidden" ></asp:GridView>
You can bind a single list/array item and get it in the ItemTemplate like this:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a target="_blank" href="<%# Container.DataItem %>">
<%# Container.DataItem %>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink ID="UserId" runat="server" DataNavigateUrlFields="UserId" NavigateUrl='<%# "~/Report.aspx?name=" %>'
Text='<%# Eval("UserId") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

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>

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

Categories

Resources