ClientScriptManager csm = Page.ClientScript;
//Script标记靠近<form>标签
//csm.RegisterClientScriptBlock(this.GetType(), "alert", js,false);
//Script标记靠近</form>标签
csm.RegisterStartupScript(this.GetType(), "alert", js, false);
动态方式
RegisterClientScriptBlock 生成的脚本位于<form>标签之后
RegisterStartupScript 生成的脚本位于</form>标签之前 需要页面元素
Response.Write() 生成的脚本位于Html代码的顶部即<!DOCTYPE>之前
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "scriptKey"))
{
string scriptText = "<script language='javascript'>
document.getElementById('txtUserName').focus();</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", scriptText);
ScriptManager.RegisterStartupScript(this, typeof(Page), "alertExist", "alert('您选择的商品(编号:" + Vcode + ")已在购物车存在!')", true);
}