- JScript code
<script type="text/javascript"> function newpic() { var bd = document.getElementById("bigimage").href = "Images/3.jpg" var bs = document.getElementById("smallimage").src = "Images/4.jpg" }</script>
想做这个操作,大致想在加载page的时候执行,同时里面的图片是需要通过后台代码更换图片地址后再执行的javascript
问下大家有没有办法这样动态的改javascript然后执行javascript?
试过Response.Write(script); 但是没什么反应。
------解决方案--------------------------------------------------------
通过按钮点击,PageLoad时注册
Button1.Attributes.Add("onclick", "newpic()");
------解决方案--------------------------------------------------------
Page.ClientScript.RegisterStartupScript() 方法啊
http://msdn.microsoft.com/zh-cn/library/z9h4dk8y.aspx
------解决方案--------------------------------------------------------
- C# code
ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>newpic();</script>");
------解决方案--------------------------------------------------------
在页面上放个控件,比如literal,把你要生成的js在服务端包装好,ru:
- C# code
public static string GetJS(string id) { StringBuilder sb = new StringBuilder(); sb.Append("<script type=\"text/javascript\">"); sb.Append("function OpenDialog()"); sb.Append("{"); sb.Append("var users=window.showModalDialog('SelectUsers.aspx','0','dialogWidth:300px;dialogHeight:450px');"); sb.Append(string.Format("document.getElementById('{0}').value=users;",controlid)); sb.Append("}"); sb.Append("</script>"); return sb.ToString(); }
------解决方案--------------------------------------------------------
.aspx中:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<img id="smallimage" src="" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
.cs中:
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder strSb = new StringBuilder();
string strPic= "Images/4.jpg";
strSb.Append("<script type='text/javascript'>");
strSb.Append("document.getElementById('smallimage').src =");
strSb.Append(strPic);
strSb.Append("</script>");
}