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);
}
The keyword public static void main is the means by which you create a main method within the Java application. It's the core method of the program and calls all others. It can't return values and accepts parameters for complex command-line processing.
static: It means Main Method can be called without an object. public: It is access modifiers which means the compiler can execute this from anywhere. void: The Main method doesn't return anything.
static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. void means that the method has no return value. If the method returned an int you would write int instead of void .
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);
}
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>");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With