2011-10-08 157 views
1

我是Jquery和javascript的新手。我在我的as.net項目中使用Jquery UI選項卡。我的標籤內容是dynamic.so,我使用SQL Server來保存並檢索它們。我有一個類來建立基於點擊特殊按鈕的標籤。這是我的課:點擊加載jquery標籤內容

public class TabBuilder{ 
public string Build(int ServiceId) 
{ 

    string title = " <div class='demo'><div id='tabs' onclick='alert(yes)'><ul>\n"; 
    string content = ""; 

    DataTable select = new DataTable(); 
    ServicesBLL ser = new ServicesBLL(); 
    select = ser.FormsSelTabs(ServiceId); 

    int i = 1; 

    foreach (DataRow row in select.Rows) 
    { 

     title += createTitle(row["LinkName"].ToString(), i); 
     content += createContent(row["LinkHref"].ToString(), i); 
     i++; 
    } 
    title += "</ul>\n"; 
    content += "</div></div>"; 

    return title + content; 
} 

private string createTitle(string title, int i) 
{ 

    string htm = "<li><a id='tab{0}' href='#tabs-{1}'>{2}</a></li>\n"; 
    htm = string.Format(htm, i,i, title); 
    return htm; 
} 

private string createContent(string content, int i) 
{ 
    string htm = "<div id='tabs-{0}'><iframe dir='rtl' frameborder='0' height='600px' width='100%' src='{1}'></iframe></div>\n"; 
    htm = string.Format(htm, i, content); 
    return htm; 
}} 
在我的aspx頁面

我有一個文本(LtrTabs)來顯示標籤和aspx.cs代碼,我有這樣的代碼來構建標籤:

protected void btnEntities_Click(object sender, EventArgs e) 
{ 
    ServiceID = 1; 
    TabBuilder tab = new TabBuilder(); 
    LtrTabs.Text = tab.Build(ServiceID); 
} 

我需要懶加載標籤,但我不知道如何實現它。我看到了一些例子,但是沒有一個使用數據庫來加載內容。

+0

作爲更一般的說明,你真的不應該在服務器端代碼中輸出HTML。尤其是_compiled_服務器端代碼。如果你的平面設計師想要改變iframe的大小呢? – 2011-10-08 13:22:26

回答

1

我並沒有通過服務器端代碼篩選就知道肯定,但只要你有一個函數與標記輸出標籤,看起來像這樣:

http://jqueryui.com/demos/tabs/#Ajax_mode

然後,這只是一個用有效資源填充hrefs的問題。在服務器端,只要它能夠識別HTML內容的傳入請求(無需發回製表符等等,只是內容),這就是您所需要的。

要回顧一下:

  1. 函數返回空選項卡中的HTML。或者,如果這不需要是動態的,只需將它寫爲HTML;沒有必要用不必要的邏輯使事情複雜化。

  2. 函數返回標籤的內容。

你怎麼區分這兩者真的取決於你。 POST/GET變量,不同的URL ......但是你的應用程序正在構建。