2013-03-26 63 views
0

我正在編寫一個for循環,顯示帶有一些chartfx顯示的鏈接列表。 chartfx需要一個sqlDataSource。每次for循環執行一次迭代時,我試圖給出唯一的ID,但是我無法將它傳遞給一個值或函數。下面的例子在我的代碼中。 getSQLID()只是一個函數,它返回一個字符串,我想成爲我的ID。這一切都在aspx頁面上完成,並且該函數位於.cs中。任何幫助將非常感謝你。如何使用變量或函數在HTML中聲明我的SqlDataSource ID

 //name of the contentplace holder on the aspx page 
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" > 

    //code behind 
    Control ctrl = LoadControl("WebUserControl.ascx"); 
    Control placeHolderControl = this.FindControl("Content2"); 
    Control placeHolderControl2 = this.FindControl("ContentPlaceHolder1"); 

    ctrl.ID = "something"; 
    if (placeHolderControl != null) 
     placeHolderControl.Controls.Add(ctrl); 
    if (placeHolderControl2 != null) 
     placeHolderControl2.Controls.Add(ctrl); 
+0

嗯。似乎這不是解決問題的最佳方法。您是否需要爲每個chartfx控件分別創建一個SqlDataSource? – jadarnel27 2013-03-26 15:18:56

+0

是的,我會的,每個人都顯示自己的結果基於sqlDataSource – dreebee 2013-03-26 15:35:35

+0

'for'循環在哪裏? – bmm6o 2013-03-26 23:52:01

回答

0

首先,回想一下,在設計器中聲明的服務器控件在編譯時會附加到您的類中。因此,在運行時嘗試在循環中創建多個實例是沒有意義的,這就是爲什麼例如在編譯時必須知道Id標記。

一種替代方法是在代碼中創建它們的背後,有着類似:

for (int i=0; i<2; ++i) 
{ 
    var chart = new Chart(); 
    chart.Id = "chartId" + i; 
    chart.DataSourceId = "srcid" + i; 

    var src = new SqlDataSource(); 
    src.Id = "srcid" + i; 

    Controls.Add(chart); // either add to the collection or add as a child of a placeholder 
    Controls.Add(src); 
} 

在你的情況下,所有這些聲明的屬性轉換爲後面的代碼可以是一個工作位(雖然它是可能的)。另一種方法是創建一個用戶控件(ascx),其中包含現在位於aspx頁面中的標記。你會在你的代碼背後實例化控件,類似於:

for (int i=0; i<2; ++i) 
{ 
    var ctrl = LoadControl("~/path/to/Control.ascx"); 
    ctrl.Id = "something_" + i; 
    Controls.Add(ctrl); // again, either here or as a child of another control 
    // make the src, hook them up 
} 
+0

我得到這個錯誤'<%= GetSqlId(i)%>'不是一個有效的標識符。 ' 當我做同樣的事情爲chartfx它似乎工作 DataSourceID =「<%= GetSqlId(i)%>」 – dreebee 2013-03-27 16:16:14

+0

顯然他們不喜歡有功能給他們ID的 – dreebee 2013-03-27 16:23:19

+0

你介意給我看一些事情的aspx方面,因爲我有所有這與codefx相關的代碼我不是100%確定代碼將如何連接到圖表生病更多我的代碼上面。我應該試着在代碼背後做代碼嗎? – dreebee 2013-03-27 17:50:03

相關問題