简体   繁体   English

如何使用c#ASP.NET显示警报提示?

[英]How to display alert prompt using c# ASP.NET?

I can not show the alert message after data submitted inside DB using c# ASP.NET.I am explaining my code below. 使用c#ASP.NET在DB内部提交数据后,我无法显示警告消息。我在下面解释我的代码。

mission.aspx.cs: mission.aspx.cs:

protected void submit_Click(object sender, EventArgs e)
{

    missionBO objMissionBo = new missionBO();
    if (HiddenField1.Value == "")
    {
        objMissionBo.heading = TextBox1.Text.Trim();
        if (insertimage.HasFile)
        {
            //int length = insertimage.PostedFile.ContentLength;
            string filename = insertimage.FileName;
            insertimage.PostedFile.SaveAs(Server.MapPath(@"~\Upload\" + filename.Trim()));
            string path = filename.Trim();
            //byte[] imgbyte = new byte[length];
            //HttpPostedFile img = insertimage.PostedFile;
            //img.InputStream.Read(imgbyte, 0, length);
            objMissionBo.image = path;

        }
        objMissionBo.description = TextBox2.Text.Trim();
        missionvissionBL objMissionBL = new missionvissionBL();
        string action = "insert";
        int result = objMissionBL.insertMissionData(objMissionBo, action);
        if (result == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "prompt", "var value = prompt('Data inserted successfully.'); storeinput(value);", true);
            clearAll();
            Response.Redirect("missionvision.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "prompt", "var value = prompt('Data could not inserted successfully.'); storeinput(value);", true);
        }
    }

<script>
    function storeinput(value) {
        console.log('value',value);
        document.getElementById("<%=hidValue.ClientID%>").value = value;
    }
</script>

My requirement here, is that I have show the success/ error message after every submit of value. 我在这里的要求是,每次提交值后,我都必须显示成功/错误消息。

I use the following code the alert a popup on serverside. 我使用以下代码在服务器端弹出警报。 Although it is VB you can just convert it. 尽管它是VB,但您可以将其转换。

Dim strMsg As String
strMsg = "<script language=javascript> alert('Hi'); </script>"
Page.RegisterStartupScript("alert", strMsg)

public class MessageBox { private static Hashtable m_executingPages = new Hashtable(); 公共类MessageBox {私有静态Hashtable m_executingPages = new Hashtable();

public MessageBox(){}

/// <summary>
/// Show alert window
/// </summary>
/// <param name="sMessage">Alert text</param>
public static void Show(string sMessage)
{
    // If this is the first time a page has called this method then
    if (!m_executingPages.Contains(HttpContext.Current.Handler))
    {
        // Attempt to cast HttpHandler as a Page.
        Page executingPage = HttpContext.Current.Handler as Page;

        if (executingPage != null)
        {
            // Create a Queue to hold one or more messages.
            Queue messageQueue = new Queue();

            // Add our message to the Queue
            messageQueue.Enqueue(sMessage);

            // Add our message queue to the hash table. Use our page reference
            // (IHttpHandler) as the key.
            m_executingPages.Add(HttpContext.Current.Handler, messageQueue);

            // Wire up Unload event so that we can inject 
            // some JavaScript for the alerts.
            executingPage.Unload += new EventHandler(ExecutingPage_Unload);
        }
    }
    else
    {
        // If were here then the method has allready been 
        // called from the executing Page.
        // We have allready created a message queue and stored a
        // reference to it in our hastable. 
        Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];

        // Add our message to the Queue
        queue.Enqueue(sMessage);
    }
}

  private static void ExecutingPage_Unload(object sender, EventArgs e)
  {
    // Get our message queue from the hashtable
    Queue queue = (Queue) m_executingPages[ HttpContext.Current.Handler ];

    if( queue != null )
    {
      StringBuilder sb = new StringBuilder();

      // How many messages have been registered?
      int iMsgCount = queue.Count;

      // Use StringBuilder to build up our client slide JavaScript.
      sb.Append( "<script language='javascript'>" );

      // Loop round registered messages
      string sMsg;
      while( iMsgCount-- > 0 )
      {
        sMsg = (string) queue.Dequeue();
        sMsg = sMsg.Replace( "\n", "\\n" );
        sMsg = sMsg.Replace( "\"", "'" );
        sb.Append( @"alert( """ + sMsg + @""" );" );
      }

      // Close our JS
      sb.Append( @"</script>" );

      // Were done, so remove our page reference from the hashtable
      m_executingPages.Remove( HttpContext.Current.Handler );

      // Write the JavaScript to the end of the response stream.
      HttpContext.Current.Response.Write( sb.ToString() );
    }
  }

} }

You can use MessageBox class just call : MessageBox.Show("TEST"); 您可以使用MessageBox类,只需调用:MessageBox.Show(“ TEST”); and that`s it. 就是这样。

Use below solution for prompt message from c# 将以下解决方案用于来自C#的提示消息

Just call where needed GetMessage function in your code as shown in below line 只需在代码中调用需要的GetMessage函数即可,如下行所示

GetMessage("warning","Your message which you want to disply")

Below provided function is for alert prompt 下面提供的功能用于警报提示

public void GetMessage(string messageType, string message)
{
   StringBuilder sb = new StringBuilder();
   sb.Append("<script type='text/javascript'>");
   sb.Append("alert('" + messageType + " : " + message + "');");
   sb.Append("</script>");
   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ajax", sb.ToString(), false);
}

if message or message type will contain " or ' then you have to use Replace to avoid problems for example message.Replace("'", " "); 如果消息或消息类型包含"'则必须使用“ Replace以避免出现问题,例如message.Replace("'", " ");

protected void submit_Click(object sender, EventArgs e)
{

    missionBO objMissionBo = new missionBO();
    if (HiddenField1.Value == "")
    {
        objMissionBo.heading = TextBox1.Text.Trim();
        if (insertimage.HasFile)
        {
            //int length = insertimage.PostedFile.ContentLength;
            string filename = insertimage.FileName;
            insertimage.PostedFile.SaveAs(Server.MapPath(@"~\Upload\" + filename.Trim()));
            string path = filename.Trim();
            //byte[] imgbyte = new byte[length];
            //HttpPostedFile img = insertimage.PostedFile;
            //img.InputStream.Read(imgbyte, 0, length);
            objMissionBo.image = path;

        }
        objMissionBo.description = TextBox2.Text.Trim();
        missionvissionBL objMissionBL = new missionvissionBL();
        string action = "insert";
        int result = objMissionBL.insertMissionData(objMissionBo, action);
        if (result == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "prompt", "alert('Data inserted successfully.'); storeinput('"+result +"');", true);
            clearAll();
            Response.Redirect("missionvision.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "prompt", "alert('Data could not inserted successfully.'); storeinput('"+result +"');", true);
        }
    }

<script>
    function storeinput(value) {
        console.log('value',value);
        document.getElementById("<%=hidValue.ClientID%>").value = value;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM