2011-02-23 116 views
1

我有主頁和HTML代碼,它們從AddThis類發佈字符串。Page.DataBind()衝突

MainMasterPage.master

<div class="add-this"> 
    <%# AddThis.Publish %>         
</div> 

AddThis.cs類代碼

public static class AddThis 
{ 
    public static string Publish 
    { 

     get 
     { 
      return 

       "<div class='addthis_toolbox addthis_default_style'>" + 
       "<a class='addthis_button_delicious'></a>" + 
       "<a class='addthis_button_digg'></a>" + 
       "<a class='addthis_button_stumbleupon'></a>" + 
       "<a class='addthis_button_facebook'></a>" + 
       "<a class='addthis_button_twitter'></a>" + 
       "<a class='addthis_button_googlebuzz'></a>" + 
       "<a class='addthis_button_email'></a>" + 
       "<a class='addthis_button_blogger'></a>" + 
       "<a class='addthis_button_compact'></a>" + 
       "<span class='addthis_separator'> </span>" + 
       "<a class='addthis_button_facebook_like'></a>" + 
       "</div>" + 
       "<script type='text/javascript' src='http://s7.addthis.com/js/250/addthis_widget.js#username=tomasr'></script>"; 

     } 

    } 
} 

要綁定<%#AddThis.Publish%>標記和執行等級碼我添加的DataBind掌握這樣的網頁代碼

Mai nMasterPage.master.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    Page.DataBind(); 
} 

的問題是,我的主要主網頁(MainMasterPage.master)嵌套在第三方應用論壇使用,我得到的時候被執行論壇網頁下面的錯誤。似乎MainMasterPage.master中的DataBind代碼在初始化之前在論壇代碼中綁定控件。 問題是,如何在所有網頁中綁定我的AddThis標籤,但不綁定其他對象?

Invalid attempt to call FieldCount when reader is closed. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Invalid attempt to call FieldCount when reader is closed. 

Source Error: 


Line 58: 
Line 59:   
Line 60:   Page.DataBind(); 
Line 61: 
Line 62:  } 

回答

1

全部誠信。我個人會使用文字頁面上和文字的。文本設置爲AddThis.Publish類

Literal1.Text = AddThis.Publish; 

使用內聯代碼可以是該死的討厭

其他選項數據綁定是使用

<%# Eval("AddThis.Publish") %> 

改爲

+0

謝謝您的回答。當我嘗試使用第二個建議時,出現錯誤: 數據綁定方法(如Eval(),XPath()和Bind()只能用於數據綁定控件的上下文中。 – Tomas 2011-02-23 10:43:04

+0

你的第一個建議很好,謝謝! – Tomas 2011-02-23 10:48:18