我有default.aspx页和一个用户控件。
usercontrol具有以下用于多个文件上传的代码。
现在的问题是,当我添加一个要上传的文件时,当前上下文文件没有给我任何值,但我仍然认为它仍为零,因为它是从用户控件渲染的。

我该怎么办?

我的用户控件UPLOAD.ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FileUpload.ascx.cs" Inherits="FileUpload" %>
<script type="text/javascript" src="_scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
    var i = 1;
    $(document).ready(function () {
        $("#addfile").click(function () {
            $("#dvfiles").append("<input name=" + i + "fu type=file /><a href=#>remove</a><br>");
            i++;
        });

        $("#dvfiles a").live('click', function () {
            $(this).prev("input[type=file]").remove();
            $(this).remove();
        });
    });

    $(document).submit(function () {
        var flag = true;
        $("#dvfiles input[type=file]").each(function () {
            if ($(this).val() == "") {
                $(this).css("background", "Red");
                flag = false;
            }
        });
        return flag;
    });
</script>

<div id="Fileuploader">
<a href="#" id="addfile">Attach a file..</a><br />
<asp:Label ID="lblMessage" runat="server"></asp:Label><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
    onclick="btnUpload_Click" />
</div>


UPLOAD.ASCX.CS

protected void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        HttpFileCollection filecolln = Request.Files;
        //here i don't get values of current files.
      // this is zero. because of this following if condition failed
       //please help here
        if (filecolln.Count > 0)
        {
            for (int i = 0; i < filecolln.Count; i++)
            {
                HttpPostedFile file = filecolln[i];
                if (file.ContentLength > 0)
                {
                    file.SaveAs(ConfigurationManager.AppSettings["FilePath"] + System.IO.Path.GetFileName(file.FileName));
                }
            }
            lblMessage.Text = "Uploaded Successfully!";
        }
        else
        {
            lblMessage.Text = "No files selected!";
        }

    }
    catch (Exception ex)
    {
        lblMessage.Text = ex.Message;
    }
}


Default.aspx code

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>
    <%@ Register TagPrefix="ucFileuploader" tagName="Fileuploader" src="FileUpload.ascx"    %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ucFileuploader:Fileuploader ID="Fileuploder" runat="server" />
    </div>
    </form>
</body>
</html>

最佳答案

问题是您使用的是JavaScript的ID名称。

因此,当您使用.aspx中的id addfile进行控制时,它将按原样呈现,
但是,当您在ID为Fileuploader的用户控件中拥有ID为addfile的控件时,
比呈现的ID是Fileuploader_addfile,
因此,请在Java脚本中使用正确的ID更改ID名称。

要检查渲染的ID的名称是什么,请在浏览器中打开页面,打开页面的源代码并找到您的元素,然后将ID复制到Java脚本中。
使用呈现的ID名称更改Java脚本中的所有ID。

关于javascript - 当前上下文值未从用户控件呈现到.aspx页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8325725/

10-11 02:04
查看更多