问题描述
button单击事件:protectedvoidButton1_Click(objectsender,EventArgse){ClientScript.RegisterStartupScript(this.GetType(),"aa","<script>hello()</script>");}前台js:<scripttype="text/javascript">functionhello(){alert("你好");}</script>报错:缺少对象求求大家啊!!!
解决方案
解决方案二:
没错阿,我试验过了~~
解决方案三:
直接这样写就可以了ClientScript.RegisterStartupScript(this.GetType(),"aa","hello();");
解决方案四:
我这就是不行,郁闷!!!是不是我ie问题啊,我已经被微软更总了xp是盗版的!!!
解决方案五:
一.使用RegisterStartUpScript注册只执行一次的javascript第1步(创建并注册并使用javascript):Page.aspx.csprotectedvoidPage_Load(objectsender,EventArgse){//创建JavaScriptstringinfo="<script>alert('你好,我是农国苏!')</script>";//判断Welcome是否已被注册if(!Page.ClientScript.IsStartupScriptRegistered("Welcome")){//动态注册并执行JavaScriptPage.ClientScript.RegisterStartupScript(this.GetType(),"Welcome",info);}}二.使用RegisterClientScriptBlock注册javascript第1步(创建并注册javascript):Page.aspx.csprotectedvoidPage_Load(objectsender,EventArgse){//创建JavaScriptstringinfo="";info+="functionshowName(username)";info+="{alert('您的名字是:'+username);}";//判断myName是否已被注册if(!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(),"myName")){//动态注册JavaScriptPage.ClientScript.RegisterClientScriptBlock(this.GetType(),"myName",info,true);}}第2步(调用javascript):Page.aspx<div>姓名:<inputid="txtUserName"type="text"/><inputid="btnSubmit"type="button"value="确定"onclick="showName(txtUserName.value)"/></div>三.使用RegisterClientScriptInclude注册javascript第1步(创建javascript):Script/Info.jsfunctionshowName(username){alert('您的姓名是:'+username);}第2步(调用javascript):Page.aspx<div>姓名:<inputid="txtUserName"type="text"/><inputid="btnSubmit"type="button"value="确定"onclick="showName(txtUserName.value)"/></div>第3步(注册javascript):Page.aspx.csprotectedvoidPage_Load(objectsender,EventArgse){//判断是否已被注册if(!ClientScript.IsClientScriptIncludeRegistered(this.GetType(),"myMessage")){//动态注册JavaScriptPage.ClientScript.RegisterClientScriptInclude("myMessage","Script/Info.js");}}
参考如下
解决方案六:
ClientScript.RegisterStartupScript(this.GetType(),"aa","hello();",true);
解决方案七:
谢谢大家,问题解决,js我的位置写的不标准,我写到最后了,现在写到<head>上面就行了