asp.net 后台怎么使用javascript

chouer523 2008-11-21 04:19:47
<script language="javascript" type="text/javascript">
function theInvoke()
{
var thetxtstr = document.getElementById('TextBox1').value
window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');
}
</script>
我上面这段javascript 我要写成如下的形式
Response.Write("<script language="javascript"; type="text/javascript";>;");
Response.Write("function theInvoke()");
Response.Write("{ var thetxtstr = document.getElementById('TextBox1').value;");
Response.Write("window.open('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');}");
Response.Write("theInvoke();</script>");

这样出现错误,谁能帮我改装一下,50分奉上 谢谢!~~~~~
...全文
296 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq196260188 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yfqvip 的回复:]
C# code
System.Text.StringBuilder strBuf = new System.Text.StringBuilder();
strBuf.Append("<script language=\"javascript\" type=\"text/javascript\">");
strBuf.Append("function theInvoke()");
strBuf.Append("{");
strBuf.Append(" var thetxtstr = document.getElementById('TextBox1').value ");
strBuf.Append("window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;'); ");
st…
[/Quote]


正解
a13971240 2008-11-24
  • 打赏
  • 举报
回复
if (!IsPostBack) {
InitForm();
}
private void InitForm() {
Literal ltrScript = new Literal();
StringBuilder sbScript = new StringBuilder();
sbScript.Append(@"<script language="javascript" type="text/javascript">
function theInvoke()
{
var thetxtstr = document.getElementById('TextBox1').value
window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');
}
</script> ")
ltrScript.Text = Convert.ToString(sbScript);
frmLogin.Controls.Add(ltrScript); //frmLogin是自己from表单名称
}
jiang_jiajia10 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ojekleen 的回复:]
C# code
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "notFound", "window.open('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');");
//在后台可以直接
string thetxtstr =this.TextBox1.Text.Trim();
[/Quote]
可以了
似乎LZ写了些冗余代码
//var thetxtstr = document.getElementById('TextBox1').value ;你这个变理好像没有用到吧
tm62490309 2008-11-24
  • 打赏
  • 举报
回复
为什么不考虑 后台调用JS方法 然后 在前台写JS呢?
y63964632 2008-11-24
  • 打赏
  • 举报
回复
建议用ClientScriptManager注册~
zhu371816210 2008-11-24
  • 打赏
  • 举报
回复
C# code

System.Text.StringBuilder strBuf = new System.Text.StringBuilder();
strBuf.Append("<script language=\"javascript\" type=\"text/javascript\">");
strBuf.Append("function theInvoke()");
strBuf.Append("{");
strBuf.Append(" var thetxtstr = document.getElementById('TextBox1').value ");
strBuf.Append("window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;'); ");
strBuf.Append("}");
strBuf.Append("</script> ");
strBuf.Append("");
Response.Write(strBuf.ToString())
这个行
freexiaoyu 2008-11-22
  • 打赏
  • 举报
回复
this.Button1.Attributes.Add("onclick","theInvoke()");
zhangli0911 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yfqvip 的回复:]
C# code
System.Text.StringBuilder strBuf = new System.Text.StringBuilder();
strBuf.Append("<script language=\"javascript\" type=\"text/javascript\">");
strBuf.Append("function theInvoke()");
strBuf.Append("{");
strBuf.Append(" var thetxtstr = document.getElementById('TextBox1').value ");
strBuf.Append("window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;'); ");
st…
[/Quote]
正解
tinalucky 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 chouer523 的回复:]
怎么没有调用的啊
Response.Write("theInvoke();");
调用不起来 晕倒 !~~~~~~~~~~~~~~
[/Quote]
要用<script></script>标签
后浪 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 chouer523 的回复:]
怎么没有调用的啊
Response.Write("theInvoke();");
调用不起来 晕倒 !~~~~~~~~~~~~~~
[/Quote]
外面掉了<script>...
Response.Write('<script tyep=text/javascript>theInvoke()</script>');
hubofly 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 chouer523 的帖子:]
<script language="javascript" type="text/javascript">
function theInvoke()
{
var thetxtstr = document.getElementById('TextBox1').value
window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');
}
</script>
我上面这段javascript 我要写成如下的形式
Response.Write(" <script language="javascript"; type="text/javascript";>;");
Response.Write("function theInvoke()");
Res…
[/Quote]

把所有的Response.write 的东西给一个 你声明的 string jsStr 类型的 一个变量 ,然后你在 你aspx页面的最下面写上 <%=jsStr%>
这样应该就不会报错了,报错的原因应该是你的 document.getElementById('TextBox1').value这个没有获取到值

Robin 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gongjiajie 的回复:]
用一个Literal控件 在后台Page_Load里这样写
this.Literal1.Text=@"";双引号里就写你JS的内容

切记住 写进双引号里的JS 一定要把双引号变成2个双引号 举个例子
this.Literal1.Text=@" <script language=""javascript"" type=""text/javascript""> ";

如果不明白+QQ 438224991 我发你源码
[/Quote]

有UpdatePanel 这样就不行了。

楼主可以
string strScript= @"<script language='avascript' type='text/javascript'>
function theInvoke()
{
var thetxtstr = document.getElementById('TextBox1').value
window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');
}
</script> ";

Response.Write(strScript);
满衣兄 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 chouer523 的回复:]
怎么没有调用的啊
Response.Write("theInvoke();");
调用不起来 晕倒 !~~~~~~~~~~~~~~
[/Quote]
用7楼的那个
wuyq11 2008-11-21
  • 打赏
  • 举报
回复
Response.Write("<script>theInvoke();</script>");
chouer523 2008-11-21
  • 打赏
  • 举报
回复
怎么没有调用的啊
Response.Write("theInvoke();");
调用不起来 晕倒 !~~~~~~~~~~~~~~
phper2008 2008-11-21
  • 打赏
  • 举报
回复
或者直接
string scritp=@"
<script type=""text/javascript"">
function(a,b)
{{
alert(""yuna123"");
}};
</script>
";
Response.Write(script);
phper2008 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 phper2008 的回复:]
Response.Write(" <script language=\"javascript\" type=\"text/javascript\";>");
Response.Write("function theInvoke()");
Response.Write("{ var thetxtstr = document.getElementById('TextBox1').value;");
Response.Write("window.open('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');}");
Response.Write("theInvoke(); </script>");
[/Quote]
Response.Write(" <script language=\"javascript\" type=\"text/javascript\">");

搞不懂你为什么加这么多“;”language="javascript"; type="text/javascript";
有这样写的么,就算在html里也是错啊,注意在""里再写"就得写成\"转义下!
或者如3楼的直接<script></script>浏览器自己知道这是js语言
evjen 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cefriend 的回复:]
这种写法是错误的

用stringbuilding类

C# code
StringBuilding str = new StringBuilding()
str.append("<script language="javascript" type="text/javascript"> ");
str.append("function theInvoke() ");
str.append(" {var thetxtstr = document.getElementById('TextBox1').value");
str.append(" window.open ('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');} ");
str.append("</scr…
[/Quote]

顶一下 不过也可以不用stringbuilding 直接str+
ojekleen 2008-11-21
  • 打赏
  • 举报
回复

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "notFound", "window.open('cphelp.aspx','MybTarget','dialogWidth=150px;dialogHeight=80px;');");
//var thetxtstr = document.getElementById('TextBox1').value ;你这个变理好像没有用到吧
//在后台可以直接
string thetxtstr =this.TextBox1.Text.Trim();
流金年代 2008-11-21
  • 打赏
  • 举报
回复
用一个Literal控件 在后台Page_Load里这样写
this.Literal1.Text=@"";双引号里就写你JS的内容

切记住 写进双引号里的JS 一定要把双引号变成2个双引号 举个例子
this.Literal1.Text=@"<script language=""javascript"" type=""text/javascript""> ";

如果不明白+QQ 438224991 我发你源码
加载更多回复(5)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧