2013-01-15 49 views
-1

我有一個aspx,aspx.cs文件,在使用JavaScript功能aspx文件IM,Asp.net,在aspx文件使用<%=getter%>,Controls集合不能被修改,因爲該控件包含代碼塊在

javascript代碼我使用在aspx.cs文件中定義的getter(<%= getSize%>)方法,

但頁面返回:「控件集合不能被修改,因爲控件包含代碼塊」爲什麼?

<!-- 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; 
     } 
     } 
    } 
    } 
+0

您是否正在爲Page或其子控件添加一些動態控件? –

+0

沒有我不是..... –

回答

0

您尚未發佈完整但嘗試將腳本移出<head>標記。

+0

好吧,讓我試試。 –

+0

thx yea它的工作原理 –

0

getSize屬性或功能?如果它的一個屬性大小將取值在頁面加載時,如果它是一個函數,那麼您需要查看頁面方法,因爲您不能以這種方式調用它,因此,heres an example

+0

這是一個屬性,但我得到的錯誤加載頁面:Controls集合不能修改,因爲該控件包含代碼塊東陽存在<% , %>標記 –

+0

好吧,看起來像一個重複的問題這篇文章在這裏http://stackoverflow.com/questions/4995274/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blo嘗試在''標記 – James

相關問題