如果不用Ajax,在CS文件中运行某段js代码是可以的:
Page.ClientScriptRegisterStarupScript(Page.GetType(),"","<script>window.open('default.aspx')</script>");
如果页面中使用了ajax,则上述代码执行无效。应对这种情况,通常采用:
ScriptManager.RegisterStartupScript(this.Button1,this.GetType(),"alertScript","window.open('default.aspx');",rue);
其中,第一个参数为要注册脚本的控件ID.
第二个参数为,注册脚本控件的类型,是控件还是this.GetType()都可以。typeof(string)也没问题。
第三个参数为,脚本函数的名字,随便起。
第四个参数为,脚本内容。
第五个参数为,是否添加脚本标签,如果第四个参数里面包含了<script></script>标签,此处为false,否则为ture.
aspx代码如下:
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox runate="server" ID="TextBox1">
</asp:TextBox>
<asp:Button runat="server" Text="Button" ID="Button1" OnClick="Button1_Click"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ConrolID="Button1"/>
</Triggers>
</asp:UpdatePanel>
<div>
注意:在Button1_Click的事件中注册脚本。