当前上下文值不会从Usercontrol呈现到.aspx页面
Current context value is not rendered from Usercontrol to .aspx page
我有默认。Aspx页面和一个用户控件。Usercontrol有以下代码用于多个文件上传。现在的问题是,当我添加一个文件上传,当前的上下文文件没有给我任何值,它仍然是零,我猜,因为它是从用户控制渲染。
我该怎么办?
My 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控件时,它会按原样呈现,但当你有control with id addfile在usercontrol with id Fileuploader时,呈现的id Fileuploader_addfile因此,在Java脚本中使用正确的id更改id名称。
要查看渲染id的名称,在浏览器中打开页面,打开页面的源代码,找到您的元素并将id复制到java脚本中。
我怀疑可能是这一行:
<script type="text/javascript" src="_scripts/jquery-1.4.1.min.js"></script>
应该是:
<script type="text/javascript" src="/_scripts/jquery-1.4.1.min.js"></script>
相关文章:
- 将 jQuery 代码添加到 Index.aspx 页面
- 如何在aspx页面之间传递参数
- .aspx页面上引导程序中的动态选项卡
- 在新的浏览器选项卡上加载新的aspx页面后调用JavaScript函数
- window.onload适用于aspx页面,但不适用于普通html
- 在aspx页面c#之间传递/引用/发送变量/会话状态
- 将数据发布到iframe(aspx)以在aspx页面的页面加载中捕获它
- 如何使用js在两个aspx页面之间进行通信
- 将外部javascript文件视为Aspx页面的一部分
- aspx页面VS2012的javascript函数中未命中断点
- 将Javascript变量传递给C#函数aspx并从中返回到aspx页面
- .aspx页面JS将在控件似乎由于某种原因而被呈现之前启动Onload脚本
- 在aspx页面中实现javascript时出现问题
- 如何在aspx页面中使用日期选择器
- 如何从aspx页面引用jquery函数
- 如何从Aspx页面发送Json数据
- 如何在aspx页面中的转发器ItemDataBound中传递函数中的Control.ClientID
- 禁用.aspx页面上的权限,但启用超链接
- 如何在传递到aspx页面之前加密数据
- 调整从 aspx 页面链接的 pdf 的大小