How to prevent PostBack without using JavaScript? - 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 ="">

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.

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

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

How disable RequiredFieldValidator in script

I have TextBox with RequiredFieldValidator on my page.
I also have link that calls some simple javascript.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" />
<asp:LinkButton ID="Link1" runat="server" OnClientClick="DoSomething(); return false;" CausesValidation="false"Text="Do" />
function DoSomething() {
textbox1.val("blah"); }
When user type something into textbox and then delete that text and focus next control - then validator fires.
Then user can use link that adds text using javascript. TextBox1 text is no longer empty but RequiredFieldValidator still shows error message. How to prevent that?
you can use javascript ValidatorEnable function
ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
I would recommend a CustomValidator here since you do NOT want the normal RequiredFieldValidator behavior. The CustomValidator client method will only run on postback.
The whole point of the validators is that they are to be used for both server side and client side validation.
If you only want to validate on the server side, don't use a validator and simply check that the values are valid and report an error.

Categories