Using an asp.net button to launch html code - asp.net

I want to use an asp.net button to launch an outlook window using the following html.
<a href="mailto:sample#website.com?subject=Insurance Text">
What do I need to do to file html code from my onClick event?

Try this
<asp:Button runat="server"
ID="btn"
OnClientClick="document.location = 'mailto:sample#website.com?subject=Insurance Text'; return false;"
Text="Mail" />

There are two approaches. If you want the standard button, you could use something like this:
<asp:Button ID="MailToButton"
Text="Send Email"
OnClientClick="javascript: navigate('mailto:blah#blah.com'); return false;"
runat="server" />
EDIT 2: Never mind about the UseSubmitBehavior property - I was incorrect. You'll just have to use return false;. Apparently ASP.NET does not render a regular non-submit button. How to disable postback on an asp Button
If you want an anchor tag, you can just use the NavigateUrl property of the Hyperlink tag:
<asp:HyperLink ID="MailToHyperlink"
Text="Send Email"
NavigateUrl="mailto:blah#blah.com"
runat="server" />
You cannot launch Outlook from the standard click event in the code behind, however. The code behind click event occurs on the server, not on the client's machine, so whatever you do it needs to happen on the client's machine either through standard HTML or through javascript.

Why an ASP.NET button?
Just use a simple HTML button.
They are plenty of example on the web. This one should work: using mailto button click event

Related

ASP.NET Button submits page and I can't stop it

I have a simple proof of concept web page. It uses one master page and one content page, the default one, default.aspx. I'm doing some client side debugging with alert boxes. I dragged an asp.net button onto the page and set CausesValidation = false and UseSubmitBehavior = false and yet when I click it the page submits.
What am I doing wrong?
Here is a design time code....
<asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="False" CausesValidation="False" />
Here is Runtime render, wth is it putting in a PostBack?
<input type="button" name="ctl00$ContentPlaceHolder1$Button1" value="Button" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Button1','')" id="ContentPlaceHolder1_Button1" />
--Update--
Thanks Volkan Paksoy, that worked. For those who suggested HTML buttons, that worked too and that is what I used, but I was just curious why the ASP.NET button wouldn't work. It's something I should know and probably something I knew and forgot. Thanks for the help
If you need to use asp:Button and disable submit you can add OnclientClick function such as:
<asp:Button ID="Button1" runat="server"
CausesValidation="False"
OnClick="Button1_Click"
Text="Button"
UseSubmitBehavior="False"
OnClientClick="return false;" />
This should stop the postback. But you can simply use an input too of course. This is just one way of doing it.
If you want to create a button that does not submit the page/postback to the server, you could create one using raw HTML like so:
<input type="button" />

Form button navigation

Is there a good reason why I can't do this
<asp:Button runat="server" ID="Btn1" OnClick='<%# Response.Redirect("wibble.aspx?ID=" + Eval("ID")) %>'/>
I get a compilation error:
The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments
I appreciate I can create a code behind routine to handle the redirect but I would like to be able to create these type of buttons on the fly.
I can use a <asp:HyperLink> and it works fine using the NavigateUrl property, but why can't I do this with a button?
You can use the OnClientClick attribute instead. This will allow you to add Javascript code for the onclick HTML attribute.
<asp:Button runat="server" ID="Btn1" OnClientClick="window.location ='wibble.aspx?ID=<%# Eval("ID")).ToString() %>"/>
You can not do this as this will be rendered as input[type=submit] as html in browser, and for html its not functionality to redirect when you click on button.
If you have such request of having hyperlink in button , ASP.net gives linkbutton control , which will solve your need.
<asp:linkbutton PostBackUrl="~/something.aspx?id=1" runat="server">LinkButton</asp:linkbutton>

link button property to open in new tab?

In my application I have some link buttons there but when I right click on them I cannot (they are in disable mode) find the menu items Open in new tab or Open in new window.
How do I show those menu items?
Code example:
<asp:LinkButton id="lbnkVidTtile1" runat="Server" CssClass="bodytext" Text='<%#Eval("newvideotitle") %>' />
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:
Use a HyperLink control, and set the Target property
Provide a method to the OnClientClick property that opens a new window to the correct place.
In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
Here is your Tag.
<asp:LinkButton ID="LinkButton1" runat="server">Open Test Page</asp:LinkButton>
Here is your code on the code behind.
LinkButton1.Attributes.Add("href","../Test.aspx")
LinkButton1.Attributes.Add("target","_blank")
Hope this will be helpful for someone.
Edit
To do the same with a link button inside a template field, use the following code.
Use GridView_RowDataBound event to find Link button.
Dim LB as LinkButton = e.Row.FindControl("LinkButton1")
LB.Attributes.Add("href","../Test.aspx")
LB.Attributes.Add("target","_blank")
try by Adding following onClientClick event.
OnClientClick="aspnetForm.target ='_blank';"
so on click it will call Javascript function an will open respective link in News tab.
<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" CssClass="bodytext" Text='<%# Eval("newvideotitle") %>' />
This is not perfect, but it works.
<asp:LinkButton id="lbnkVidTtile1" runat="Server"
CssClass="bodytext" Text='<%# Eval("newvideotitle") %>'
OnClientClick="return PostToNewWindow();" />
<script type="text/javascript">
function PostToNewWindow()
{
originalTarget = document.forms[0].target;
document.forms[0].target='_blank';
window.setTimeout("document.forms[0].target=originalTarget;",300);
return true;
}
</script>
LinkButton executes HTTP POST operation, you cant change post target here.
Not all the browsers support posting form to a new target window.
In order to have it post, you have to change target of your "FORM".
You can use some javascript workaround to change your POST target, by changing form's target attribute, but browser will give a warning to user (IE Does), that this page is trying to post data on a new window, do you want to continue etc.
Try to find out ID of your form element in generated aspx, and you can change target like...
getElementByID('theForm').target = '_blank' or 'myNewWindow'
When the LinkButton Enabled property is false it just renders a standard hyperlink. When you right click any disabled hyperlink you don't get the option to open in anything.
try
lbnkVidTtile1.Enabled = true;
I'm sorry if I misunderstood. Could I just make sure that you understand the purpose of a LinkButton? It is to give the appearance of a HyperLink but the behaviour of a Button. This means that it will have an anchor tag, but there is JavaScript wired up that performs a PostBack to the page. If you want to link to another page then it is recommended here
that you use a standard HyperLink control.
It throws error.
Microsoft JScript runtime error: 'aspnetForm' is undefined
<asp:LinkButton ID="LinkButton1" runat="server" target="_blank">LinkButton</asp:LinkButton>
Use target="_blank" because It creates anchor markup. the following HTML is generated for above code
<a id="ctl00_ContentPlaceHolder1_LinkButton1" target="_blank" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton1','')">LinkButton</a>

Double Postback Using IE8

I'm using ASP.NET and I have a save button on webform. When that save button is clicked (ONCE) and I'm using IE8 the event handler is executed twice. If I use compatibility mode it works just fine. In FF everything works just fine. I tested IE8 on both Vista and windows 7 and get the same behavior. IE7 works just fine. Just curious is anyone has had a similar issue.
P.S. I am using an advanced layout system which positions and styles the controls based on a layout definition, so it isn't just a standard throw controls on a page setup.
We are using onserverclick on a button tag instead of using an asp.net button. The solution was to set the type of the button to "button". Before no type was set and I think it was defaulting to submit.
Changed
<button id="button1" runat="server" onserverclick="button1_OnClick" />
To
<button id="button1" runat="server" type="button" onserverclick="button1_OnClick" />
Now I do not get the double post back in IE8.
I just fixed a bug with similar symptoms which was caused by two form.submit() calls being made in the client-side javascript event handler for the button.
It appears the second form.submit() was being ignored by other browers but executed in IE8.
I just found a solution to a similar problem I was having troubles with.
Since none of the previous solutions seemed to help me I thought I'd post my own solution.
The problem:
When using an asp button on a webform and using the OnClientClick to run a custom postback mechanism. The postback seems to happen twice (the second time without viewstate being applied)
e.g. :
function ok(){
docustompostbackthing();
}
<asp:Button runat="server" ID="btnOk" Text="Ok" OnClientClick="ok();" cssClass="btn"/>
The solution:
Add a "return false;" after the js function call in the button's OnClientClick.
Adding it in the function itself doesn't do the trick.
e.g. :
function ok(){
docustompostbackthing();
}
<asp:Button runat="server" ID="btnOk" Text="Ok" OnClientClick="ok();return false;" cssClass="btn"/>
Please look at the html as it is rendered on the page:
Every time it's present
<img src=""/>
double postback can happen, for some browser...
If this is the trouble, you can resolve it setting a default, blank, image for every button
<asp:ImageButton ImageUrl="~/Images/blank.gif"...
Just to add to the number of solutions.
It's not just OnClientClick that has issues, the OnClick event does a very similar thing in IE8 as well.
<asp:Button runat="server" ID="btnOk" Text="Ok" OnClick="ok();" cssClass="btn"/>
Can be updated to the following to avoid the second event being fired off.
<asp:Button runat="server" ID="btnOk" Text="Ok" OnClick="ok();return false;" cssClass="btn"/>
I was using jquery(formobj).trigger(submit) this was changed to
jquery(formobj)[0].submit()
Note I am calling submit directly on the form instead of the jquery method
hope this helps someone
I had this same issue happening in IE 11 and managed to fix it by adding type="button" to the button control.

asp.net and jquery dialog

does anyone know how to use jquery modal dialog in asp.net pages?
http://jqueryui.com/demos/dialog/
I've tried creating a simple aspx page, pasted code from the example (http://jqueryui.com/demos/dialog/), it almost works. The dialog briefly shows up and then disappears. It seems that the page is doing a postback. I'm not sure how to stop it, so that the modal dialog stays up.
I've been using jqModal for modal dialogs. I've set it up to use a standard HTML input button to trigger the dialog, and then you can put a regular asp.net button or other controls inside of the hidden div.
Hope this helps.
I assume you have something like:
<asp:Button ID="popupButton" OnClientClick="showModalPopup();" Text="Click me" runat="server" />
I would try adding "return false;" to the OnClientClick property of your Button:
<asp:Button ID="popupButton" OnClientClick="showModalPopup(); return false;" Text="Click me" runat="server" />
Or if you don't need to access the button in code-behind, simply use a standard HTML button and set it's onclick event attribute:
<button onclick="showModalPopup();">Click me</button>

Resources