如何用button触发fileupload控件的click事件

xiaoxiao2021-02-28  68

正儿八经是这样的,若执行无效的话,有可能与MasterPageFile母版中的ContentPlaceHolder1有关。

web页面:

 

 <script type="text/javascript">       

      //单击按钮,就像单击“浏览...”一样作用         function BrowseFile() {             document.getElementById("FileUpload1").style.display = "inline";             document.getElementById("FileUpload1").click();         }     </script>

 

 <asp:FileUpload ID="FileUpload1" runat="server" />                                  <asp:TextBox ID="txtFileUrl" runat="server"></asp:TextBox>                                  <input   id="Button1"   type="button"   value="浏览文件"   οnclick="BrowseFile();"/>

后台:

   protected void Page_Load(object sender, EventArgs e)         {             if (!IsPostBack)             {

//单击后给txtFileUrl文件框赋值,就是FileUpload1的路径。实际上就是利用了浏览打开文件的功能                 this.FileUpload1.Attributes.Add("onchange", "document.getElementById('" + txtFileUrl.ClientID + "').value = this.value");

       //若要因此FileUpload1控件

         this.FileUpload1.Style.Add("display",   "none");

               }

}

转载请注明原文地址: https://www.6miu.com/read-51395.html

最新回复(0)