如何上传图像文件中没有任何ASP.NET回发 [英] how to upload an image file without any postback in ASP.NET

查看:118
本文介绍了如何上传图像文件中没有任何ASP.NET回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是&LT上传文件; ASP:文件上传> < ASP:按钮> 控制但我想这样做不回发。按钮点击我执行下面的code。

 保护无效btnUpload_Click(对象发件人,EventArgs的发送)
{
    字符串strFileName = Path.GetFileName(FileUpload1.FileName); // fileupload1是在< ASP:文件上传ID
    FileUpload1.SaveAs(使用Server.Mappath(〜/ UploadFile /+ strFileName +));
    imgUpload.ImageUrl =../Uplo​​adFile/+ strFileName +; // imgupload是在< IMG ID上我显示上传后的图像
    imgUpload.Visible = TRUE;
}

上传文件,我表示从我的项目解决方案的指定文件夹中保存的图像后,但在点击上传按钮在整个页面被加载,我不希望在点击上传按钮回发。


解决方案

 只是添加脚本    < SCRIPT LANGUAGE =JavaScript的>
        功能改变(OBJ){
             __doPostBack(&下;%= btnUpload.C​​lientID%gt;中,);
        }      < / SCRIPT>
调用脚本在你这样的按钮单击事件
< pre>
的onchange =更改(本);
< / pre>您的图像控制与的ContentTemplate更新面板添加    < ASP:文件上传ID =imgFileUploader=服务器/>
             < ASP:按钮的ID =btnUpload=服务器
         文本=上传的onclick =btnUpload_Click的onchange =更改(本); />
      < BR />
        < BR />
     < ASP:的ScriptManager ID =ScriptManager1=服务器>
     < / ASP:ScriptManager的>
  &安培; NBSP;< ASP:的UpdatePanel ID =UpdatePanel1=服务器>
            <&的ContentTemplate GT;
               < IMG ID =imgOrginal=服务器的风格=高度:200像素;宽度:200像素; />
              < /&的ContentTemplate GT;
      < / ASP:的UpdatePanel>

了解更多详情

I am uploading a file using the <asp:FileUpload> and <asp:button> controls but I want to do it without a postback. On button click I execute the following code.

protected void btnUpload_Click(object sender, EventArgs e)
{
    string strFileName = Path.GetFileName(FileUpload1.FileName);  //fileupload1 is the <asp:fileupload ID
    FileUpload1.SaveAs(Server.MapPath("~/UploadFile/" + strFileName + ""));
    imgUpload.ImageUrl = "../UploadFile/" + strFileName + "";  //imgupload is the <img ID on which I am showing the image after upload
    imgUpload.Visible = true;
}

After uploading the file I am showing the saved image from the specified folder in my project solution, but on clicking the upload button the whole page gets loaded and I don't want the postback on clicking the upload button.

解决方案

just add the script

    <script language="javascript">
        function Change(obj) {
             __doPostBack("<%= btnUpload.ClientID %>", "");
        }

      </script>


Call the script in your button click event like this 
<pre>
onchange="Change(this);"
</pre>   

Your image control added in the update panel with contentTemplate 

    <asp:FileUpload ID="imgFileUploader" runat="server"   />
             <asp:Button ID="btnUpload" runat="server"  
         Text="Upload" onclick="btnUpload_Click" onchange="Change(this);" />
      <br />
        <br />
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
  &nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>    
               <img id="imgOrginal" runat="server" style="height: 200px; width: 200px;" />
              </ContentTemplate>
      </asp:UpdatePanel>

for more details

这篇关于如何上传图像文件中没有任何ASP.NET回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆