OnClick for Hyperlink - c#

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.

Related

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);
}

how to pass image from one page to another in asp.net

I get image by image-handler in first page and how to pass into second page
The .aspx page is like
<asp:Image ID="Image1" runat="server" Height="250px" Width="290px"
ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("idnews") %>' />
Just pass the name of the image on the second page and set the same on the second page.
Try this code and implement according to your code
<asp:HyperLink
ID="lnkImage" runat="server"
ImageUrl='<%# Eval("productid","~/Handler.ashx?productid={0}") %>'
NavigateUrl='<%# Eval("productid","ProductLarge.aspx?productid={0}")' />
and markup in ProductLarge.aspx should be,
<img src='Handler.ashx?productid=<%=Request["productid"] %>' alt='Large Image' />
You are fetching image from database, so its not very big deal. You can just pass the Image id via Query string or other method and can display image as u display it on previous page.

hyperlink in gridview error

<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..

ASP NET image onmouseover not working

I have 2 images in my page. What i want to do is, when i move the mouse over 2nd image, the first image will change to other image, but something is not working.
Code:
<asp:Image ID="imgProduct" runat="server" ImageUrl="~/Images/1.png" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/2.png" onmouseover="imgProduct.src='Images/2.png';"/>
If i change the ID in onmouseover="imgProduct.src= to any image id in masterpage, that image is changing correctly, but its not working in the default page.
Any suggestions?
On Page_Load event add this line.
Image2.Attributes.Add("onmouseover",imgProduct.ClientID+".src='Images/2.png'");
Try this instead :
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/2.png"
onmouseover="javascript:document.getElementById('imgProduct').src=this.src;" />
Good Luck !!

Categories