Form button navigation - asp.net

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>

Related

ASP.NET Webforms OnClientClick without enter submission

I have the following command button:
<asp:Button Text="Insert Value" UseSubmitBehavior="false" runat="server" OnClientClick="return Confirmation.value('insert');" CommandName="Insert" />
I am using UseSubmitBehavior="false" to prevent ASP.Net page enter key causing post back
I do not want to listen to the enter keyCode via javascript because enter is used to submit non-webform elements not related to the form
Apparently, when using a Command and UseSubmitBehavior="false" then OnClientClick doesnt work. If I turn on submit behavior it works as expected but then hitting enter on the page automatically tries to click the button.
I prefer not to listen for the click event in Jquery or in javascript, and prefer a webform solution. Possible a better way of prevent enter from submitting the form or a way for OnClientClick to work properly with no submit behavior
You may need to use Panel and keep all your form inside Panel Tag and set the DefaultButton value of Panel to Button. Like Below:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
// other form elements
<asp:Button Text="Insert Value" ID="Button1" runat="server" OnClientClick="return Confirmation.value('insert');" />
</asp:Panel>

Using an asp.net button to launch html code

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

Postback using button in GridView

I have the following in an ASP.NET GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDown" runat="server" ToolTip="Down" CommandName="Down"
ImageUrl="~/images/arrow.png" Width="16px" Height="16px"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
It's simply an image button that when clicked raises the RowCommand event for the GridView. It is very convenient in that it allows me to use the CommandName and CommandArgument in order to determine the button that was clicked and for which row it was clicked.
However, I find that the .NET ImageButton is limited in that it can't display CSS sprites in order to change the image on mouseover. At least I couldn't get it to work, but of course that doesn't preclude some manner in which it's possible.
Anyway, I'd like to use a standard HTML button tag because I know how to get it to work with my sprites.
<button name="btnDown" id="btnDown" type="submit" class="downArrow"></button>
But what I don't know how to do is get the button to cause a postback and raise the RowCommand event and still allow me to somehow access the CommandArgument and CommandName parameters.
Right I would make the button a server control first, then use its various properties as an alternative to commandName and CommandArgument. Additionally I would use OnServerClick event in place of rowCommand. I would use at last the sender object parse it to an hmt button and extract the values out of its properties. I think I'vent got lost.
<button runat="server" name="Down" id="btnDown" title="<%# Container.DataItemIndex %>" type="submit" class="downArrow" OnServerClick="ClickMe"></button
Have you considered using a button field to perform the action you wish to do?
Take a look at the following link
ButtonFields within a GridView
Just use (original post here):
<asp:LinkButton ID="LinkButton1" runat="server">
<div class="sprite-box_mac_osx_disc_button" />
</asp:LinkButton>
Of course use your own css with all width, height, background and background-position. And add Command and CommandArgument to LinkButton.

Displaying Asp.net AJAX Calendar Extender Two Ways

Is there a way to set up the calendar extender so that the calendar displays when the text box recieves focus AND when the when the element with the "PopupButtonID" is clicked? With my current settings it seems to be one or the other.
It's a bit of an ugly way to do it, but you can do this if you're prepared to use two extenders.
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ID="CalendarImageButton" ImageUrl="~/date_16x16.gif" />
<ajaxtoolkit:CalendarExtender runat="server" id="Extender1" TargetControlID="DateTextBox"/>
<ajaxtoolkit:CalendarExtender runat="server" ID="Extender2" TargetControlID="DateTextBox" PopupButtonID="CalendarImageButton" />
This way the calendar will appear whether you focus on the textbox or click the imagebutton.
Not that I'm aware of; it's one or the other. The only way I can think of is set it to use the popup control, then add a JS event handler for the textbox focus, and manually find the calendar extender, and there may be a show() method so you could manually invoke the trigger potentially. Not 100% sure. To find out do:
function textboxFocus() {
var c = $find("<%= calextenderid.ClientID %>");
//can use firebug to see if c.open method exists, or check for something else
}
Again, never done it, so not 100% sure.
HTH.
Use the CalendarExtender's show() method, referencing it by BehaviorID:
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ImageUrl="~/date_16x16.gif" OnClientClick="$find('Extender1').show();return false;" />
<ajaxtoolkit:CalendarExtenderrunat="server" id="Extender1" BehaviorID="Extender1" TargetControlID="DateTextBox"/>

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>

Resources