繁体   English   中英

单击按钮后,ASP.NET页不会转到服务器

[英]ASP.NET page not going to server after button click

我有一个在弹出窗口中使用的asp.net页面。 该弹出窗口基本上包含两个图像控件,而标记包含JCrop插件。 从页面打开弹出窗口,因此页面被加载。 我正在读取以字节为单位的图像,转换为base64并将其设置为两个图像控件的src属性(都具有runat ='server')。 该页面上有2个按钮。 但是,单击这些按钮不会击中任何事件处理程序,例如page_load,button_clicked等。

<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;
        }
    }

下面的代码是用户选择图像文件并单击UploadClick按钮时的代码。

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.')");
    }

当我单击任意按钮时,它根本不会达到page_load。 请帮忙。

一旦在jQuery弹出窗口中放置一个ASP.NET服务器控件按钮,您就会在主页中丢失click事件。 此时,您基本上已经从DOM中删除了该按钮。 您可以尝试在主页上使用jQuery隐藏服务器按钮,将服务器按钮替换为标准html按钮,然后使用jQuery中的.trigger事件单击服务器按钮。

像这样:

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

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

 });

只需记住用jQuery隐藏服务器按钮,而不要隐藏ASP.NET的Visible = false属性,因为如果这样做,它将不会呈现到页面上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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