Calling javascript function from button hides FullCalendar in my ASP.net - javascript

I have an C# ASP.net website that is using FullCalendar (http://arshaw.com/fullcalendar/).
I want to grab all the calendar events and save them to my SQL database. I have added the following button which calls a javascript function:
<script type="text/javascript">
function save() {
alert('save');
//this will save to database...
}
</script>
<asp:Button ID="btnSave" runat="server" OnClientClick=" return save();" Text="Save" />
I notice that when I click the button the message appears but then the whole calendar just disappears from my page, why is this? and how can I prevent it.

It sounds like you're wanting to keep the page from submitting with this button. Adding return false; to your save() function will prevent the postback from occurring. If that is not right and you have a server-side click event for this button, in it, you will need to emit the javascript needed to recreate the calendar.

Use a HTML input button
<input type="button" id="btnSave" runat="server" Value="save" onclick="save()" />

Related

How to use many buttons inside the form of asp.net c# having different functionality

This is my first ASP.NET web app and every form without <form runat="server"></form> tag is not running plus all element should be inside form tag. Now I have some buttons links for some other pages of my project.
If I use following code it opens new link on another page instead of same page:
<asp:Button ID="ButtonAttendance" OnClientClick="window.open('AttendanceForm.aspx','AttendanceForm')" runat="server" Text="Attendance"
CssClass="btn" /><br />
And if I use following code then every buttons acts after filling the form elements only:
<asp:Button ID="ButtonAttendance" OnClick=ButtonAttendance_Click() runat="server" Text="Attendance"
CssClass="btn" /><br />
Code behind:
protected void ButtonAttendance_Click(object sender, EventArgs e)
{
Response.Redirect("AttendanceForm.aspx");
}
My requirement is when click on any button we should redirect to related page directly on the same page without filling any form elements. How it is possible?
Your code behind looks fine.
If you are using a webform you have to put your controls within the form tags for the postback to work correctly and raise the OnClick event on the sever side.
<form runat ="server">
<asp:Button ID="ButtonAttendance" OnClick=ButtonAttendance_Click() runat="server" Text="Attendance"
CssClass="btn" /><br />
</form>
If you don't need to have the server set values or interact with the buttons on the page just make them regular HTML buttons and use the onclick event for JavaScript to do the redirection.
<button id="btnAttendance" onclick="window.location.href='AttendanceForm.aspx';return false;" class="btn">Attendance</button>
instead of window.open use window.location it will open in the same age
window.location='AttendanceForm.aspx'

Why does submit button have a Webresources.axd?

I have a very basic submit button without a postbackurl specified but it creates a webresources.axd reference. I did see Why is my ASP.NET page injecting this WebResource.axd Javascript file? but I don't have postbackurl in the statement.
<form method="post" action="/thispage" id="formAlpha">
<asp:button ID="cmdSubmit" text="Submit" runat="server" />
</form>
Remove the button, there's no axd. Is there something else that could be causing it? I don't use JQuery.

HTML button causing unwanted page load

I feel with this particular problem I have to explain how my webpage works, otherwise it might not make sense to anyone. The page will load, you will be given some text and the option to click on a asp.net combobox filled with options, when you select one of them, the page will reload and get all the information in relation to that user and display it in a dynamically created HTML table.
After this there are two divs that runat server and dependent on if the user has uploaded a file will display an option of changing the file, or adding a new one.
I have a standard html button that when is clicked is meant to run my JavaScript function, but when you click on the button it fires my aspxcombobox code again. I will attach all my code that I feel might be useful.
HTML for DIVS
<div id ="upload" runat = "server" visible = "false" class="default">
<asp:Label ID="uploadlbl" runat="server" Text="Upload driving licence" style="color:Red"></asp:Label>
<dx:ASPxUploadControl ID="LicenceUpload" runat="server">
</dx:ASPxUploadControl>
<dx:ASPxButton ID="UploadButton" runat="server" Text="Upload" style="margin-left: 2px">
</dx:ASPxButton>
</div>
<div id="ChangeUpload" runat="server" visible="false" class="default">
<span>Licence already uploaded</span>
<br />
<button id="changeLicence" onclick="changeLicence()">Change Licence?</button>
</div>
JAVASCRIPT
function changeLicence() {
alert('test');
}
It's a submit button. It submits the form it is in. Since you are using ASP.NET, the entire page is probably in a form.
Add type="button" to make it a non-submit button.
default behavior of button is submit, Add type="button" attribute so that it doesn't submits form.
<button
type="button"
id="changeLicence"
onclick="changeLicence()">Change Licence?</button>

How to prevent PostBack without using JavaScript?

I have an asp button in default.aspx:
<asp:Button ID="Button1" runat="server" Text="Button" />
And there is a procedure in default.aspx.vb:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Panel1.Controls.Add(New LiteralControl(Date.Now))
End Sub
And teacher asked me, how to make exactly same operation (when click on button, shows up a date) but without postback and without javascript code. I could make a java code there and use OnClientClick="return false" but it seems not right
Refer The Client Side of ASP.Net Pages
You can't do. The default behaviour of Button(input type=submit) is to submit/post the form to the url specified in the form's action attribute.
If you want to prevent default behaviour you need to write a javascript return false
<asp:Button ID="btn" OnClientClick="return false" runat="server" Text="Button" />
By Default asp.net post the form to the same page itself. We say this as PostBack. See the form tags action value from rendered html in browser , it will be the same page name
<input type =submit name ="btn" id="btn"
onclick="javascript:__doPostBack('btn','')"value ="Button">
The following built in javascript code does this
<script>
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
Your page will have the below fields added automatically, inorder to detect which event needs to be fired in server side. you can access simply in a request parameters like Request.Params["__EVENTTARGET"]
<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">

ASP.NET/JavaScript - Why Isn't "Return False" Not Preventing Postback?

I feel like i've done this scenario plenty of times, and it usually works, so im obviously missing something.
Here's my server-side ASP.NET Button:
<asp:Button ID="btnFoo" runat="server" Text="Foo" CssClass="button foo" OnClientClick="foo_Click();" />
Which get's rendered on the client as:
<input type="submit" name="reallylongclientid" value="Foo" onclick="foo_Click();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(reallylongclientidandpostbackoptions, false, false))" id="reallylongclientid" class="button foo">
No surprises there.
Here's the surprise, in my JavaScript function:
function foo_Click() {
return false;
}
Okay so there's more to it than that, but i cut it down to prove a point.
When i click the button, it calls the client-side function, and returns false.
But it still posts back to the server, why?
I basically want to do this on the click of the button:
Do client-side validation.
If validation passes, post back
If not, show some error messages on the form.
Of course, i could change this to an client-side button (input type="button") and manually kick off the postback when i want, but i shouldn't need to do that. Or should i?
write return statement so when you click on button it return false which not allow to submit form
<asp:Button ID="btnFoo" runat="server" Text="Foo" CssClass="button foo"
OnClientClick="return foo_Click();" />

Categories