I am using asp:hyperlink button to open a Terms and Condition pop up window.
Code for the hyperlink is -
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:window.open('test.aspx');"
ForeColor="#F58022" runat="server">Terms and Conditions
</asp:HyperLink>
When I click this URL in a browser it opens up my test.aspx page. But along with test.aspx it opens up another page:
the URL of the page is: "javascript:window.open('test.aspx');"
the body of the page is: [object]
Can you please suggest me how to get rid of this unwanted page?
Thanks
Use:-
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:window.open('test.aspx'); return false;"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
The problem is that window.open returns a window object. One purpose of the javascript: "protocol" was to allow javascript code to generate HTML content which is return be the expression following the protocol. Navigation then happens to a new page containing that HTML.
In you case because you have Target="_blank" a new page is opened and the object return by your expression (the new window opened by window.open) has its toString() method called and that is what is displayed in this extra window.
Edit:
I've struck the code because it doesn't work. The correct solution is provided by silky. However I'm not deleting the answer because the explanation of what is going on in the question code stands. Hence the solution really is:-
<asp:HyperLink ID="HyperLink4" href="#"
onclick="window.open('test.aspx'); return false;"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
Target no longer need it isn't used. Left as HyperLink control since there may be other reasons the OP needs it as a control on the page.
Is there any reason why you need to use the HyperLink control?
You could probably just use a standard HTML link (or an HtmlAnchor control) instead and use the client-side onclick event to fire your JavaScript:
<a id="HyperLink4" runat="server" href="test.aspx" target="_blank"
onclick="window.open('test.aspx');return false;"
style="color:#F58022">Terms and Conditions</a>
Make it:
NavigateUrl="javascript:window.open('test.aspx'); return false;"
Better practice, however, is to put this in OnClientClick
NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false"
-- edit:
<asp:LinkButton ID="HyperLink4" Target="_blank"
NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:LinkButton >
Updated per comments.
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:void window.open('test.aspx');"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
Remove Target=_Blank, you don't need it, the JavaScript opens a new window already...
Tip: to know why it acts differently, view source in the browser and check what the web control produces in HTML terms.
Related
I am trying to execute some javascript on the click of a asp gridview's hyperlinkfield. The javascript executes fine but the page posts back so when the iframe flickers before showing (The javascript simply sets the iframe's source and then its display from none to block.). How can I go about disabling autopostback on this control since it does not have such an attribute?
<Columns>
<asp:hyperlinkfield Text="Update"
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');"
HeaderText="Update"
target="_blank"/>
<asp:hyperlinkfield Text="Add Findings"
NavigateUrl="javascript:ShowForm('frmAuditFindings.aspx');"
HeaderText="Add Findings"
target="_blank" />
</Columns>
CSS
.ShowMe{display:block;}
.HideMe{display:none;}
Javascript
function ShowForm(urlToGoTo){
document.getElementById('myiFrame').src = urlToGoTo;
document.getElementById('myiFrame').className = "ShowMe";
}
Try using the TemplateFiled and the anchor tag like this :
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">
</ItemTemplate>
</asp:TemplateField>
if you want you can set runat="server" and give it an ID
In your javascript event handler add return false so the browser does not continue the click action, in this case doing the postback.
Of course, ideally, you wouldn't be using <asp:HyperlinkField>. Have you considered using <a runat="server"> instead?
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false"
Try this NavigateUrl attribute in the markup
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false;"
And also you can try basic HTML anchor tag and javascript to open new window.
See if you are not doing anything on postback of these hyper links then what is the purpose to draw server side control.
These affects the performance while rendering on web page. So I would suggest you to use <a> insread of these hyperlinkfields
you can use instead :
<a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">
Postback will never occure in this case and reduce the rendering time and momory cost in comparative to byperlinkfield.
I want to change the source of the iframe from a listbox. And here is my code.
<iframe id="iframeID" frameborder="0" width="100%" height="300px"></iframe>
<asp:Button ID="btnView" runat="server" Text="Pop Up HTML Preview"
OnClientClick="ShowPopUp();" />
ShowPopUp = function() {
var x = document.getElementById('<%=ListBox1.ClientID %>');
var val = x.options[x.selectedIndex].Value;
document.getElementById("iframeID").src = val;
But error says I have an undefined iframe.
What is wrong?
Please help me. Thanks :)
You may change the source using jquery:
$("#content").attr('src', 'http://your new source here');
so you can invoke this method when the selected item in the listbox is changed.
If it doesn't do anything from C#, make a normal button - ASP tends to add fancy prefixes to its controls' IDs (like ContentPlaceHolder1_btnView), and those can't be accessed that way by .ID from C# code. So you can either chceck these IDs (which are able to change if you move them somewhere else) by browser's source code inspector, or use
<input type="button" ID="btnView" Value="Pop Up HTML Preview"
OnClick="ShowPopUp();" />
(Modify your JS accordingly)
use value, JavaScript is case sensitive
var val = x.options[x.selectedIndex].value;
and change OnClientClick as below
<asp:Button ID="btnView" runat="server" Text="Pop Up HTML Preview"
OnClientClick="ShowPopUp(); return false;" />
Button click event make full post back request to server. So again your Ifram will have default src value after postback. Here you have not given value as default src. So nothing will get after click on button. after you add return false button click will not postback after click.
I have a asp label that I need to be able to change according to the code behind. How can I do this?
ASPX: (The first part works correctly only for "TestA#abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>
Code Behind:
public changeLabel()
{
if (//Some Condition Here)
{
this.EmailLabel.Text = "TestA#abc.com";
}
else
{
this.EmailLabel.Text = "TestB#abc.com";
}
}
What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>
And then instead of changing the Text property, change the NavigateUrl property.
You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.
<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>
You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.
This other SO question might also be helpful: How to launching email client on LinkButton click event?
<html>
<body>
<span id="foo"
onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
Click Me!
</span>
</body>
</html>
For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.
Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>
I am using a button that has to be invible and should be used by a javascript function.
<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/
I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?
Thanks in advance :)
A pretty clean approach in ASP.Net it give it a "hidden" class:
<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />
Then in your stylesheet:
.hidden { display: none; }
If you set it to Visible="False" then the code will not be executed.
Instead I think you should wrap it in a <div> and set display:none via css:
<div style="display: none;">
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" />
</div>
adding style="display:none" would hide the button till you make it visible again
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" style="display:none"/>
How about
style="display:none"
for the button instead of Visible = "true".
Can you just use a hidden form field in this case? This is generally the preferred method of passing information along.
See http://www.tizag.com/htmlT/htmlhidden.php
<asp:Button ID="btnMyOrders" runat="server" Text="my orders" CssClass="dropdown-item" OnClick="btnMyOrders_Click" Visible="False" />
Example then in Form(){
btn.Visible = true; to show it again
}
I think the first question you should ask yourself is : why would I put in my HTML doc a button that should not be visible ?
An HTML document should be used ONLY TO DESCRIBE A CONTENT'S SEMANTICS. So an HTML doc should ONLY contain the content you want to publish and extra data to explain the content's SEMANTIC !! NOTHING about the way it is displayed, NOTHING about the way it behaves !!
All displaying and behaviors questions should be managed with CSS and Javascript, NEVER within HTML itself.
Any element needed for Javascript only purpose should be added in the doc by Javascript itself !
You should never find, in an HTML doc, such things as prev/next buttons for a javascript picture gallery for example. The HTML doc should only contain the pictures list, than your JS script will change the way this list is shown, making it a eye candy gallery and add buttons for navigation.
So in your example, your saying you want to add in your HTML doc an invisible button with some Javascript actions on it.... that's probably an example of some content that should never be in the HTML doc.