1

データベースの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>

しかし、私のヘッダーには何も表示されていません。ヘッダーでdatabinderを使用できないと聞きましたが、これを行う方法を誰かに勧めてもらえますか?

これは私のCSコードです

    string topic = Request.QueryString["topicid"].ToString();

    // Define the select statement.
    // All information is needed
    string selectSQL = "SELECT * FROM PostView WHERE TopicID ='" + topic + "'";

    // Define the ADO.NET Objects
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    con.Open();
    SqlDataReader postView = cmd.ExecuteReader();
    topicView.DataSource = postView;
    topicView.DataBind();
4

1 に答える 1

4

データソースにバインドする必要はありません。ページの単純なプロパティにバインドするだけです。これをHeaderTemplateで使用します。

<h1><%# TopicName %></h1>

次に、TopicNameをパブリックプロパティとして分離コードに追加します。

public string TopicName { get; set; }

次に、クエリを実行するときに設定します。

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

サイドノート

知っているかどうかはわかりませんが、SQLインジェクションには注意する必要があります。クエリ文字列をSQLクエリに直接挿入する代わりに、次を使用することをお勧めします。

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

次に、トピックをSqlCommandにパラメーターとして追加します。

于 2012-05-21T04:40:33.057 に答える