2012-05-24 26 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

相关问题