文件上传回发后空 [英] FileUpload empty after PostBack

查看:230
本文介绍了文件上传回发后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于一个ASP.NET fileuploader就是回发后空的一个问题。我有一个FileUpload控件,一个TextBox控件和一个按钮控件。

I have a problem regarding a ASP.NET fileuploader that is empty after postback. I have one fileupload control, one textbox control and one button control.

当我按一下按钮,它会检查是否填充文本框,如果是它执行的操作,如果它不那么它显示了一个错误信息,并在网页上停留。然而,当我preSS按钮,文本框为空,按钮postsback和fileuploader是空的,用户必须再次选择该文件。所以:用户选择文件>文件位于fileuploader>用户presses按钮>的错误信息弹出>的fileuploader回发后失去文件

When I click the button, it checks if the textbox is populated, if it is it performs actions, if its not then it shows an error message and stay on the page. However, when I press the button and the textbox is empty, the button postsback and the fileuploader is empty and the user has to select the file again. So: User selects file > The file is located in the fileuploader > the user presses the button > the error message pops up > the fileuploader loses the file after postback.

当用户选择的文件,但尚未点击按钮(koala.jpg)

When the user selected a file but not yet clicked the button (koala.jpg)

当按钮被pressed,文件消失(没有文件选择)结果

When the button is pressed, the file is gone (no file selected)

如何保持在fileuploader文件中的回发后,或也许会告诉该按钮不回发如果文本框为空?

编辑:

请注意,我简化了问题,第一个帖子的内容是编造的,使我的问题更加明确。

Be aware that I simplified the question and that the content of the first post are just made up to make my question more clear.

页面加载:

    protected void Page_Load(object sender, EventArgs e)
    {
        //First check if User is logged in 
        if (Session["User"] == null)
        {
            Response.Redirect(GetRouteUrl("Login", null));
        }

        TicketsBLL objTicketsBll = new TicketsBLL();


        var cat = objTicketsBll.getAllCategories();

        ddlCategory.DataTextField = "name";
        ddlCategory.DataValueField = "id";
        ddlCategory.DataSource = cat;
        ddlCategory.DataBind();



    }

按钮点击:

//check if everthing is filled :
        errorContent.Visible = false;
        errorSubject.Visible = false;

        if (txtSubject.Text == "" || txtSubject.Text == null || txtContent.Text == null || txtContent.Text == "")
        {
            if (txtSubject.Text == "" || txtSubject.Text == null)
            {
                errorSubject.Visible = true;
            }
            if(txtContent.Text == null || txtContent.Text == ""){
                errorContent.Visible = true;
            }
        }

        else { //Do something after this

&errorSubject放大器; errorContent被隐藏的div显示为错误

errorSubject & errorContent are hidden divs that show up as the error

推荐答案

这是不可能设置一个文件输入的值。这是一个浏览器的安全特性,它与一个密码字段相同,唯一不同的是用密码字段中可以使用JavaScript设置的值。

It's not possible to set the value of a file input. This is a browser security feature, it's the same with a password field, the only difference is with a password field you can set the value using JavaScript.

然而,随着文件输入你不能设置的值。

However with the file input you cannot set the value.

解决方案是prevent回传,使用的JavaScript 回发前检查值。

The solution is to prevent the postback, use JavaScript to check the value before posting back.

这篇关于文件上传回发后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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