0

電子メール ID、パスワード、その他の詳細を含む多くのテキスト ボックスがあるオンライン フォームがあります。ユーザーが正しい情報を入力しなかった場合。以下のコードに示すエラーメッセージを表示しています。SQL からの戻り値を使用しています。ボタンをクリックするたびにラベルをクリアしようとしています。誰でも私を助けることができますか?

protected void Button1_Click(object sender, EventArgs e)
    {
        {

            {
                SqlConnection sqlCon = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand("UpdateRequestAccess_Test", sqlCon);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "UpdateRequestAccess_Test";
                cmd.Connection = sqlCon;

                ----------Parameters are declared here--------

                SqlParameter rpv = new SqlParameter();
                rpv.DbType = DbType.Int32;
                rpv.Direction = ParameterDirection.ReturnValue;

                cmd.Parameters.Add(rpv);
                try
                {
                    sqlCon.Open();
                    cmd.ExecuteScalar();

                    int retValue = Convert.ToInt32(rpv.Value);


                        if (retValue == 10)
                            lblMessage.Text = "Request was sent successfully!";

                        if (retValue == 11)
                            Label2.Text = "*Email Address is already registered.";

                        if (retValue == 12)
                            Label3.Text = "*Passwords do not match.";

                        if (retValue == 13)
                            Label4.Text = "Sorry, Your application was already denied earlier.";

                        if (retValue == 14)
                            Label5.Text = "*Please select an option 'Yes' or 'No' under Select Online Tools.";

                        if (retValue == 15)
                            Label6.Text = "*Please enter the information in the text boxes above.";

                        if (retValue == 15)
                            Label7.Text = "*Please Select an option from the dropdown above.";

                }

                catch (Exception ex)
                {

                        lblMessage.Text = ex.Message;

                        Label2.Text = ex.Message;

                        Label3.Text = ex.Message;

                        Label4.Text = ex.Message;

                        Label5.Text = ex.Message;

                        Label6.Text = ex.Message;

                        Label7.Text = ex.Message;

                }

            }
        }

    }
4

4 に答える 4

1

ClearTextMsg(); を入れます。ページ読み込みイベントに。

ページの読み込みで if(!IsPostBack) { } コード ブロックを使用する場合は、その外側に配置します。

パーフェクト ポジションは、ページ ロード イベントの最初の行です。次のように

protected void Page_Load(object sender, EventArgs e)
{
     ClearTextMsg();
     if (!IsPostBack)
     {
        // any valid c# statments
     }
}

// call this function
private void ClearTextMsg()
{
lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
}
于 2013-11-21T07:14:23.280 に答える
1

以下のようにボタンクリックイベントを更新します

protected void Button1_Click(object sender, EventArgs e)
{
                    lblMessage.Text = string.Empty;

                    Label2.Text = string.Empty;

                    Label3.Text = string.Empty;

                    Label4.Text = string.Empty;

                    Label5.Text = string.Empty;

                    Label6.Text = string.Empty;

                    Label7.Text = string.Empty;


    {

        {
            SqlConnection sqlCon = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("UpdateRequestAccess_Test", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateRequestAccess_Test";
            cmd.Connection = sqlCon;

            ----------Parameters are declared here--------

            SqlParameter rpv = new SqlParameter();
            rpv.DbType = DbType.Int32;
            rpv.Direction = ParameterDirection.ReturnValue;

            cmd.Parameters.Add(rpv);
            try
            {
                sqlCon.Open();
                cmd.ExecuteScalar();

                int retValue = Convert.ToInt32(rpv.Value);


                    if (retValue == 10)
                        lblMessage.Text = "Request was sent successfully!";

                    if (retValue == 11)
                        Label2.Text = "*Email Address is already registered.";

                    if (retValue == 12)
                        Label3.Text = "*Passwords do not match.";

                    if (retValue == 13)
                        Label4.Text = "Sorry, Your application was already denied earlier.";

                    if (retValue == 14)
                        Label5.Text = "*Please select an option 'Yes' or 'No' under Select Online Tools.";

                    if (retValue == 15)
                        Label6.Text = "*Please enter the information in the text boxes above.";

                    if (retValue == 15)
                        Label7.Text = "*Please Select an option from the dropdown above.";

            }

            catch (Exception ex)
            {

                    lblMessage.Text = ex.Message;

                    Label2.Text = ex.Message;

                    Label3.Text = ex.Message;

                    Label4.Text = ex.Message;

                    Label5.Text = ex.Message;

                    Label6.Text = ex.Message;

                    Label7.Text = ex.Message;

            }

        }
    }

}
于 2013-12-13T10:23:00.780 に答える
1
lblMessage.Text = ex.Message;
Label2.Text = ex.Message;
Label3.Text = ex.Message;
Label4.Text = ex.Message;
Label5.Text = ex.Message;
Label6.Text = ex.Message;
Label7.Text = ex.Message;

これらの行をコードの先頭、trytry-catch ステートメントのすぐ上に追加してみてください。

または多分あなたができる

Label7.Clear(); 

ただし、構文についてはわかりません。私はvb.netプログラマーです:)

于 2013-11-19T06:52:55.307 に答える
1

試す..

lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
于 2013-11-12T01:34:11.863 に答える