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 TopicID=@topicID";
                    // 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>

谁能告诉我如何让它工作?

到目前为止,它并没有说我有任何错误,但它也不起作用。

这可能吗?

4

1 回答 1

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

    }
于 2012-05-24T04:13:06.477 回答