0

ListViewを使用してその特定のコメントを投稿したユーザーに応じてコメントを削除するにはどうすればよいですか?ボタンを追加してVisiblefalseにしたので、現在のログインユーザーがコメントを投稿したユーザーであることを確認したときに.csコードで、削除ボタンが表示されます。現在、私はこれを試しました:

protected void PostCommentButton_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
        return;


    MembershipUser currentUser = Membership.GetUser();
    Guid currentUserId = (Guid)currentUser.ProviderUserKey;


    string connectionString = ConfigurationManager.ConnectionStrings["CommentConnectionString"].ConnectionString;
    string insertSql = "INSERT INTO Comments(Subject, Body, UserId) VALUES(@Subject, @Body, @UserId)";

    using (SqlConnection myConnection = new SqlConnection(connectionString))
    {
        myConnection.Open();

        SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
        myCommand.Parameters.AddWithValue("@Subject", Subject.Text.Trim());
        myCommand.Parameters.AddWithValue("@Body", Body.Text.Trim());
        myCommand.Parameters.AddWithValue("@UserId", currentUserId);

        myCommand.ExecuteNonQuery();

        myConnection.Close();

        if (currentUser.UserName == Eval("UserName").ToString())
    {
        Control deleteButton = e.Item.FindControl("Button2");
        deleteButton.Visible = true;
    }

}

しかし、これは私にエラーを与えています。'System.EventArgs'には'Item'の定義が含まれておらず、タイプ'System.EventArgs'の最初の引数を受け入れる拡張メソッド'Item'が見つからなかったと記載されています。このコードをPostCommentボタンに配置しました。私はどこかで間違ったことをしましたか?

これが表示されているエラーです。

コンパイルエラー

説明:このリクエストを処理するために必要なリソースのコンパイル中にエラーが発生しました。以下の特定のエラーの詳細を確認し、ソースコードを適切に変更してください。

コンパイラエラーメッセージ:CS1061:「System.EventArgs」に「Item」の定義が含まれておらず、「System.EventArgs」タイプの最初の引数を受け入れる拡張メソッド「Item」が見つかりませんでした(usingディレクティブまたはアセンブリリファレンス?)

ソースエラー:

Line 59:         TextBox2.Text = string.Empty;
Line 60: 
Line 61:         if (e.Item.ItemType == ListViewItemType.DataItem)
Line 62:         {
Line 63:             ListViewDataItem currentItem = (ListViewDataItem)e.Item;
4

1 に答える 1

0

Button2がページにある場合は、次のように参照を見つけることができます。

Button btn = Page.FindControl("Button2");

リストビュー内にある場合は、次のように見つけることができます

if (e.Item.ItemType == ListViewItemType.DataItem)
{
   ListViewDataItem currentItem = (ListViewDataItem)e.Item;
   Button btn = (Button)currentItem.FindControl("Button2");
}
于 2012-07-28T17:26:31.133 に答える