C# 如何在公共静态void方法中创建消息框
我有一个公共静态方法,如果选择了某些值,我希望显示一条消息。这是在ASP.NET中,因此使用System.Windows.Forms添加C# 如何在公共静态void方法中创建消息框,c#,asp.net,static-methods,messagebox,C#,Asp.net,Static Methods,Messagebox,我有一个公共静态方法,如果选择了某些值,我希望显示一条消息。这是在ASP.NET中,因此使用System.Windows.Forms添加导致问题,因为我正在使用使用System.Web.UI.WebControls。那么如何创建一条消息呢 public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd) { if (SerNo.QtyRcvd != 1) { if (SerNo.Reason =
代码>导致问题,因为我正在使用使用System.Web.UI.WebControls代码>。那么如何创建一条消息呢
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
//message
}
}
}
代码后面的Javascript:
function UpdateSerialQtyRcvd(sender, SerNoID, QtyRcvd) {
if (QtyRcvd < 0) {
alert("Qty Rcvd must be greater than 0");
}
else {
PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
}
}
添加JavaScript警报
:
clientscriptmanager.registerstartupscript(this.GetType(),"MyAlert","<script>alert('Hello');</script>",true);
此方法显示一条消息。UpdateSerialQtyRcvd
位于WebUserControl.ascx
中,如您所需:
public static void UpdateSerialQtyRcvd(System.Web.UI.Page pg)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "alert", "<script>alert('Message');</script>");
}
现在,您可以从注册了WebUserControl.ascx
的每个页面调用UpdateSerialQtyRcvd
静态方法。像这样:
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>");
//}
}
}
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
throw new Exception("Your message");
}
}
}
function OnUpdateFail(result) {
alert(result.get_message());
}
YourPage.aspx
:
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="uc1" TagName="WebUserControl2" %>
<uc1:WebUserControl2 runat="server" id="WebUserControl2" />
更改业务方法以在出现错误情况时引发异常,如下所示:
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>");
//}
}
}
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
throw new Exception("Your message");
}
}
}
function OnUpdateFail(result) {
alert(result.get_message());
}
并处理javascript函数中的错误,如下所示:
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>");
//}
}
}
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
throw new Exception("Your message");
}
}
}
function OnUpdateFail(result) {
alert(result.get_message());
}
您可以从静态webmethod返回所需的消息。并在客户端使用Javascript警报发出警报消息
将webmethod的返回类型更改为字符串
[WebMethod]
public static string UpdateSerialQtyRcvdUserControl(int SerNoID, int QtyRcvd)
{
return JobDeliveryDebrief.UpdateSerialQtyRcvd(SerNoID, QtyRcvd);
}
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
//message
return "message";
}
}
}
将UpdateSerialQtyRcvd的返回类型更改为字符串
[WebMethod]
public static string UpdateSerialQtyRcvdUserControl(int SerNoID, int QtyRcvd)
{
return JobDeliveryDebrief.UpdateSerialQtyRcvd(SerNoID, QtyRcvd);
}
public static void UpdateSerialQtyRcvd(int SerNoID, int QtyRcvd)
{
if (SerNo.QtyRcvd != 1)
{
if (SerNo.Reason == "")
{
//message
return "message";
}
}
}
在Javascript警报中,无论您从onUpdate成功函数中的webmethod收到什么消息
function UpdateSerialQtyRcvd(sender, SerNoID, QtyRcvd) {
if (QtyRcvd < 0) {
alert("Qty Rcvd must be greater than 0");
}
else {
PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
}
}
函数更新SerialQTYRCVD(发送方、SerNoID、QtyRcvd){
如果(QtyRcvd<0){
警报(“数量Rcvd必须大于0”);
}
否则{
PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID、QtyRcvd、OnUpdate成功、OnUpdate失败);
}
}
使用ScriptManager.RegisterStartupScript(this,GetType(),“Message”,“alert('Message');”,true)代码>您搜索过一点吗?我在这里找到了几篇关于这个的帖子。@SuperOli是的,我已经搜索过了,但是我找不到解决方案OK,那么下次,请列出你在帖子中尝试过的东西。@SuperOli我找到了。我说我试过使用System.Windows.Forms;但是这不起作用..在发布OnUpdateSuccess、OnUpdateFail函数的代码后,您可以在其中显示导致错误的警告框:错误2496当前上下文中不存在名称“ClientScriptManager”是否真的是aspx.cs页面?它是一个ascx.cs页面使用ScriptManager.RegisterStartupScript(page,page.GetType(),“key”,“警报('Hello')”,false);
另一个错误:error 2496非静态字段、方法或属性System.Web.UI.Control.Page.get需要对象引用。
和error 2497非静态字段、方法或属性“object.GetType()”需要对象引用。