3

I would like to have asp:Button and when I click it, I dont want it to do postback but add a new div into . here is my code

    <script type="text/javascript">
        $(document).ready(function (e) {
            $('#<%=btn_comment_add.ClientID %>').click(function (e) {
                $('#comments').append('<div>asd</div>');
                return false;
            });
        });
    </script>

here is the asp.net button

  <asp:Button ID="btn_comment_add" runat="server" Text="Gönder" CssClass="theme04" UseSubmitBehavior="false" />

I can see that code adds the div but it postbacks so it gets deleted. I see the new div for a second only:)

please help me to fix it so it wont postback. Thank you very much people

2
  • I also tried e.preventDefault(); but didnt work Commented Feb 7, 2013 at 16:15
  • Would setting the type="button" not prevent the button from being a submit button? I think by default it renders as type="submit". Commented Feb 7, 2013 at 16:26

5 Answers 5

5

instead of using ASP Button use simple HTML button and assign jquery function that you wish to run,

<input type="button" name="btnname" onclick="MyJueryFunction()" />

and write you jquery as

function  MyJueryFunction()
{
  $('#comments').append('<div>asd</div>');

 }
Sign up to request clarification or add additional context in comments.

4 Comments

Yo dawg... Why is there a click event handler inside a click event handler?
You've obtrusified your unobtrusive JavaScript! :)
But...no. That's not the way you write jQuery. You should use jQuery to set up your click event. Don't do "onclick=".
@aquinas: Does it make a difference if i assign the onclick function initially instead of assigning at runtime? if it does, could you please help me understand.
3

If you don't want it to postback, then why do you have an asp.net button? In any case, you can do this:

  <asp:Button ID="btn_comment_add" runat="server" Text="Gönder" CssClass="theme04" UseSubmitBehavior="false" 
OnClientClick="return false;" />

5 Comments

What else can i use instead of button. i am new to this
Just do <input type=button>. If you don't need it to postback, then there is no reason to make it an ASP.NET server control
What if I am using a Jquery to display a popup from the client side? I just don't want it to postback
@aquinas The button is inside a GridView. How can I use a standard HTML button which can be accessed from code-behind and front end without postback?
If you need to be able to access the properties of button from the backend (e.g., visible or not, etc) then yes, you need to make it a server control.
2

Get rid of the UseSubmitBehavior.

When you have this set to false, ASP.NET will add a client side onclick event which will fire before/after your own javascript event, so no matter what you do within your click event to try and stop it from posting back, it will.

View the source of the page, you will see something like;

javascript:__doPostBack(&#39;btnID&#39;,&#39;&#39;)

On the button

3 Comments

But i am using btn_comment_add.clientid() and still doesnt work
What do you mean you are still using btn_comment_add.clientid()? Just remove the UseSubmitBehaviour attribute from the button and don't change anything else.
İ decided to use regular html button to get rid of the complexity. thank u very much
1

there are several ways you can achieve this. Following is another way

    $("#<%=ButtonClick.ClientID %>").bind('click', function (e) {
         e.preventDefault();
         //Do Your work 
    });

Comments

0

IN THIS CASE ASP BUTTON HAVE TO CALL FUNCTION WITH SOME VALIDATION //ASP BUTTON

asp:Button ID="btnSave" runat="server" Text="Save" class="btn btn-success" OnClientClick="return Valid()" OnClick="btnSave_Click"

function Valid() {

        if ($("#ctl00_ContentPlaceHolder1_ddlLocation").val() == 0) {
            alert("Please select Location");
            $("#ctl00_ContentPlaceHolder1_ddlLocation").focus();
            return false;
        }

        if ($("#ctl00_ContentPlaceHolder1_txtArea").val().length == 0) {
            alert("Please Type Area");
            $("ctl00_ContentPlaceHolder1_txtArea").focus();
            return false;
        }
        return true;
    }

Comments

Your Answer

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