8

I have a public static method and I would like a message to be displayed if certain values are selected. This is in ASP.NET so adding using System.Windows.Forms; causes problems because I am using using System.Web.UI.WebControls;. So how do I create a message?

public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
     if (SerNo.QtyRcvd != 1)
     {
         if (SerNo.Reason == "")
         {
             //message
         }
     }
}

Javascript behing the code:

function UpdateSerialQtyRcvd(sender, SerNoID, QtyRcvd) {
        if (QtyRcvd < 0) {
            alert("Qty Rcvd must be greater than 0");
        }
        else {
            PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
        }
}

Calling the web method:

[WebMethod]
public static void UpdateSerialQtyRcvdUserControl(int SerNoID, int QtyRcvd)
{
     JobDeliveryDebrief.UpdateSerialQtyRcvd(SerNoID, QtyRcvd);
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
user123456789
  • 1,914
  • 7
  • 44
  • 100

5 Answers5

7

Add a JavaScript alert:

clientscriptmanager.registerstartupscript(this.GetType(),"MyAlert","<script>alert('Hello');</script>",true);

To use ClientScript inside a static method pass the Page object as a parameter to your static method.Page is not static class.You can not call non static fields inside static method.So you need to pass the page object to your static method as a parameter.

protected void Page_Load(object sender, EventArgs e)
{
     UpdateSerialQtyRcvd(SerNoID, QtyRcvd,Page);
}

public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd,Page page)
{
    if (SerNo.QtyRcvd != 1)
    {
        if (SerNo.Reason == "")
        {
            page.ClientScript.RegisterStartupScript(page.GetType(),"alert", "<script>alert('Hai');</script>");
        }
    }        
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
shreesha
  • 1,811
  • 2
  • 21
  • 30
  • that causes the error: Error 2496 The name 'ClientScriptManager' does not exist in the current context – user123456789 Aug 13 '15 at 14:05
  • is it really aspx.cs page?? – shreesha Aug 13 '15 at 14:17
  • it is an ascx.cs page – user123456789 Aug 13 '15 at 14:22
  • use `ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "", false);` – shreesha Aug 13 '15 at 14:25
  • another error: `Error 2496 An object reference is required for the non-static field, method, or property System.Web.UI.Control.Page.get` and `Error 2497 An object reference is required for the non-static field, method, or property 'object.GetType()'` – user123456789 Aug 13 '15 at 14:27
  • Try doing Page objPage=new Page();and replace page with objPage – shreesha Aug 13 '15 at 14:32
  • you are getting error because it is a static method – shreesha Aug 13 '15 at 14:35
  • But if I try call the LoadJavaScript function in my if statement I get the error: Error 47 An object reference is required for the non-static field, method, or property 'System.Web.UI.Control.Page.get'. I don't want to call the message in Page_Load, it needs to be called my UpdateSerialQtyRcvd function – user123456789 Aug 14 '15 at 08:32
  • This doesn't work. Gives the error invalid web service call. Missing value for parameter page – user123456789 Aug 17 '15 at 09:27
1

This method show a message. The UpdateSerialQtyRcvd is in a WebUserControl.ascx as you want:

public static void UpdateSerialQtyRcvd(System.Web.UI.Page pg)
{
     pg.ClientScript.RegisterStartupScript(pg.GetType(), "alert", "<script>alert('Message');</script>");
}

Now you can add your if-statement like this:

public static void UpdateSerialQtyRcvd(System.Web.UI.Page pg, int qtyRcvd)
{
     if (qtyRcvd != 1)
     {
          //if (SerNo.Reason == "")
          //{
                pg.ClientScript.RegisterStartupScript(pg.GetType(), "alert", "<script>alert('Message');</script>");
          //}
     }
}

Now you can call to UpdateSerialQtyRcvd static method from every page that has registered the WebUserControl.ascx. like this:

YourPage.aspx:

<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="uc1" TagName="WebUserControl2" %>
<uc1:WebUserControl2 runat="server" id="WebUserControl2" />

YourPage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl2.UpdateSerialQtyRcvd(this,2);
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

Change your business method to throw an exception when there is an error condition, like this:

public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
     if (SerNo.QtyRcvd != 1)
     {
         if (SerNo.Reason == "")
         {
             throw new Exception("Your message");
         }
     }
}

And handle the error in your javascript function, like this:

function OnUpdateFail(result) {
    alert(result.get_message());
}
Martín Misol
  • 333
  • 1
  • 7
1

Use ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('Message');", true);

Purvesh Desai
  • 1,797
  • 2
  • 15
  • 39
0

You could return desired message from static webmethod. And at the client side alert message using Javascript alert.

change return type of webmethod to string

[WebMethod]
public static string UpdateSerialQtyRcvdUserControl(int SerNoID, int QtyRcvd)
{
    return JobDeliveryDebrief.UpdateSerialQtyRcvd(SerNoID, QtyRcvd);
}

change return type of UpdateSerialQtyRcvd to string

public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
     if (SerNo.QtyRcvd != 1)
     {
         if (SerNo.Reason == "")
         {
             //message
             return "message";
         }
     }
}

In Javascript alert whatever message you get from webmethod in OnUpdateSuccess function

 function UpdateSerialQtyRcvd(sender, SerNoID, QtyRcvd) {
            if (QtyRcvd < 0) {
                alert("Qty Rcvd must be greater than 0");
            }
            else {
               PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
            }
    }
Arti
  • 2,993
  • 11
  • 68
  • 121