Adding a click event to a Hyperlink displayed in ListView VB.NET ASP.NET - asp.net

Is there a way to add a click event to a Hyperlink tag that's render in ListView?
Basically, I have a HyperLink tag that generates a link dynamically and it opens up to a new tab when users click on it. At the same time, when the user click on it, I want to post a text or make the text label visible. Sample code below:
<asp:ListView ...>
<ItemTemplate>
<asp:Label ID="Msg" Text="*You have already accessed this link*" runat="server" Visible="false"/>
<asp:HyperLink ID="label1" NavigatUrl='<%#Eval("Link")%>'Target="_blink" text="Click Link" runat="server"></asp:HyperLink>
<//ItemTemplate>
</asp:ListView>

You can use QueryString. Add the link with a Query Parameter on it and then -
If IsPostBack=True then 'Check if the page is reloading [Because when you clik on the link with the same link, it will reload the page]
'Considering the QueryString will be like - "yourdomain/default.aspx?item=1"
'Check for QueryString
Dim s_ItemId As String = Request.QueryString("item")
if s_ItemId<>"" then
'Do whatever you want
End If

Related

on every postback page opens in a new tab

I have a weird problem in my page. I have a button called Print. I wanted report should come in a new tab, so i wrote some javascript on button's onClientClick. Which works great with no problem at all.
But problem starts now when user comes back on original page again, now here i have several controls which cause postback. Say for example its a dropdownlist. so whenever user changes dropdown item it causes postback which is fine but everytime it opens a new tab on every postback.
hope I am clear in question...
Any help??
Here is a code:
<asp:Button ID="btnshow" runat="server" Text="Show" Font-Bold="true" ForeColor="Black" Width="90px" OnClick="btnshow_Click" OnClientClick ="document.forms[0].target = '_blank';"/>
The issue is document.forms[0].target='_blank' is setting the target on the form not the individual button so a postback triggered by any control will open in a new tab.
You should use a HyperLink control instead of the Button control. The HyperLink control has a Target property which allows you to specify how the link should be opened.
Below is an example taken from the HyperLink documentation. This will render an anchor tag with target="_blank".
<asp:HyperLink ID="lnkPrint" NavigateUrl="http://www.microsoft.com" Text="Print" Target="_blank" runat="server" />

UrlReferrer is null after clicking hyperlink control

I've got my asp:hyperlink control on a page named "Items"
<asp:HyperLink ID="btnAddToCart" Text="Add To Cart" runat="server" Visible="False" />
The codebehind is setting the NavigateUrl for the hyperlink control.
Dim btnAddToCart As HyperLink = CType(e.Item.FindControl("btnAddToCart"), HyperLink)
btnAddToCart.NavigateUrl = "http://fake.com/checkout?productid=123"
When I click the button, the "Checkout" page loads. However, when I check the Request object in my .NET code, the Request.UrlReferrer is empty... and none of the other properties provide detail that the previous page was "Items".
Is my mistake horribly obvious, or is there some other way I should be redirecting so that the UrlReferrer gets populated?

How to remove postback on linkbutton inside gridview?

I'd like to ask on how to prevent post back when a LinkButton inside the GridView is clicked?
My current implementation is that, I have a GridView with customer details and the button link which is the ID link button, once this link button is clicked the customer details will be shown on their respective fields (i.e. textbox, etc), but I want it to look like more interactive and faster when clicking the said button by removing Post back. How can I achieve this? Thanks.
So just to confirm my answer after our comments, replace this...
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="linkView" CssClass="View" Text ='<%# Eval("ID")%>' runat="server"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
...with this...
<asp:TemplateField> <ItemTemplate> <%# Eval("ID")%> </ItemTemplate> </asp:TemplateField>
...where loadViaAjax is a Javascript function which populates your customer fields via AJAX or some other means. Ensure this function returns false to prevent the browser responding to the anchor click.
Please mark this as the answer if it works for you.

Render hyperlink button through gridview

I have a hyperlink button in gridview with the attribute navigateurl = "".
<asp:HyperLink ID="btntest" runat="server" CommandName="Print"
CommandArgument='<%# Eval("rentalid") %>' NavigateUrl="" CssClass ="ImClass orangeButton"
Text="Display/Print Rental Agreement As PDF111" Target="_blank">
</asp:HyperLink>
I have a gridview command that makes a file and give this hyperlink button navigateurl attribute value but the problem is the hyperlink button is not calling gridview command because navigateurl is blank and also how can I render the hyperlink button when it got the navigateurl at the same time?
Use link button instead and do whatever stuff you want to do in command event of a linkbutton.
you can also style a linkbutton like a button.
I had a similar issue.... there are a few ways we came up with, but the best option for us was to use a literal in our grid-view. We set the html in the literal to open in a new window and passed the query strings that we needed. We did this twice, once selecting out the HTML in a stored proc. The other on a Row Data Bound. It sounds like either could work in your situation.
Hope this helps!

ASP.Net Label control's Text property not updating text under button click event

I have developed a small application in SharePoint. I am using custom application page and load .ascx Control over that page under Page_Load() method. In .ascx Control, I have created a small form with text boxes and a button Control. under this button click event, I want to change the text property of label like: lblConfirmation.Text = "Confirmation is OK"; but it did not change the text. It shows nothing because i set text property empty in label Control.
<asp:Label ID="lblConfirmation" runat="server" Text="" ></asp:Label>
Any Idea, what is wrong ?
What are this single quotes in your asp:Label definition ? please check this
<'asp:Label ID="lblConfirmation" runat="server" Text="" ><'/asp:Label>
it must be replaced by
<asp:Label ID="lblConfirmation" runat="server" Text="" ></asp:Label>
Try to debug the code in your visual studio and let us know if you get any error ?
Thanks

Resources