0

ASPで自分のウェブサイトの画像にコメントを追加しようとしています。ネット、c# を使用。画像に gridview を使用しています。コメントを挿入するために、グリッドに新しいテキスト ボックス ファイルを追加します。CommentID、UserId、Body、および id_image を含む ImageComments という名前のテーブルを 1 つ追加します。これが私のコードです。

 protected void addReviewButton_Click(object sender, EventArgs e)
    {

        MembershipUser currentUser = Membership.GetUser();
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        string connectionString =
        ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string insertSql = "INSERT INTO ImageComments(Body, UserId) VALUES(@Body, @UserId)";
        TextBox reviewTextBox = GridView1.FindControl("reviewTextBox1") as TextBox;

        string a = reviewTextBox.Text;

            using (SqlConnection myConnection = new SqlConnection(connectionString))
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
                myCommand.Parameters.AddWithValue("@Body", a);
                myCommand.Parameters.AddWithValue("@UserID", currentUserId);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }

            reviewTextBox.Text = string.Empty;

        }

    }

オブジェクトのインスタンスに設定されていないオブジェクト参照という有名なエラーが発生しました。メソッド FindControl() に問題があると思います。TextBox (コメント ファイル) がグリッドの外側にある場合に問題がないためです。このエラーで約 1 週間立ち往生しています。誰か知ってる人いたら助けてください

4

1 に答える 1

1

reviewTextBox に問題がある場合は、以下のコードを試して、グリッドビューでテキストボックスを見つけることができます

Button button = sender as Button;
TextBox reviewTextBox = button.NamingContainer.FindControl("reviewTextBox1") as TextBox;
于 2013-09-16T06:13:01.083 に答える