Popup window from asp:Hyperlink with parameter in c# - c#

I need open in my GridView the asp:Hyperlink with parameter to window popup.
Here is a working sample for a popup window with parameter, but I have error.
The error message says:
Server tag is not correctly formatted.
What's the problem ?
How to do resolve this ?
Can you help me ?
Thank you in advance.
My code below.
<asp:TemplateField HeaderText="btest">
<ItemTemplate>
<asp:HyperLink runat="server" ID="btest" Text="btest"
NavigateUrl='<%#"javascript:_popupWin=window.open('btest.aspx?Sample_ID=" + Eval("Sample_ID") + "',
'_popupWin','width=300,height=300,resizable=yes,location=yes,scrollbars=yes');_
popupWin.focus();" %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Try this :
<asp:TemplateField HeaderText="btest">
<ItemTemplate>
<asp:HyperLink runat="server" ID="btest" Text="btest"
NavigateUrl='<%# String.Format("btest.aspx?sID={0}", Eval("Sample_ID"))%>'
onclick="javascript:w= window.open(this.href,'Sample_ID',
'left=20,top=20,width=1500,height=300,toolbar=0,resizable=0');return false;">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

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

gridview hyperlinkfield url path issue

lets say i have the following link stackoverflow.com stored in db.
When i click the link from grid-view it redirect me to below path
http://localhost:30987/Main/stackoverflow.com
Note I am taking user input so i cant add http:// to user input because i cant determine the user website use HTTP or HTTPS
How can i solve this issue ?
<asp:TemplateField HeaderText="Website" SortExpression="Website">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" Text='<%# Bind("Websitelink") %>' NavigateUrl='<%# Bind("Websitelink") %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
As like Andrei mentioned in the comment, use HtmlAnchor(<a>) instead for <asp:HyperLink to make it work. You code will looks like the following:
<asp:TemplateField HeaderText="Website" SortExpression="Website">
<ItemTemplate>
<%#Eval("Websitelink") %>
</ItemTemplate>
</asp:TemplateField>

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

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>

Send value of gridview row using hyperlink within it

I am not able to sent the value of the MachineID to another page using the hyperlink in gridview.
<!-- <asp:TemplateField HeaderText="FailedFiles"
SortExpression="NumFailedFilesOverSLA">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%#Bind("NumFailedFilesOverSLA") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I have tried putting
DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}"
but don't know why this is not working??
Please suggest...
thanks
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Inventory_ID", "/default.aspx?ID={0}") %>'
Text="Details"></asp:HyperLink>
</ItemTemplate>
This should solve your problem. This is exactly how I used it.
If this doesn't work, then check that you are actually getting a value back from the DB for MachineID:
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("NumFailedFilesOverSLA") %>'
runat="server" DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}">
</asp:HyperLink>
First, try to put the default gridview in the page and attach it to the data source, so you can test if there is data to display.
If you are assigning the data source from code behind don't forget to call the DataBind() method after that.

Categories

Resources