C# 如何在c语言中将函数转换成类或模块#
我只是在学习如何在我的项目中使用课堂。我一直在开发DataAccessClass.cs,我认为我做得很好。 从数据访问中休息一下,我决定尝试在类中创建一个空白。此void以javascript警报的形式向客户端发送消息。它运行良好,但必须包含在每一页中。当我试图将它变成一个类时,我被告知我的类不包含ClientScript的定义。我包括了原始页面中所有的“使用”指令,但没有用。。。如有任何提示或建议,将不胜感激 原代码:C# 如何在c语言中将函数转换成类或模块#,c#,javascript,.net,C#,Javascript,.net,我只是在学习如何在我的项目中使用课堂。我一直在开发DataAccessClass.cs,我认为我做得很好。 从数据访问中休息一下,我决定尝试在类中创建一个空白。此void以javascript警报的形式向客户端发送消息。它运行良好,但必须包含在每一页中。当我试图将它变成一个类时,我被告知我的类不包含ClientScript的定义。我包括了原始页面中所有的“使用”指令,但没有用。。。如有任何提示或建议,将不胜感激 原代码: //-----------------------------------
//------------------------------------------------------------------------------
//Name: SendErrorMessageToClient
//Abstract: show alert on client side
//------------------------------------------------------------------------------
protected void SendErrorMessageToClient(string strErrorType, string strErrorMessage)
{
string strMessageToClient = "";
//Allow single quotes on client-side in JavaScript
strErrorMessage = strErrorMessage.Replace("'", "\\'");
strMessageToClient = "<script type=\"text/javascript\" language=\"javascript\">alert( '" + strErrorType + "\\n\\n" + strErrorMessage + "' );</script>";
this.ClientScript.RegisterStartupScript(this.GetType(), "ErrorMessage", strMessageToClient);
}
或者这个:
catch (Exception excError)
{
string strErrorType = "Unhandled Exception:";
string strErrorMessage = excError.Message;
SendErrorMessageToClient(strErrorType, strErrorMessage);
}
ClientScript
属性是每个ASPX
页面继承的Page
类的一部分。因此,您不能仅从类内部使用它,除非它(即您的类)将Page
作为其基类。这是针对类中方法中的字段。您收到错误,因为“Clientscript”是从System.Web.UI.Page派生的属性,并且通过移动到单独的类文件,您不再有权访问此属性
您也可以通过传入页面并将代码修改为
protected void SendErrorMessageToClient(string strErrorType, string strErrorMessage, Page page)
{
string strMessageToClient = "";
//Allow single quotes on client-side in JavaScript
strErrorMessage = strErrorMessage.Replace("'", "\\'");
strMessageToClient = "<script type=\"text/javascript\" language=\"javascript\">alert( '" + strErrorType + "\\n\\n" + strErrorMessage + "' );</script>";
page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorMessage", strMessageToClient);
}
受保护的void senderErrorMessageToClient(字符串strErrorType,字符串strErrorMessage,第页)
{
字符串strMessageToClient=“”;
//在JavaScript中允许客户端使用单引号
strErrorMessage=strErrorMessage.Replace(“'”,“\\”);
strMessageToClient=“警报(“+strErrorType+”\\n\\n“+strErrorMessage+”);”;
page.ClientScript.RegisterStartupScript(this.GetType(),“ErrorMessage”,strMessageGetOClient);
}
您需要将页面传递到函数中
SendErrorMessageToClient(Page page, string strErrorType, string strErrorMessage)
这样你就可以改变
this.ClientScript...
到
原因是ClientScript是Page类的一部分
或者,最好是传递ClientScript对象,而不是页面。
所以你的定义应该是
SendErrorMessageToClient(ClientScript clientScript, string strErrorType, string strErrorMessage) {
string strMessageToClient = "";
//Allow single quotes on client-side in JavaScript
strErrorMessage = strErrorMessage.Replace("'", "\\'");
strMessageToClient = String.Format("<script type='text/javascript' language='javascript'>alert('{0}\\n\\n{1}');</script>",
strErrorType, strErrorMessage);
clientScript.RegisterStartupScript(this.GetType(), "ErrorMessage", strMessageToClient);
}
SendErrorMessageToClient(客户端脚本客户端脚本、字符串strErrorType、字符串strErrorMessage){
字符串strMessageToClient=“”;
//在JavaScript中允许客户端使用单引号
strErrorMessage=strErrorMessage.Replace(“'”,“\\”);
strMessageToClient=String.Format(“警报({0}\\n\\n{1}”);”,
strErrorType、strErrorMessage);
RegisterStartupScript(this.GetType(),“ErrorMessage”,strMessageGetOClient);
}
为什么要将其设置为类?它不应该是一个类,它没有任何属性或字段,除非你能想到一个。你知道如果你把它变成一个类,你仍然需要在每一页上初始化它
您可以将其设置为静态字符串方法,但仍必须在每个页面上包含此方法
this.ClientScript.RegisterStartupScript(this.GetType(),“ErrorMessage”,strMessageGetOClient)在this.ClientScript中,“this”引用当前页面对象实例。如果将该代码放入一个单独的类中,除非新类继承System.Web.UI.Page,否则该类无法知道ClientScript是什么。但是您仍然需要将当前页面实例的引用传递给它,以便它能够在所讨论的页面上操作。新类定义的代码是什么样子的?返回字符串不是更容易吗?然后使用当前方法?@Ramhound显示字符串,而不是使用名为SendErrorMessageToClient
的方法。我在VB的模块中放入了类似的内容,但在C#中没有找到等效的构造。我只是想消除重复的代码。
page.ClientScript...
SendErrorMessageToClient(ClientScript clientScript, string strErrorType, string strErrorMessage) {
string strMessageToClient = "";
//Allow single quotes on client-side in JavaScript
strErrorMessage = strErrorMessage.Replace("'", "\\'");
strMessageToClient = String.Format("<script type='text/javascript' language='javascript'>alert('{0}\\n\\n{1}');</script>",
strErrorType, strErrorMessage);
clientScript.RegisterStartupScript(this.GetType(), "ErrorMessage", strMessageToClient);
}