hyperlink in gridview error - c#

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=""
onclick="javascript:w= window.open(
<%# Eval("booking_id","hideFromStartborrow.aspx?booking_id={0}")%>,
'mywin','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');">
new Window</asp:HyperLink>
I want to send it to another one, but it does not.And want to make the page smaller.

You can try this Hyperlink
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# String.Format("hideFromStartborrow.aspx?booking_id={0}", Eval("booking_id")) %>' onclick="javascript:w= window.open(this.href,'DownloadImage','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');return false;">Open</asp:HyperLink>

OnClientClick will be executed in the client side and only if it returns true,it will be posted back to server..If its false then nothing will happen.. Inorder to deal directly with server side use onClick..Here you are using both javascript and server side code so i would recommend onclientclick..

Related

Add multiple parameters in URL asp.net

I am currently trying to send 2 parameters through an URL in ASP.NET. I have been able to successfully send across 1 parameter. I am now wanting to learn how to send 2 across.
I am wanting to also send another ID as well that links to Favourite_ID similar to what is happening below with Recipe_ID
ASP.NET
<asp:HyperLink Runat ="server" NavigateUrl ='<%#"RecipePage?id=" + DataBinder.Eval(Container.DataItem, "Recipe_ID").ToString()%>' ID="Hyperlink1"><%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%></asp:HyperLink></asp>
<asp:HyperLink Runat ="server" NavigateUrl ='<%#"RecipePage?id=" + DataBinder.Eval(Container.DataItem, "Recipe_ID").ToString()+"&YourId="+DataBinder.Eval(Container.DataItem, "YourId")%>' ID="Hyperlink1"><%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%></asp:HyperLink></asp>
You can place you id name in place of YourId and can add multiple ids like above
<asp:hyperlink
runat="server"
id="hlReceipt"
navigateurl='<%# String.Format("RecipePage.aspx?id={0}&name={1}", Eval("Receipt_ID"), Eval("Receipt_Name")) %>'
text='<%# Eval("Receipt_title") %>'>
</asp:hyperlink>

Pass parameter from Gridview column to Javascript

I need to pass an argument (which comes from my database) from a radgrid view column to my javascript (which opens a dialog box window). However, I can't put the "bind("Id")" as a parameter from where I call the javascript as href.
In simpler words, I am looking for a way to pass <% Bind("Id")%> to the javascript, OpenMyWindow, call instead of the hardcoded, 111, right now.
<telerik:GridTemplateColumn UniqueName="Meet" DataField="Subject" HeaderText="Meet">
<ItemTemplate>
<div style="text-align: center">
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("Subject") %>' href="javascript: OpenMyWindow(111);" Width="30%">
</asp:LinkButton>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
When I try "OnClick" instead of "href", my popup dialog box closes instantly and doesn't stay opened.
Try using "OnClientClick" and return false from your javascript method to prevent post back.
Alternatively. You could use a method passing in the DataIten. Then output an anchor tag created anyway you like:
<%# formatOpener(Container.DataItem) %>
With code behind of:
protected string formatOpener(object item)
{
ObjectType myObj = (ObjectType)item;
return String.Format("<a href=\"javascript:OpenMyWindow({0});\" width=\"30%\"/>{1}</a>", myObj.ID, myObj.subject);
}
I think you don't need a LinkButton, you could achieve it with an asp:HyperLink (which renders as a a tag):
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("Subject") %>' NavigateUrl='<%# "javascript: OpenMyWindow(" + Eval("ID").ToString() + ");" %>'></asp:HyperLink>
Also, don't use Bind if you don't need it, for displaying purposes always use Eval.
User HyperLink control instead and try setting the NavigateUrl property of the HyperLink this way:
<asp:HyperLink ID="hlLink" runat="server" Text='<%# Bind("Subject") %>' NavigateUrl='<%#Eval("Id", "javascript: OpenMyWindow({0});")%>'>
hope it helps./.
I finally figured out and solved this issue. Actually, somebody told me on the other post that, CommandArgument is completely a server-side property and doesn't render any html attribute. So I can't change any button's attribute and fire click on it. I finally made "Id" come through the code behind and made it work.
aspx code
<telerik:GridButtonColumn UniqueName="Subject" DataTextField="Subject" HeaderText="Meeting">
<HeaderStyle Width="30%" />
<ItemStyle HorizontalAlign="Center" />
</telerik:GridButtonColumn>
Code Behind
var subjectLink = meetingRow["Subject"].Controls[0] as LinkButton;
subjectLink.Attributes.Add("onClick", "javascript: return OpenMyWindow('" + meetingId + "')");

How can we change a linkbutton to a hyperlink with variables?

I'm not very familiar with ASPX, not enough to get my head around this.
Our developers have used this code in a gridview control which opens a new tab and sends the user to a "details" page.
<asp:LinkButton Text='<%# Bind("OrderId") %>' CommandArgument='<%# Bind("FormId") %>' ID="lnkOrderId" runat="server" OnClick="lnkOrderId_Click"></asp:LinkButton>
This code produces HTML that looks like this:
<a id="gvGridView_lnkOrderId_2" href="javascript:__doPostBack('gvGridView$ctl04$lnkOrderId','')">20109</a>
To be able to use a Jquery fancybox control, we need the code to produce the code like this:
<a id="gvGridView_lnkOrderId_2" href=”http://domain.com/Details.aspx?OrderId=20109&FormId=0”>20109</a>
I can see we need to use asp:hyperlink, but what I tried with the variable doesn't work.
Is this possible to do?
I use a TemplateFieldand direct render the link, here is how:
On the GridView aspx page I use:
<asp:TemplateField >
<ItemTemplate >
<%#LinkToGoto(Container.DataItem)%>
</ItemTemplate>
</asp:TemplateField>
and on code behind I make the link as:
protected string LinkToGoto(object oItem)
{
// read the data from database
var cOrderId = (int)DataBinder.Eval(oItem, "OrderId");
// format and render back the link
return String.Format("go to order", OrderId);
}

OnClick for Hyperlink

I'm new to ASP.NET and C# and also new to this forum but here is my problem.
I'm trying to create a image gallery with ASP.NET Webforms(requirement) and C#. I'm using a Listview to show the thumbnails and when you click on the small picture the big version should show above in a img tag for example. But I cant find any "OnClick" code for a hyperlink. Do I have to do that with JavaScript?
Thanks for all help.
here is some of the code:
<ItemTemplate>
<asp:HyperLink ID="ImageHyperLink" runat="server" ImageUrl='<%# Eval("Name","~/files/thumbs/{0}") %>' NavigateUrl='<%# Eval("Name","~/files/{0}") %>'></asp:HyperLink>
</ItemTemplate>
Try taking a look at LinkButton
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(v=vs.110).aspx
<asp:LinkButton runat="server" id="LinkButton1" OnClick="OnClickAction"></asp:LinkButton>
This will allow you to do a server-side method in much the same way an <asp:Button> will do. However will look just like a hyperlink.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="http://stackoverflow.com/">HyperLink</asp:HyperLink>
You will not get on click event for HyperLink, But for navigating on to the another page.
You will get one property which is NavigateUrl
By using this property you can redirect to any page by giving url.

c# code in control attribute in aspx file

I'd like to change the text attribute of a button according to the session landuage. So I write:
<asp:Button id="btOptIn" runat="server"
Text="<% = UI.Instance.TxtSubmit %>"
onclick="btOptIn_Click" />
This does not work. I tried several other versions (like Text='<%# Eval(UI.Instance.TxtSubmit) %>') but could not succeed.
The same code (<% = UI.Instance.TxtSubmit %>) works outside the quotes of the attribute. What is the syntax to make it work within an attribute of a control?
Thank you for your time.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ > can be used to access data in resources files.
In the Page_Load of you will have to make a call to Page.DataBind() for this to work.

Categories