简体   繁体   中英

how to prevent postback using jquery

$(document).ready(function () {
    $("#Div_1").show();
    $("#Div_2").hide();

    $('#Buttion1').click(function () {
        $("#Div_1").hide();
        $("#Div_2").show();
    });
});

in the above code, while loading is working but when click (Buttion1 is ASP.Net Server button) on button Div_2 not showing off and Div_1 not hiding off

 $('#Buttion1').click(function () {

        $("#Div_1").hide();
        $("#Div_2").show();
                 return false;

});

Here is the correct way to do it.

Eg: http://jsfiddle.net/vtLMT/

HTML

<div id="div1">Div1</div>
<div id="div2">Div2</div>
<button id="btn">Click Me</button>

CSS

#div1 {  }
#div2 { display: none; }

JQUERY

$(document).ready(function() {

    $("#btn").click(function() {
    $("#div1").hide();
    $("#div2").show();
    return false;
    });

});

try this

    $(document).ready(function () {
        $("#Div_1").show();
        $("#Div_2").hide();

        $('#Buttion1').click(function () {
            $("#Div_1").hide();
            $("#Div_2").show();
            return false;
        });

    });

or

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Button Text="text" ID="Buttion1" OnClientClick="hideshow();" runat="server"
            OnClick="Buttion1_Click" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Buttion1" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
<div id="Div_1">
</div>
<div id="Div_2">
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $(document).ready(function () {
            $("#Div_1").show();
            $("#Div_2").hide();
        });

    })
    function hideshow() {
        $("#Div_1").hide();
        $("#Div_2").show();
        return true;
    }
</script>

server side code

    protected void Buttion1_Click(object sender, EventArgs e) { 
        //server side code
    }

note:

1)your two div must be out side updatepanel.

2)your hideshow function must not be undefined.Place function on at the en of the page

3)on button client click call hideshow function which return true

4) after javascript function return true..it goes for the server side event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM