Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET页面加载事件中注册并执行JavaScript函数_Javascript_Asp.net_Function - Fatal编程技术网

在ASP.NET页面加载事件中注册并执行JavaScript函数

在ASP.NET页面加载事件中注册并执行JavaScript函数,javascript,asp.net,function,Javascript,Asp.net,Function,如何在页面加载事件中在ASP.NET中注册和执行JavaScript函数,以便验证文本框内容,如果其中没有内容,我可以禁用保存按钮 function Validate(source, arguments) { } 使用 例如,见 <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xh

如何在页面加载事件中在ASP.NET中注册和执行JavaScript函数,以便验证文本框内容,如果其中没有内容,我可以禁用保存按钮

function Validate(source, arguments)
{
}
使用

例如,见

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
        StringBuilder cstext1 = new StringBuilder();
        cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
        cstext1.Append("script>");

        cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RegisterStartupScript</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

公共无效页面加载(对象发送方,事件参数e)
{
//定义页面上客户端脚本的名称和类型。
字符串csname1=“popuscript”;
类型cstype=this.GetType();
//从Page类获取ClientScriptManager引用。
ClientScriptManager cs=Page.ClientScript;
//检查启动脚本是否已注册。
如果(!cs.isstartupscript已注册(cstype,csname1))
{
StringBuilder cstext1=新的StringBuilder();
追加(“警报('Hello World!'”);
RegisterStartupScript(cstype,csname1,cstext1.ToString());
}
}
RegisterStartupScript

希望这有帮助

是否需要一个字段验证器为您执行此操作?为什么要使用“原始”javascript?你的意思是你想执行一个javascript,得到一个结果,并根据它的“禁用”按钮,在页面加载上完成所有这些?你为什么要尝试使用javascript?您的意思是希望在
页面中执行此检查并禁用提交按钮,因此
文本框的值由后面的代码而不是用户设置(否则您将使用
RequiredFieldValidator
CustomValidator
进行检查),那么,为什么不直接在代码隐藏中执行检查呢?@5 RequiredFieldValidator只在再次输入和删除某些内容时进行验证。它应该在页面加载事件中触发!如果是-您可以在页面加载事件中引用文本框和保存按钮。如果文本框text=string.empty,请禁用按钮。您可以使用下面的registerstartupscript,但是您在IMO中做了额外的工作。如果javascript是动态生成的,那么如何调试它呢?您可以创建not dynamic函数,并动态调用该函数,它只是一个示例@msfanboyits,因为我搜索的html控件为null,所以它不会在页面末尾执行。