弹出在按钮';的操作

本文关键字:操作 按钮 | 更新日期: 2023-09-27 18:24:36

在我的aspx页面中,我有一个按钮可以在数据库中保存几个值。

我需要在操作结束时插入一个popuup。

目前的代码是:

protected void btnSendRequest_Click(object sender, EventArgs e)
{
    var myDbAccess = new DBAccess();
    Event newEvent = (Event)Session["NewEvent"];
    myDbAccess.SaveEvent(newEvent);
    // Insert here a PopUp like "Successfully saved!"
}

我怎样才能做到这一点?

Luigi

弹出在按钮';的操作

您可以通过以下操作实现:

protected void btnSendRequest_Click(object sender, EventArgs e)
{
    var myDbAccess = new DBAccess();
    Event newEvent = (Event)Session["NewEvent"];
    myDbAccess.SaveEvent(newEvent);
    // Insert here a PopUp like "Successfully saved!"
   string script = "<script type='"text/javascript'">alert('successfully saved');</script>";
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}

我希望这能有所帮助。干杯:)

您应该使用ClientScript.RegisterStartupScript打开弹出窗口。

示例

protected void btnSendRequest_Click(object sender, EventArgs e)
{
    var myDbAccess = new DBAccess();
    Event newEvent = (Event)Session["NewEvent"];
    myDbAccess.SaveEvent(newEvent);
    // Insert here a PopUp like "Successfully saved!"
    var popupCode = @"<script>alert ('Javascript block of code executed')</script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(),"myKey",popupCode);
}

你也可以通过使用像下面这样的重载从字符串中删除块

protected void btnSendRequest_Click(object sender, EventArgs e)
{
    var myDbAccess = new DBAccess();
    Event newEvent = (Event)Session["NewEvent"];
    myDbAccess.SaveEvent(newEvent);
    // Insert here a PopUp like "Successfully saved!"
    var popupCode = @"alert ('Javascript block of code executed');";
    ClientScript.RegisterClientScriptBlock(this.GetType(),"myKey",popupCode,true);
}

注意RegisterClientScriptBlock方法末尾的TRUE值。

试试上面的代码,让我知道发生了什么。

您可以这样使用:

   string Scr=@"<script language='javascript'></script>";
   ClientScriptManager cs = Page.ClientScript;
   cs.RegisterStartupScript(this.GetType(), "MyScript",Scr, false);
   cs.RegisterClientScriptBlock(this.GetType(), "My alert", "alert('" Saved Successfully"');", true);

注意,第一个RegisterStartupScript具有False作为最后一个参数,下一个具有True

有关更多帮助:-RegisterClientScript

Jquery模型对话框是最好的。你可以很容易地将其与asp.net集成。
  • jquery方法
  • 您可以从代码中使用RegisterStartupScript方法后面:

public void SomeButton_Click(Object sender, EventArgs e)
   {
       string script = "$('#someid').dialog('open');";
       ClientScript.RegisterStartupScript(GetType(), "popup", script, true);
   }

  • jquery插件或Colorbox
  • ModalPopupExtender