【问题标题】:Asp.net, using <%=getter%> in aspx file, The Controls collection cannot be modified because the control contains code blocksAsp.net,在 aspx 文件中使用 <%=getter%>,无法修改 Controls 集合,因为该控件包含代码块
【发布时间】:2013-01-15 15:39:49
【问题描述】:

我有一个aspx,aspx.cs文件,在我使用javascript函数的aspx文件中,

在 javascript 代码中,我使用在 aspx.cs 文件中定义的 getter () 方法,

但页面返回:“控件集合无法修改,因为控件包含代码块”为什么?

 <!-- language-all:lang-js -->
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadFile.aspx.cs"
Inherits="UploadControl_CustomProgressPanel" Title="Untitled Page" %>
 <!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 id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
var size = <%=getSize%>;
var id= 0;

function ProgressBar() 
{
if(document.getElementById("FileUpload1").value != "")
{
    document.getElementById("divProgress").style.display = "block";
    document.getElementById("divUpload").style.display = "block";
    id = setInterval("progress()",20);
    return true;
}
else
{
    alert("Select a file to upload");
    return false;
}    

}

function progress()
{
//size = size + 1;
if(size > 299) 
{
    clearTimeout(id);
 }
document.getElementById("divProgress").style.width =  size + "pt";
document.getElementById("lblPercentage").firstChild.data = parseInt(size / 3) +  "%";
}

aspx.cs 文件:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class UploadControl_CustomProgressPanel : System.Web.UI.Page 
{   

   protected int size = 2;

   protected int getSize{ get { return this.size; }}


   protected void Page_Load(object sender, EventArgs e)
    {
    string UpPath;
    UpPath = "C:\\";
    if (! Directory.Exists(UpPath))
    {
        Directory.CreateDirectory("C:\\");      
    }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    //StatusLabel.Text = "File scelto, Upload in corso..";
    if(FileUpload1.HasFile)
    {
        try
        {   
            string filePath = Request.PhysicalApplicationPath;
            filePath += "classic/hub/csv/";
            filePath += FileUpload1.FileName;
            this.size = 50;
            FileUpload1.SaveAs(filePath);
            //StatusLabel.Text = "Upload status: File uploaded!";
            Label1.Text = "Upload successfull!";
        }
        catch(Exception ex)
        {
            //StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
        }
    }
    }

【问题讨论】:

  • 您是否正在向 Page 或其子控件添加一些动态控件?

标签: c# javascript asp.net


【解决方案1】:

您尚未发布完整的 HTML,但请尝试将您的脚本移出 &lt;head&gt; 标记。

【讨论】:

    【解决方案2】:

    getSize 是属性还是函数?如果它的 a 属性大小将采用它在页面加载时的值,如果它是 a 函数,那么您需要查看页面方法,因为您不能以这种方式调用它,heres an example

    【讨论】:

    • 这是一个属性,但加载页面时出现错误:无法修改控件集合,因为控件包含代码块,因为存在 标记
    • 好的,这里看起来像这篇帖子的重复问题stackoverflow.com/questions/4995274/… 尝试将违规代码包装在&lt;asp:placeholder runat="server"&gt; 标记中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    相关资源
    最近更新 更多