在Repeater中运行脚本并显示结果

时间:2012-05-24 03:26:59

标签: asp.net repeater

我的页面上有一个转发器,我想运行一个特定于每个项目的脚本。

这是我目前的代码

        <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>

有人能告诉我如何让它发挥作用吗?

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

这有可能吗?

1 个答案:

答案 0 :(得分: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

    }
相关问题