Asp.net fileupload阻止了onclick事件? [英] Asp.net fileupload prevents onclick event?

查看:144
本文介绍了Asp.net fileupload阻止了onclick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个看似相当简单的问题......但我似乎无法让它发挥作用。我正在开发一个asp.net C#webforms应用程序,我们需要上传一个文件。我在页面上放了一个fileupload控件和一个按钮。想法是用户选择一个文件,然后单击按钮进行实际上传。有很多可用的例子,我尝试了几个 - 无济于事。



问题是,在用户选择文件然后单击按钮后,该按钮的onclick事件永远不会被触发。文件上载恢复为未选择文件,并且没有发生任何事情。奇怪的是,当文件上传处于显示未选择文件的状态时,onclick被触发...击败我。



我正在使用bootstrap,jquery,scriptmanager和masterpages,因此我制作了一个简单的表单,它被简化为简单的html,但它仍然无效。我在按钮的onclick事件中放了一个断点。如果没有选择文件,则会触发此断点,但是如果我确实选择了一个文件,然后单击该按钮则不会命中它,也没有任何反应。



我已经包含了标记和代码隐藏。有没有人经历过这种行为?任何帮助表示赞赏,提前谢谢!



我尝试过:



I stumbled upon an issue that seems to be rather simple... yet I can't seem to get it work. I'm working on a asp.net C# webforms application and we have the need to upload a file. I have put a fileupload control on the page and a button. The idea is that the user selects a file and then clicks on the button to do the actual upload. Plenty of examples available, and I tried several - to no avail.

The problem is that after the user selects a file and then clicks the button, the onclick event of that button is never fired. The fileupload reverts to "No file chosen" and nothing has happened. The odd thing is, that when the file upload is in the state where it shows "No file chosen" the onclick is fired... beats me.

I'm using bootstrap, jquery, a scriptmanager and masterpages so I made a simple form that is stripped down to plain html and it's still not working. I have put a breakpoint in the onclick event of the button. If there is no file selected this breakpoint is hit, but if I do select a file and then hit the button it is not hit and nothing happens.

I have included the markup and codebehind. Has anyone experienced this behavior? Any help is appreciated, thanks in advance!

What I have tried:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testupload.aspx.cs" Inherits="Admin_testupload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
            <hr />
            <asp:LinkButton ID="lbt_Go" runat="server" OnClick="lbt_Go_Click">Test</asp:LinkButton>
        </div>
    </form>
</body>
</html>







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_testupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void lbt_Go_Click(object sender, EventArgs e)
    {
        var x = "breakpoint";
    }
}

推荐答案

尝试使用更新面板。它可以帮助您解决问题。



Try to use update panel. which help you resolve your problem.

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
        <hr />
        <asp:LinkButton ID="lbt_Go" runat="server" OnClick="lbt_Go_Click">Test</asp:LinkButton>        
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID = "lbt_Go" />
    </Triggers>
</asp:UpdatePanel>


感谢您的建议,但使用更新面板没有帮助。



Ajax控件工具包有一个高级文件上传工作,所以当我终于找到答案时,我已经准备好走这条路了:标准的文件上传控件是通过默认限制为~4MB,我的所有测试文件都变得更大了。一旦我在web.config中更改了此限制,就会按预期工作。尽管如此,获得某种异常可以触发这个问题本来不错。



我认为自己是银狐网络设计师,但我以前从未需要上传文件。我碰到这个问题有点蠢,但我很高兴它现在正在工作。
Thank you for your suggestion, but using an update panel did not help.

The Ajax Control Toolkit has an advanced file upload that did work so I was already preparing to go that route when I finally found the answer: the standard fileupload control is by default limited to ~ 4MB and all my testfiles happened to be larger. Once I changed this limit in web.config things worked as expected. It would have been nice to get an exception of some sort to trigger this issue though.

I consider myself a silver fox webforms designer but I have never needed to upload files before. I do feel a bit foolish that I ran into this issue, but I'm glad it's working now.


这篇关于Asp.net fileupload阻止了onclick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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