简体   繁体   中英

ASP.NET page not going to server after button click

I have an asp.net page that I use in popup. This pop up basically contains two image controls and markup contains the JCrop plugin. From a page, the popup opens and hence the page loads. I am reading image in bytes, converting to base64 and setting it to the src attribute of both the image controls(both have runat='server'). There are 2 buttons in that page. But the click on these buttons are not hitting any event handler like page_load, button_clicked etc.

<img id="target" runat="server" alt="Main Image" />
<img id="imgCropped" runat="server" alt="Preview Image" class="jcrop-preview" style="border-color:gray" />

protected void Page_Load(object sender, EventArgs e)
    {
        strMIMEType = Session["MIMEType"].ToString();
        strImageData = Session["ImageData"].ToString();
        strImageName = Session["ImageName"].ToString();
        if (!IsPostBack)
        {
            string sTemp = "data:" + strMIMEType + ";base64," + strImageData;
            target.Src = sTemp;
            imgCropped.Src = sTemp;
        }
    }

Below code is when user selects a image file and hit UploadClick button.

protected void btnUploadclick(object sender, EventArgs e)
    {
        HttpPostedFile objfile = AsyncUpload.PostedFile;
        if (AsyncUpload.HasFile)
        {
            if (!IsImageValid(objfile))
                ClientScript.RegisterClientScriptBlock(this.GetType(), "ImageNotValid", "alert('Image format is wrong. Please upload JPEG, PNG or GIF images.')");
            else
            {
                Session["ImageData"] = Convert.ToBase64String(System.IO.File.ReadAllBytes(objfile.FileName), 0, System.IO.File.ReadAllBytes(objfile.FileName).Length);
                Session["MIMEType"] = objfile.ContentType;
                Session["ImageName"] = AsyncUpload.FileName;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "OpenPopUp", "window.open('ImageCropPopup.aspx', 'CropImage', 'height=450,width=700,left=350,top=170,resizable=no,scrollbars=no,toolbar=no,status=no');", true);
            }
        }
        else
            ClientScript.RegisterClientScriptBlock(this.GetType(), "ImageNotUploaded", "alert('Please select an Image.')");
    }

It does not hit page_load at all when I click any of the button. Please help.

Once you place an ASP.NET server control button in a jQuery popup you lose the click event in the main page. You have essentially removed the button from the DOM at that point. You can try hiding your server button with jQuery on the main page, replacing the server button with a standard html button then click the server button with the .trigger event from jQuery.

Like this:

$("#yourHtmlButtonID").click(function(){

   $("#yourServerButtonID").trigger("click");

 });

Just remember to hide your server button with jQuery and not ASP.NET's property of Visible = false because it won't get rendered to the page if you do.

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