ASP.NET 中的 Javascript 警报

发布于 2024-11-02 02:32:48 字数 1559 浏览 3 评论 0原文

我想在我的 ASP.NET 页面中使用 Javascript Alert 功能。

例如这样;

Response.Write("<script language=javascript>alert('ERROR');</script>);

但是,这行不通。

我在这里问我做错了什么,每个人都建议我使用 RegisterScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);

但我不想使用它,因为它与 PostBack 一起使用,

如果没有 PostBack,我该如何做到这一点?

编辑:例如使用;

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}

I want to use Javascript Alert function in my ASP.NET page.

For example like this;

Response.Write("<script language=javascript>alert('ERROR');</script>);

But, this doesn't work.

I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);

But i don't want use it because it's working with PostBack

How can i do that without PostBack?

EDIT: For example for using;

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑白记忆 2024-11-09 02:32:48

请参阅来自 MSDN 的注释

如果你想注册一个脚本块
不属于部分页面
更新,如果您想注册
脚本在执行过程中仅阻塞一次
初始页面渲染,使用
RegisterClientScriptBlock 方法
ClientScriptManager 类。你可以
获取参考
来自的 ClientScriptManager 对象
页面的 ClientScript 属性。

所以,我认为 ClientScriptManager.RegisterStartupScript 方法是你所需要的:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    " ", 
    @"<script language=javascript>alert('ERROR');</script>", 
    true
);

See a note from MSDN:

If you want to register a script block
that does not pertain to partial-page
updates, and if you want to register
the script block only one time during
initial page rendering, use the
RegisterClientScriptBlock method of
the ClientScriptManager class. You can
get a reference to the
ClientScriptManager object from the
ClientScript property of the page.

So, I think ClientScriptManager.RegisterStartupScript method is what you need:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    " ", 
    @"<script language=javascript>alert('ERROR');</script>", 
    true
);
墨小墨 2024-11-09 02:32:48

在您的代码中您忘记了引号。我刚刚在这样的示例页面中尝试过它:

Response.Write("<script language=javascript>alert('ERROR');</script>");

并且它有效。您将 Response.Write 放在代码中的什么位置?你能提供更多细节吗?你想让我做什么?

In your code you forgot the quotation mark. I just tried it in a sample page like this:

Response.Write("<script language=javascript>alert('ERROR');</script>");

and it worked. Where did you place the Response.Write in your code? Could you give some more details? What do you want to do?

怪我太投入 2024-11-09 02:32:48

为了向用户显示警报消息,在网页中我有一个代码看看这个

public void UserMsgBox(string sMsg)
{
StringBuilder sb = new StringBuilder();
System.Web.UI.Control oFormObject = null;
sMsg = sMsg.Replace("'", "\\'");
sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
sb = new StringBuilder();
sb.Append(sMsg);
foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
    oFormObject = oFormObject_loopVariable;
    if (oFormObject is HtmlForm) {
        break; // TODO: might not be correct. Was : Exit For
    }
}
oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
}

For displaying an alert message to the User, in a webpage i have a code have a look at this

public void UserMsgBox(string sMsg)
{
StringBuilder sb = new StringBuilder();
System.Web.UI.Control oFormObject = null;
sMsg = sMsg.Replace("'", "\\'");
sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
sb = new StringBuilder();
sb.Append(sMsg);
foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
    oFormObject = oFormObject_loopVariable;
    if (oFormObject is HtmlForm) {
        break; // TODO: might not be correct. Was : Exit For
    }
}
oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
}
り繁华旳梦境 2024-11-09 02:32:48

尝试使用 RegisterStartupscript 来注册脚本。
请参阅:http://msdn.microsoft.com /en-us/library/system.web.ui.page.registerstartupscript.aspx

try using RegisterStartupscript for registering the script.
Refer : http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文