Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery文件上传刷新页面_Javascript_Asp.net_File Upload - Fatal编程技术网

Javascript jQuery文件上传刷新页面

Javascript jQuery文件上传刷新页面,javascript,asp.net,file-upload,Javascript,Asp.net,File Upload,我看到这个问题: '我必须在单击按钮时显示一个打开的对话框。基本上我必须上传一个文件,为此我使用FileUpload控件,但我不想向用户显示它,而是想显示一个按钮的名称 答案是: <script type="text/javascript"> $(document).ready(function() { $("#btn").click(function() { $("#FileUpload1").click()

我看到这个问题:

'我必须在单击按钮时显示一个打开的对话框。基本上我必须上传一个文件,为此我使用FileUpload控件,但我不想向用户显示它,而是想显示一个按钮的名称

答案是:

 <script type="text/javascript">      
   $(document).ready(function() {      
      $("#btn").click(function() {       
        $("#FileUpload1").click();         
       return false; 
      });         
   });  
   </script>   
   <style type="text/css">      
      .Class { visibility:hidden;}     
   </style> </head> <body>    
   <form id="form1" runat="server">  
   <div>       
      <asp:Button ID="btn"  runat="server" Text="Send File"/>   
      <asp:FileUpload ID="FileUpload1" CssClass="Class" runat="server" /> 

$(文档).ready(函数(){
$(“#btn”)。单击(函数(){
$(“#文件上载1”)。单击();
返回false;
});         
});  
.Class{可见性:隐藏;}
但是我试过了,它所做的只是刷新页面,有人知道问题出在哪里吗?

因为“FileUpload1”不是ClientID。只需查看页面生成的HTML源代码,您就会看到这一点

您应该使用以下内容:

<script type="text/javascript">      
$(document).ready(function() {      
  $("#<%= btn.ClientID %>").click(function() {       
    $("#<%= FileUpload1.ClientID %>").click();         
      return false; 
  });         
});  
</script> 

$(文档).ready(函数(){
$(“#”)单击(函数(){
$(“#”)点击();
返回false;
});         
});  
所有服务器端控件(具有
runat=“server”
属性的控件)的ID都由ASP.NET重新写入。您的ID实际上看起来像
ctl00\u MainContent\u btn

您可以通过使用
服务器标记或将CSS类分配给控件并在JavaScript/jQuery中引用该类来解决这个问题


编辑:您可能还需要确保ASP按钮不是提交按钮,这会导致生成的页面提交表单。

这听起来像是一个安全风险,如果安全性阻止了它的工作,我也不会感到惊讶


看看这个插件。

我建议你不要走那种路线。如果要避免向用户显示FileUpload控件

将客户端模式设置为静态,以便能够访问如下控件

<asp:FileUpload ID="FileUpload1" ClientIDMode="Static" CssClass="Class" runat="server" /> 
  <asp:Button ID="btn"  ClientIDMode="Static"  runat="server" Text="Send File"/>

您的页面会刷新,因为表单的目标隐式地是当前页面。您需要将表单的目标设置为(例如)隐藏的iframe:

<form id="my-form" action="url" target="form-target" method="post">
  <!-- your form here -->
</form>

<iframe id="form-target" name="form-target" style="display:none;"></iframe>

<script>
  // Assuming you are using jquery
  var form = $('#my-form'),
      iframe = $('#form-target');

  // create a function to be called each time the iframe loads
  iframe.load(function () {
    var responseText, iframeBody;

    // Get the response from the server. It will be in the body tag of your iframe
    iframeBody = $(this).contents().find('body');
    responseText = iframeBody.text().trim();
    // Don't continue until we actually have a response
    if (!responseText) return;
    // Clear the iframe's html so this function won't be called again for the same content
    iframeBody.html('');

    // do whatever you want with the response, for example JSON decode it
    response = JSON.parse(responseText);
  });
</script>

//假设您使用的是jquery
变量形式=$(“#我的形式”),
iframe=$(“#形式目标”);
//创建每次加载iframe时要调用的函数
iframe.load(函数(){
var responseText,iframeBody;
//从服务器获取响应。它将位于iframe的body标记中
iframeBody=$(this.contents().find('body');
responseText=iframeBody.text().trim();
//在我们真正得到答复之前不要继续
如果(!responseText)返回;
//清除iframe的html,这样就不会对相同的内容再次调用此函数
iframeBody.html(“”);
//对响应执行任何您想要的操作,例如JSON解码
response=JSON.parse(responseText);
});

你当然可以想出一个更具描述性的标题。你能发布生成的HTML吗?@Matt-你对一个将他的类命名为“Class”的家伙有什么期望?开玩笑;-)@多米尼克如果你仔细阅读,你会发现这不是我的代码;)@罗伊-是的,我知道,说我在开玩笑!!;-)@mohib我不能在我的组织中使用@James我不能在我的组织中使用它你不能使用什么?jQuery脚本?不能使用任何第三方库?这只是一个javascript文件,不是你安装的插件…@RoyGavrielov,什么不起作用?我们需要您提供更多信息来帮助您解决问题。对不起,我将(“#btn”)替换为(“”),将(“#FileUpload1”)替换为(“#”),但仍然无法工作。您能否编辑问题以包含新信息?您是否收到任何错误消息?您是否尝试过使用FireBug或调试任何C代码来缩小导致刷新的范围?另外,你是否检查过你的ASP按钮是否没有将表单提交到你所在的页面?我刚刚尝试了他的代码,效果非常好。这样做。。打开这页。。单击“发送文件”按钮。。按Ctrl+Shift+J并查看列出的错误。是的,但现在我的HTML访问被拒绝:(