编程语言
首页 > 编程语言> > c#-单击事件调用隐藏的FileUpload控件

c#-单击事件调用隐藏的FileUpload控件

作者:互联网

我在asp.net页面上有一个FileUpload控件,该控件已隐藏

<asp:FileUpload id="FileUploadControl" runat="server" CssClass="hidden" />

我希望在用户单击“浏览”按钮时调用此控件

<asp:Button runat="server" id="BrowseButton" text="browse" />

每次用户单击浏览按钮时,是否都可以将控制权交给FileUpload控件?

编辑:

在BrowseButton_Click事件上,我想上传文件

 protected void BrowseButton_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    Response.Write("<script>alert('done');</script>");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {
                Response.Write("<script>alert('please choose');</script>");
            }
        }

解决方法:

是的,您可以使用jQuery在客户端进行此操作.请找到以下代码:

<asp:FileUpload id="FileUploadControl" runat="server"  CssClass="hidden" />

<asp:Button runat="server" id="BrowseButton" text="browse" OnClientClick="openfileDialog();"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
   function openfileDialog() {
       $("#FileUploadControl").click();
   }
</script>

标签:buttonclick,file-upload,asp-net,c
来源: https://codeday.me/bug/20191120/2043816.html