ASP.NET URL Navigation - asp.net

I have asp.net button and on it's button click I am redirecting it using
Response.Redirect ("SomeURL.aspx");
I am not passing anything to SomeURL.aspx. Can this be achieved without a roundtrip to the server?

You could use an html anchor tag. This is the simplest approach and probably the best since anchors are the proper control to allow navigation.
My link
If you still want to use the asp.net button you could do something like this
<asp:Button runat="server" ID="myButton"
OnClientClick="window.location.href='SomeURL.aspx'; return false;"
Text="Submit"></asp:Button>

You can try with this code - based on Javascript Navigate
window.navigate("SomeURL.aspx");
Sample
<input type="button" value="Navigate to SomeURL" onclick="funcNavigate();">
<script language="JavaScript">
function funcNavigate() {
window.navigate("SomeURL.aspx");
}
</script>

Can this be achieved without a roundtrip to the server?
Not using code behind.
However, you could wire up a client side click handler or use a hyperlink to accomplish the same thing.
<button onclick="window.location='SomeURL.aspx'; return false;">Some URL</a>
or
Some URL
The hyperlink is the simplest answer.

Related

The server tag is not well formed for repeater C#

I try to open popup from linkbutton inside repeater. Here is my code:
<asp:Linkbutton cssclass="blue-button" id="LinkbtnPrint" runat="server" CommandName="PrintItem" CausesValidation="false" OnClick="javascript:dnnModal.show('<%# DotNetNuke.Common.Globals.NavigateURL("RptAppendixI","doanhnghiepid",Request.QueryString["doanhnghiepid"],"NamBC",NamBC2.ToString(),"ThangBC",ThangBC2.ToString(),"mid",Moduleid.ToString())+"?popUp=true" %>',false,580,950,false)">
<i class="fa fa-print" aria-hidden="true"></i>
</asp:Linkbutton>
I get the error message: "System.Web.HttpParseException: The server tag is not well formed.".
How can I fix this problem?
Its probably because you're mixing two different technologies, if you're using javascript to handle the request and the way you can avoid the exception is by removing the runat="server" attribute. After that you can handle the command that you're defining in the linkbutton in ajax call to the method from javascript, also there is this. I am not sure what are you trying to do while opening the modal but you can simplify it by creating a function inside the javascript that would handle those values instead of passing them straight away from the click event calling both js framework and dotnet framework at the same time.
OnClick="javascript:dnnModal.show('<%# DotNetNuke.Common.Globals.NavigateURL("RptAppendixI","doanhnghiepid",Request.QueryString["doanhnghiepid"],"NamBC",NamBC2.ToString(),"ThangBC",ThangBC2.ToString(),"mid",Moduleid.ToString())+"?popUp=true" %>',false,580,950,false)
if this doesn't help you could try to remove code above and do ajax call to navigate the url that you're calling on click.

Prevent page refresh in ASP.NET

I have the following code in my aspx file:
<button type="button" id="btnAskQuestion" runat="server" onserverclick="btnAskQuestion_Click">Ask Question</button>
I've tried every combination of onclick="return false;" and onclick="preventDefault()" I can think of, including putting them in the javascript function that gets called. Everything I try has one of two results: either I get a postback, or the server side code (btnAskQuestion_Click) never executes.
Any idea what I might be doing wrong?
You cannot execute server-side code this way, using onserverclick causes postback.
If you wish to prevent full page refresh and still execute server-side code, you have to call a client-side JS function via onclick and execute an AJAX call from there.
Another alternative is to use your button as a trigger for UpdatePanel - this way only partial postback will be performed.
Try using the property UseSubmitBehavior="false" in the button markup.
or you can use a "trick" :
Markup
<button onclick="myFunction()">Click Me!</button>
<div style="display:none">
<asp:Button runat="server" id="btnButton" .../>
</div>
js
function myFunction()
{
if (true statement)
$("[id$=btnButton]").click();
else
alert("false");
}
What this does is that you handle stuff with normal markup and do the logic using js. And you can trigger a click of the button that do something in the server.
There're OnClick, that fires on server and OnClientClick that fires on client browser. You should do this:
<asp:Button ID="btnAskQuestion" runat="server"
OnClick="btnAskQuestion_Click"
OnClientClick="return myfunction();">Ask Question</asp:button>
If myFunction returns true, then you will have a postback to the server.
My answer is appropriate only for ASP:Button, not the button control you are working with. Given the choice, I'd switch to ASP:Button.
You're looking for OnClientClick. If you put your JavaScript code there, it will kill the PostBack before it can hit the server.
On the other hand, if you're looking to execute server code without a PostBack, that's impossible. The PostBack is what triggers the server to act.

Is it possible to replace an asp:button with a HTML element

I'm using asp forms and wanted to know if it's possible to replace the standard buttons with HTML elements that are styled using CSS.
My login page uses a standard button
<asp:Button ID="LoginButton" runat="server" Text="Login"
onclick="LoginButton_Click" />
linked to code behind (C#) which performs the login check.
I've seen some nice buttons implemented using the HTML <button> element and styled with CSS which can have features such as images and roll over highlighting. The basic HTML looks like this
<button type="submit" class="positive" onclick ="...">
<img src="/icons/tick.png" alt=""/>
Login
</button>
I've seen another question discussing the Difference between asp:button and html's button so I understand the <button> element is not a drop-in replacement but I'd like to know if the asp:button can be replaced and still call the LoginButton_Click C# code behind?
EDIT:
Although I'm using ASP I don't mind using some client side javascript if necessary.
The buttons I saw which got me thinking about this were found here: Rediscovering the Button Element
EDIT 2:
I tried the answer from XIII using the LinkButton asp control and that worked, rendering the button as I wanted and activating the C# when clicked
<asp:LinkButton ID="LoginBtn" CssClass="button positive"
OnClick="LoginButton_Click" runat="server">
<img src="/icons/tick.png" alt=""/>
Login
</asp:LinkButton>
Javascript is inserted in to the page (as mentioned by Curt) which was not a problem for me but may be for other people; but since the asp:loginview and other controls associated with forms authentication already need javascript I'm not sure this is a problem with the solution.
I decided to accept jwiscarson's answer as this is a cleaner implementation and, despite what I thought, <button> can be a drop-in replacement for <asp:button>
The answer to your question:
if the asp:button can be replaced and still call the LoginButton_Click C# code behind?
is yes. If you have a button like:
<button type="submit" id="submit" class="positive" runat="server">Submit</button>
The attribute you need to set is not onclick, but onserverclick. You could also do something like:
protected override OnInit(EventArgs e)
{
submit.ServerClick += new EventHandler(submit_ServerClick);
}
If you need to do styling on that button, I think the best way to tackle that is via CSS classes like you have in your example.
An alternative approach would be to make use the LinkButton control and style that completely with CSS. We used to do so for a certain project in the past. Worked out pretty great for our customer.
The property of interest if CssClass
You may set CSS class via cssClass property of <asp:Button/>. However you may set runat="server" and onserverclick="LoginButton_Click" attribute to <button/>.
You could use HTML button if you desire, and learn how to call the __doPostBack() method with the proper arguments. Asp.Net buttons and HTML buttons are pretty much the same when it comes to the way they are rendered in the client.
As had been posted here already you could style the HTML rendered by your asp:button or use another asp control. Your asp:button will be rendered as a <input type="submit"> with possibly more limited CSS options than a <button> tag.
From some googling I think it is possible to get a <button> tag rendered but it looks like a non trivial excercise see How can I use the button tag with ASP.NET?

Access to events?

i have a question about generating images at runtime which are also links. Ok, so what i was going to do was create an ImageButton, and then set the onClick event to something such as:
window.open('http://www.themagicfinger.com/')
Which means it would look like:
newImage.Click += (window.open('http://www.themagicfinger.com/'));
But then i realised that this doesnt work, because it looks for a method which matches the event delagate.
So my question is, is this the best way to achieve what i want, and if so, how can i make it work?
Another option that came in my head would be to wrap the image in an tag, but i think this way its better.
Thanks
What you need is OnClientClick:
<asp:ImageButton id="ImageButton1" runat="server" OnClientClick="window.open('http://www.themagicfinger.com/'); return false;" ImageUrl="MyButton.png" />
This property is a string, not event, and you can also assign it from within the code behind:
ImageButton1.OnClientClick = "window.open('http://www.themagicfinger.com/'); return false;";
If you're asking how to use server-side code to tell the client to open a browser window, you can't. The server-side code has no means of telling the client to do that. The only thing you'd be able to do from server-side code is emit client-side that does this. And in that case, why use the server-side code at all?
Using jQuery for example, you can attach the click event like this:
<!-- page header (including loading jQuery), other stuff that's before the image, etc. -->
<img src="/path/to/image" alt="some text" id="imgOpenWindow" />
<!-- the rest of the page -->
<script type="text/javascript">
$(document).ready(function() {
$('#imgOpenWindow').click(function(){
window.open('http://www.themagicfinger.com/');
});
});
</script>
If for some reason you need to use an ImageButton control for this then it would look like this:
<!-- page header (including loading jQuery), other stuff that's before the image, etc. -->
<asp:ImageButton run="server" id="imgOpenWindow" ImageUrl="/path/to/image" AlternateText="some text" />
<!-- the rest of the page -->
<script type="text/javascript">
$(document).ready(function() {
$('#<%= imgOpenWindow.ClientID %>').click(function(){
window.open('http://www.themagicfinger.com/');
});
});
</script>
But I'm not sure what the behavior would be with the post-back. It might override the JavaScript handler (by posting back before your handler is executed) or post-back after the window opens, etc. I would consider that behavior to be undefined and unreliable. An Image control will do the trick, though, if you don't need the button post-back capabilities.

ASP Net - How to open up a form in a new window in ASP.NET

I am working on ASP.net using VB.net. I need to load a page in a new window on button click.
Can someone suggest the code for the same.
If you do not need a postback to the new window, then this:
<input type="button" value="buttonName" onclick="window.open('[page]')" />
OR
<asp:button text="buttonName" onclientclick="window.open('[page]');return false;" />
Else you will have to set the 'target' attribute of the form to something e.g. "_blank"
You could try using this javascript within the Button.Attribute.Add method:
Button1.Attributes.Add("onclick", "window.open('mySite'); return false;");
You can remove the 'return false' if you want the button to continue its postback.
You can customise the javascript further. Parameter information can be found here:
http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
You should do that with javascript, for example using window.open().

Resources