0

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

6
  • 3
    You teacher wants something to happen without writing code (server-side or client-side) :? Commented Oct 10, 2013 at 10:53
  • Use partial rendering in update panel. Check this for sample msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx Commented Oct 10, 2013 at 11:01
  • @Pratik That uses javascript. Commented Oct 10, 2013 at 11:02
  • @Curt Completely agree but it's just another suggestion as there is no manual javascript coding as its internally generated. In true sense without postback and without javascript Adarsh comments works. Commented Oct 10, 2013 at 11:10
  • 1
    UpdatePanel will not only use JavaScript, but will also do a postback using Ajax to get the updated content for the panel. Maybe it's what the teacher wants - but it's not really true that no postback or JS is used. Commented Oct 10, 2013 at 11:20

1 Answer 1

2

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 ="">
Sign up to request clarification or add additional context in comments.

3 Comments

Or just <input type='button' />
what about scriptmanager and updatepanel? Can I use them to prevent postback?
@JohnSmith Updatepanel internally uses ajax for postback so no postback is not possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.