使用c#更改javascript [英] altering javascript using c#

查看:119
本文介绍了使用c#更改javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

任何人都可以告诉我使用c#更改javascript代码的方法。我已经给出了一个javascript代码。我必须在标记为?的地方放置一个值。 。我正在html页面中编写此代码。

hi everybody,
Can anyone tell me a way to alter javascript code using c#.below i have given a javascript code.In that i have to put a value in the place marked as "?".i am writing this code in html page.

<script type="text/javascript">
<!--
    var image1 = new Image()
    image1.src = "test1.aspx?ImageID=?"
    var image2 = new Image()
    image2.src = "test2.aspx?ImageID=?"
    var image3 = new Image()
    image3.src = "test3.aspx?ImageID=?"
//-->
</script>



是有什么方法可以代替我的价值?使用c#.it会很棒,如果你可以帮助我的话。请先提醒你。祝福你


is there any way by which i can put a value in the place of ? using c#.it would be great if you could help me.thank you in advance.god bless you

推荐答案

一种管理方式将JS插入到页面中后端C#代码正在使用

RegisterClientScriptBlock ,参见示例:
One managed way to insert JS into the page while in the back-end C# code is using
RegisterClientScriptBlock, see example:
ClientScriptManager cs = Page.ClientScript;
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
StringBuilder sb = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
sb.Append("Form1.Message.value='Text from client script.'} </");
sb.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, sb.ToString(), false);





最好,

H



best,
H


@ faizal,



如果我们在.aspx文件中添加一个JavaScript函数,它的代码就无法更改。正如Herbisaurus所建议的那样,您将javascript函数编写为字符串。您可以根据需要在运行时更改和编写它。一旦你完成了它的编写,你必须确保它被读取并作为JS函数执行。



为此,请使用RegisterClientScriptBlock并将字符串作为参数传递。



参考 http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx 本文有关RegisterClientScriptBlock的更多详细信息
@faizal,

If we add a JavaScript function in .aspx file, it's code can not be changed. As suggested by Herbisaurus, you write your javascript function as a string. You have full liberty to change and write it at runtime as you want. Once you are done writing it, you have to make sure, it's read and executed as a JS function.

To do that, use RegisterClientScriptBlock and pass your string as a parameter.

Refer to http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx this article for more details on RegisterClientScriptBlock


这篇关于使用c#更改javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆