我想做的是,当我单击按钮时,它应该显示一个警告框。这是代码。
protected void btnAdd_Click(object sender, EventArgs e)
{
string script = "<script type="text/javascript">alert('abc 1');
</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1",
script);
Response.Redirect("~/Index.aspx");
}
它运行良好,但是当我将相同的代码放在另一个页面中时,它不起作用。
我必须研究哪些事情? 有什么建议吗?
将JS代码移动到客户端
<asp:Button ID="btnAdd" OnClientClick="alert('alert');" OnClick="btnAdd_Click" runat="server"/>
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Index.aspx");
}
也许你可以试试这个 - 语法可能是错误的 -
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Index.aspx?Issuccess=1");
}
index.aspx page -
protected void Page_Load()
{
if(Request.QueryString["Issuccess"] != null && Convert.ToInt32(Request.QueryString["Issuccess"]) == 1)
{
string script = "<script type="text/javascript">alert('abc 1');
</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1",
script);
}
}
在 ClientScript.RegisterScript 中传递最后一个参数 True 或 False,最好以这种方式传递警报消息。
喜欢这个
protected void btnAdd_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('abc 1');",true);
Response.Redirect("~/Index.aspx");
}