Why does submit button have a Webresources.axd? - javascript

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.

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'

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

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()" />

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 ="">

Why does .NET render javascript for a button when there's a custom validator on the page?

I've got two questions - first of all, why does .net render a javascript onclick event for asp buttons when there's a custom validator on the same page, and secondly, how can I get rid of the javascript?
It works fine when javascript is turned off, so I don't know what the point of it is. Here's a mini example:
<form id="form1" runat="server">
<asp:Button ID="Button1" OnClick="Button1_Click" Text="test" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="Stuff_Validate" EnableClientScript="false">
</asp:CustomValidator>
</form>
This will generate the following html for the button:
<input type="submit" name="Button1" value="test" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />
Without the custom validator, it's:
<input type="submit" name="Button1" value="test" id="Button1" />
Any ideas?
Thanks,
Annelie
If you look at what System.Web.UI.WebControls.Button does you can see why:
In GetPostBackOptions(), if you have any validators on the page (in the current validation group), then it will generate a script for the postback.
Then in AddAttributesToRender(), if any scripts are present, they are rendered out in the onclick event.
If you need to get around this, then you are going to have to build a custom Button
The javascript is added because the custom validation is done via javascript. It is meant to be there. How do you think the validation is done? If you got rid of the javascript the validation would not work.

Categories