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

时间:2015-07-01 04:55:26

标签: c# asp.net

使用c#ASP.NET在数据库中提交数据后,我无法显示警告消息。我正在解释下面的代码。

  

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>

我的要求是,我在每次提交价值后都会显示成功/错误消息。

4 个答案:

答案 0 :(得分:0)

我在服务器端使用以下代码警告弹出窗口。虽然它是VB你可以转换它。

var matrix = [ ]

答案 1 :(得分:0)

公共类MessageBox {     private static 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() );
    }
  }

}

你可以使用MessageBox类调用:MessageBox.Show(“TEST”);就是这样。

答案 2 :(得分:0)

使用以下解决方案来获取来自c#

的提示消息

只需在代码中调用所需的GetMessage函数,如下面的行所示

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

以下提供的功能用于警报提示

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);
}

如果邮件或邮件类型包含"',那么您必须使用Replace来避免问题,例如message.Replace("'", " ");

答案 3 :(得分:0)

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>