Link Button : "right click and open in new tab" not working - c#

I am trying to right click on a LinkButton and open it in a new tab or seperate window page displays nothing. I found some solution for the use of Hyperlink button as it has property to set target to "_blank" but LinkButton has not any target attribue.
I want to use LinkButton instead of hyperlink button, its just because i can't set command arguement or command name on hyperlink button and can't fire an event on it.
<asp:LinkButton ID="lnkHeadingHindi" Text='<%#Patrika.Common.ConvertNews(Eval("strMainHeadingHin").ToString())%>' CommandArgument='<%#Eval("intNewsId") %>' runat="server"></asp:LinkButton>
It would be great if anybody has a solution and let me know in case of any concern.
Thanks !!

Based on the accepted answer to this question, if you want to perform a POST (such as a LinkButton does) but have the result open in a new window, you need to add target="_blank" to the form on the page, not the link itself.
Obviously you don't want to do this when you initially render the page, as everything that caused a postback would open in a new window.
Instead, try adding the following attribute to your LinkButton:
OnClientClick="$('form').attr('target', 'blank')"
This will dynamically set the form attribute just before the form is posted back by a click to the link.
Note that this doesn't give the right-click functionality you want, but it does work to open in a new window on a left-click.
If you don't have access to JQuery, you'll need to do something like
protected void Page_PreRender(object se, EventArgs e)
{
this.Page.Form.ID = "someUniqueID"; // unless your form already has an ID
yourLinkButton.OnClientClick =
"document.getElementById('" +
this.Page.Form.ClientID +
"').setAttribute('target', '_blank')";
}

From the docs
Use the LinkButton control to create a hyperlink-style button on the Web page. The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.
As this isn't actually performing a link in the standard sense, there's no Target property on the control (the HyperLink control does have a Target) - it's attempting to perform a PostBack to the server from a text link.
Depending on what you are trying to do you could either:
1) Use a HyperLink control, and set the Target property
2) Provide a method to the OnClientClick property
that opens a new window to the correct place.
3) In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
If you like when click the link button,then open new window,
please see this

insert base tag like this
<head>
<base target="_blank" />
</head>

If you want to pass some information to the page using hyperlink, pass it in the url using QueryString.
<asp:HyperLink id="hyperlink1"
NavigateUrl="~/MyPage.aspx?intNewsId=10"
Text="ClickMe"
Target="_blank"
runat="server"/>

You can't have it both ways, you'll have to choose between:
1) the linkbutton executing some code in the current page
2) a hyperlink opening another page in a new window
I guess you want to have the linkbutton executing some code in the new page, but that's impossible.
or if u like use . anger tag
Just render an anchor with href set to appropriate url and set the target attribute to _blank it will open the url into new window
<a href="urlOfThePage" target="_blank" >Click me</a>

Related

NavigateUrl and Eval in a button

So I have this line of code which uses a hyperlink
<asp:HyperLink style="text-decoration:none" runat="server" Text="View Order" NavigateUrl='<%# "OrderDetails2.aspx?oID=" + Eval("oID") %>'>/asp:HyperLink>
Works perfectly fine but when I try to use a button to do the same thing (since I have to use an OnClick method), it doesn't allow me to use NavigateUrl and Eval in it. Any alternative methods?
Thats because asp:button does not have a property called NavigateUrl, its only a valid property for an hyperlink.
You can use JavaScript click event for an asp:hyperlink.
Not sure what you're trying to do.
If you are trying to add a link to another page, just use asp:hyperlink but use CSS to make it look like a button.
If you really do want a button with a server-side click event, you can add the following to the end of the click event handler:
Response.Redirect(String.Format("OrderDetails2.aspx?oID={0}", oID));

How would I let 1 .aspx page use another .aspx page's code?

I have 2 aspx files in my project. The first.aspx page has some content on it and when I click on a button, it will launch a frame (second.aspx that only has code to show a calendar) on the same page.
Now once that calendar(second.aspx) loads on first.aspx, I want to click a link on the calendar that will .show() a hidden DIV on the first.aspx page.
How do I access code cross pages? In other words, how can I write some code in second.aspx that will affect first.aspx.
What you're asking for is not really possible. You're probably approaching it the wrong way. What you should do is turn your calendar page into a user control so that it can be used seamlessly in first.aspx.
Here is how to get started with user controls in asp.net:
After you turn it into a user control there are different approaches to getting access to the properties of the user control from your page. Here is one approach using the FindControl method.
Hope that helps.
The easiest solution would be to show and hide your div with jquery. Simple give your div a class like:
<div class="myCalendarDiv" style="display:none" />
And your Button should look like this:
<asp:Button id="myButton" OnClientClick="return ShowCalendar();" runat="server" />
<script type="text/javascript">
function ShowCalendar() {
$(".myCalendarDiv").show();
return false;
}
</script>
Another way would be instead of creating a seprate webpage for the calendar, as proposed you can use a jquery dialog, or make a usercontrol and embedd it on the same page.
(Posted on behalf of the OP).
So since I was dealing with an Iframe, I found out that you can target the parent window which would be first.aspx.
I used "window.parent.MYFUNCTION();" to call my JavaScript function on first.aspx and show the div.

LinkButton or HyperLink?

I have a situation where I'm not sure if I should use a HyperLink or LinkButton. When a user clicks on a list of links I want to trigger a click event where I save some information to session (should use LinkButton) but I also want these links to open up new tabs (should use HyperLink).
A LinkButton will postback, it's essentially a button that renders like a link. You could set a response.redirect(url) in the event handler to set a new tab.
Can you add more information, of what you want to do in the handler, maybe this could be achieved with Jquery calling a server-side method?
Difference between Hyperlink and LinkButton
Click Api with Jquery and Jquery post.
You will need to use a LinkButton which causes a PostBack. To open additional tabs, emit JavaScript.
protected void MyLinkButton_Click(object sender, EventArgs e)
{
Session["MyData"] = 123;
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"newWindow",
"window.open('http://myurl','_blank');",
true);
}
I would say a HyperLink that hits a specific URL to store the necessary session data, then use Response.Redirect to redirect to the following page after storing the information.
The HyperLink's URL would point to your server so that you can store the information, and then you would use a redirect to point the user to the correct endpoint after storing the necessary data.
Example
HyperLink's URL points to ~/yourpage.aspx?state=NY, with target="_blank"
Server responds to URL and checks querystring.
If query string exists, store data (if (Request.QueryString["state"] != null) Session["state"] = Request.QueryString["state"])
Redirect the user to the appropriate URL (Response.Redirect("http://www.ny.gov"))
If the data is at all confidential, then you would want to use the LinkButton methods pointed out in other answers. Opening up a new tab is tricky, so you would probably have to write out some Javascript as outlined in #andleer's answer since I don't believe there is an easy way to pop open a new window from the server side otherwise.
You need to use LinkButton.
The difference between the two is that LinkButton postbacks your page to the server allowing you to make your logic while HyperLink does not postbacks - just redirects you to the specified link, therefore, use HyperLink when you want to navigate.
The LinkButton control is used to create a hyperlink button. This control looks like a HyperLink control but has the same functionality as the Button control.
with LinkButton you also get the facility of Web Control Standard Properties and Control Standard Properties .
You're right, can't do both, since a LinkButton will trigger a postback, and a hyperlink will simply navigate to a new page.
In your case use you can use a LinkButton and the server code will have to to do a redirect (if you want to navigate to another page) or handle the Tabs and return the page with the Tab opened if you are using a tabs element. (so the tab navigation will not be done front end)
In such cases you can use button for saving some information and you can also use it as a like option. But if you want to add a link then you have to use hyperlink.. You can also use javascript to link a url with the button so that when user clicks on that button the session information will get stored and then he will redirected to the new webpage.

LinkButton open new window tab

<asp:LinkButton ID="lnkbtnMoreTagRules" runat="server"
CommandName='<%#Eval("Value")%>'
CommandArgument='<%# string.Format("{0}||||{1}", Eval("Tag"),
Eval("TagAppearance"))%>'
OnCommand="lnkbtnMoreTagRules_Command">Več pravil</asp:LinkButton>
I want to close current window tab and open new one.
How can i open a new window tab with linkbutton. target="_blank" not helping.
As link button is posted back, so it cannot be used for a GET request. instead use HyperLink Class
LinkButton works like a Button and does not have a Target attribute. Use a HyperLink instead and set the Target.
You need something like this
<asp:LinkButton id="LinkButton1" runat="server" onClientClick="window.open('http://asp.net');"
I doubt we have any foolprrof code to open link in tabs because most of the browser activities are totally under the control of users.
User can change thier browser settings in the way they wanted to behave.
There is nice discussion related to this in below thread, it will be interesting for you,
http://www.dynamicdrive.com/forums/archive/index.php/t-19843.html

BulletedList onClick not firing

Ugh, this is driving me mad
Im trying to build up a dynamic menu from a bulletedList, most menu items are plain links however the log out button needs to perform some cleanup code.
I cant for the life of me get the BullettedLists onclick event to fire.
The BulletedList is inside a user control (if that makes a difference)
Any ideas?
Or - any ideas for an alternative, better solution?
Code below
BulletedList
<asp:BulletedList OnClick="menu_Click" runat="server" CssClass="MainMenu" ID="loggedInMenu" DisplayMode="HyperLink" />
Adding an element
loggedInMenu.Items.Add(new ListItem("Logout", ""));
Click handler
protected void menu_Click(object sender, BulletedListEventArgs e)
{
user.logout();
Response.Redirect("Default.aspx");
}
You're using the wrong DisplayMode for your BulletedList control. You should use a DisplayMode of LinkButton. When you use DisplayMode.HyperLink:
Users can click links to move to
another page. You must provide a
target URL as the Value property of
individual items.
This is from the MSDN docs for this control. (It's about 3/4 of the way down the page.)
When you use a BulletedList control in HyperLink mode, the value of your ListItem is the URL that you're navigating to. So your static page HTML controls would use ListItem.Value as the href attribute of the <a> tag.
Here's what the HTML markup looks like when you use a DisplayMode of HyperLink (it's a plain old HTML anchor tag w/ a href):
<li>One</li>
But since you want to postback, you should set the DisplayMode of your BulletedList control to LinkButton. When you do that, you'll enable a postback back to your page and your event handler will trap the event. You can then process the click appropriately then. The event argument that's passed in (of type BulletedListEventArgs) will have an Index property, and that will tell you what item in your list was clicked.
Here's the updated .aspx code that I used:
<asp:BulletedList ID="bullet" runat="server" DisplayMode="LinkButton"
onclick="bullet_Click">
<asp:ListItem Text="One" Value="1">One</asp:ListItem>
</asp:BulletedList>
Everything else is the same except the DisplayMode, which is set to LinkButton. When I use that, then my bullet_Click event handler is fired when I click a list item.
I hope this helps!!

Categories

Resources