Home > DeveloperSection > Forums > Disable postback on image button click asp
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    December-19-2014 10:33 PM

 ASP.Net C# 
Ratings:


 2 Reply(s)
 712  View(s)
Rate this:

I have an image button on my asp project that browse an image and display. I am using a script for my project.

 

This is my code:

 

ASPX:

 

<asp:Panel ID="stage" runat="server" cssClass="containment-wrapper" style="border:1px solid #000000;">

         <asp:ImageButton ID="imgBrowse" runat="server" Height="375px" Width="640px" src="#" />

         <input type='file' id="inpUploader" style="visibility: hidden;"/>

</asp:Panel>

JS source:

 

function readURL(input) {

    if (input.files && input.files[0]) {

        var reader = new FileReader();

 

        reader.onload = function (e) {

            $('#imgBrowse').attr('src', e.target.result);

        }

 

        reader.readAsDataURL(input.files[0]);

    }

}

 

$("#inpUploader").change(function () {

    readURL(this);

});

CS:

 

protected void Page_Load(object sender, EventArgs e)

    {

        imgBrowse.Attributes.Add("onclick", "document.getElementById('inpUploader').click();");

    }

My code is working and the image is showing after I select the image but after a few seconds the image lost because the page reloads.



lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    December-20-2014 5:44 AM

<asp:Button runat="server" Text="Button" UseSubmitBehavior="false" OnClientClick="alert('my client script here');my" />

 

<script type="text/javascript">

 

function my__doPostBack(eventTarget, eventArgument) {

    //Just swallow the click without postback of the form

}

</script>


Elena Glibart
Elena Glibart

Total Post:35

Points:128
Supported
Posted on    December-20-2014 6:28 AM

just add return false;.

 

 protected void Page_Load(object sender, EventArgs e)

    {

        imgBrowse.Attributes.Add("onclick", "document.getElementById('inpUploader').click(); return false;");

    }


Don't want to miss updates? Please click the below button!

Follow MindStick