2012-05-24 23 views
0

我在我的頁面上有一箇中繼器,我要運行一段特定於每個項目的腳本。在中繼器中運行腳本並顯示結果

這是到目前爲止我的代碼

 <asp:Repeater ID="topicView" runat="server"> 
    <HeaderTemplate> 
     <table width="925px" cellpadding="0" cellspacing="0"> 
      <tr> 
       <td style="text-align:left;" class="topic-header-home"><strong>Topic Title</strong></td> 
       <td width="10%" class="topic-header-home"><strong>Posts</strong></td> 
       <td width="20%" class="topic-header-home"><strong>Started By</strong></td> 
      </tr> 
    </HeaderTemplate> 
    <ItemTemplate>   
     <tr> 
      <td style="text-align:left;" class="topic-cont-home"> 
       <p><a href='view.aspx?topicID=<%#DataBinder.Eval(Container.DataItem, "TopicID")%>'><strong><%#DataBinder.Eval(Container.DataItem, "TopicName")%></strong></a></p> 
      </td> 
      <td class="topic-cont-home"> 
       <script type="text/C#"> 
        // Define the select statement. 
        // All information is needed 
        string selectPostCount = "select count(*) from Posts WHERE [email protected]"; 
        // Define the ADO.NET Objects 
        using (SqlConnection con = new SqlConnection(connectionString)) 
        { 
         SqlCommand pccmd = new SqlCommand(selectPostCount, con); 
         pccmd.Parameters.AddWithValue("@topicID", <%#DataBinder.Eval(Container.DataItem, "TopicID")%>); 
         con.Open(); 
         int numrows = (int)pccmd.ExecuteScalar(); 
         string posts = numrows.ToString(); 
         con.Close(); 
         posts.InnerHTML = posts; 
        } 
       </script> 
       <p id="posts" runat="server"></p> 
      </td> 
      <td class="topic-cont-home"> 
       <p><strong><%#DataBinder.Eval(Container.DataItem, "Username")%></strong></p> 
      </td> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
     </table> 
    </FooterTemplate> 
</asp:Repeater> 

誰能告訴我,我將如何得到這個工作?

到目前爲止,它不是說我有任何錯誤,但它也不工作。

這可能嗎?

回答

0

您是否嘗試過在你的代碼隱藏包裹腳本塊在受保護的方法,只是做的事:GetNumberOfRows將在代碼中的中繼器,從而將這項工作被定義背後

<asp:Repeater runat="server" ID="repeater"> 
    <ItemTemplate> 

     <p id="posts" runat="server"><%#GetNumberOfRows((int)DataBinder.Eval(Container.DataItem, "TopicID"))%></p> 
    </ItemTemplate> 
    </asp:Repeater> 

protected int GetNumberOfRows(int topicId) 
    { 
     //Run query and return result 

    } 
+0

這是什麼?對不起,我是一名視覺學習者。是否有可能在C#中發佈cs的代碼示例? – dpDesignz

+0

我更新了粗略實施的帖子 – TGH

+0

謝謝。但由於它在中繼器中,因此它不會寫入該ID,因爲它「無法在上下文中找到它」。這是我的cs代碼http://pastebin.com/0r2QRLbF – dpDesignz

相關問題