C# ASP.NET Repeater-在HeaderTemplate中显示数据绑定

C# ASP.NET Repeater-在HeaderTemplate中显示数据绑定,c#,asp.net,repeater,C#,Asp.net,Repeater,我有一个中继器,我想在数据库的HeaderTemplate中添加标题 这是到目前为止我的代码 <asp:Repeater ID="topicView" runat="server"> <HeaderTemplate> <tr> <td> <h1><%#DataBinder.Eval(Container.DataItem, "TopicName")%&g

我有一个中继器,我想在数据库的HeaderTemplate中添加标题

这是到目前为止我的代码

<asp:Repeater ID="topicView" runat="server">
    <HeaderTemplate>
        <tr>
            <td>
                <h1><%#DataBinder.Eval(Container.DataItem, "TopicName")%></h1>
            </td>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "PostBody")%>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        <tr>
            <td>
            </td>
        </tr>
    </FooterTemplate>
</asp:Repeater>

您不需要绑定到数据源,只需绑定到页面的一个简单属性即可。在HeaderTemplate中使用此选项:

<h1><%# TopicName %></h1>
然后在运行查询时设置它:

TopicName = Request.QueryString["topicid"].ToString();
旁注

不确定您是否知道,但您应该小心SQL注入。与其将查询字符串直接注入SQL查询,不如使用

string selectSQL = "SELECT * FROM PostView WHERE TopicID ='{0}';

然后将主题作为参数添加到SqlCommand中。

您不需要绑定到数据源,只需绑定到页面的简单属性即可。在HeaderTemplate中使用此选项:

<h1><%# TopicName %></h1>
然后在运行查询时设置它:

TopicName = Request.QueryString["topicid"].ToString();
旁注

不确定您是否知道,但您应该小心SQL注入。与其将查询字符串直接注入SQL查询,不如使用

string selectSQL = "SELECT * FROM PostView WHERE TopicID ='{0}';
然后将主题作为参数添加到SqlCommand中