16

アイテムの保存が成功したときにメッセージ ボックスを表示したい。私はそれをグーグルで検索し、さまざまな解決策を試しましたが、どれもうまくいきませんでした。私が使用しているコードは次のとおりです。

try
{
    con.Open();
    string pass="abc";
    cmd = new SqlCommand("insert into register values('" + 
                                       txtName.Text + "','" + 
                                       txtEmail.Text + "','" + 
                                       txtPhoneNumber.Text + "','" + 
                                       ddlUserType.SelectedText + "','" + 
                                       pass + "')", con);

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");
}
catch (Exception ex)
{

}
finally
{
    con.Close();
}

(それが重要な場合、私はFirefoxを使用しています)

4

13 に答える 13

29

@freelancer ScriptManagerを使用している場合は、メッセージにこのコードを試してください..

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);
于 2013-03-04T09:25:02.507 に答える
16

ページに MsgBox のメソッドを作成します。


public void MsgBox(String ex, Page pg,Object obj) 
{
    string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Type cstype = obj.GetType();
    ClientScriptManager cs = pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
}

msgbox を使用する場合は、次の行を入力するだけです

MsgBox("! your message !", this.Page, this);
于 2013-12-20T06:57:41.953 に答える
12

これを試してみてください。私のブラウザでは問題なく動作します。

あなたの応答書き込みコードは

Response.Write("<script>alert('login successful');</script>");

これがうまくいくことを願っています

于 2013-03-04T07:37:34.627 に答える
2

このコードは、asp.net ファイルに MsgBox を追加するのに役立ちます。関数定義は要件に合わせて変更できます。お役に立てれば!

protected void Addstaff_Click(object sender, EventArgs e)    
    {   
 if (intClassCapcity < intCurrentstaffNumber)     
                {                  
            MsgBox("Record cannot be added because max seats available for the " + (string)Session["course_name"] + " training has been reached");    
        }    
else    
    {   
            sqlClassList.Insert();    
    }    
}

private void MsgBox(string sMessage)    
    {    
        string msg = "<script language=\"javascript\">";    
        msg += "alert('" + sMessage + "');";    
        msg += "</script>";    
        Response.Write(msg);    
    }
于 2015-03-26T15:02:36.967 に答える
2

you can use clientscript. MSDN : Clientscript

String scriptText = 
        "alert('sdsd');";
    ClientScript.RegisterOnSubmitStatement(this.GetType(), 
        "ConfirmSubmit", scriptText);

try this

ClientScript.RegisterStartupScript(this.GetType(), "JSScript", scriptText); 



ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", scriptText); //use this 
于 2013-03-04T07:23:37.713 に答える
2

私はこれを使用し、動作します

public void Messagebox(string xMessage)
{
    Response.Write("<script>alert('" + xMessage + "')</script>");
}

そして、私はこのように呼びます

    Messagebox("je suis la!");
于 2019-05-02T19:18:59.543 に答える
0

「alert("Hello this is an Alert")」という 1 行のコードを持つ単純な JavaScript 関数を作成し、代わりに OnClick() で OnClientClick() メソッドを使用します。

`<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <title></title>
 <script type="text/javascript" language="javascript">
    function showAlert() {
        alert("Hello this is an Alert")
    }
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="showAlert()" />
 </div>
 </form>
 </body>
 </html>`
于 2013-08-20T10:51:27.457 に答える
0

Response.Write is used to display the text not for executing JavaScript, If you want to execute the JavaScript from your code than try as below:

try
{
    con.Open();
    string pass="abc";
    cmd = new SqlCommand("insert into register values('" + txtName.Text + "','" + txtEmail.Text + "','" + txtPhoneNumber.Text + "','" + ddlUserType.SelectedText + "','" + pass + "')", con);
    cmd.ExecuteNonQuery();
    con.Close();
    Page.ClientScript.RegisterStartupScript(this.GetType(), "click","alert('Login Successful');");
}
catch (Exception ex)
{
}
finally
{
    con.Close();
}
于 2013-03-04T07:27:35.843 に答える