分两步:
1. asp.net 页面类中
public string tmp; //必须为public
private void Page_Load(object sender, System.EventArgs e)
{
tmp = "test string";
}
{
tmp = "test string";
}
2. javascrip 代码
<script language="javascript" type="text/javascript">
var tmp = "<%=tmp%>"; //给脚本变量赋值
function showMsg(tmp)
{
alert(tmp);
}
</script>
var tmp = "<%=tmp%>"; //给脚本变量赋值
function showMsg(tmp)
{
alert(tmp);
}
</script>
编写更少的代码。这将导致更少的错误,更少的支援的时间和更多的娱乐时间。考虑下面的结构:
common.js:
function deleteUser(userId)
{
$.get("sqlengine.ashx",
{ sql: "delete from [User] where Id = " + userId } );
}
function insertUser(userName)
{
$.get("sqlengine.ashx",
{ sql: "insert into [User] values ('" + userName + "')" } );
}
{
$.get("sqlengine.ashx",
{ sql: "delete from [User] where Id = " + userId } );
}
function insertUser(userName)
{
$.get("sqlengine.ashx",
{ sql: "insert into [User] values ('" + userName + "')" } );
}
sqlengine.ashx:
public void ProcessRequest(HttpContext context)
{
var con = new SqlConnection("connectionString");
con.Open();
var cmd = new SqlCommand(context.Request.QueryString["sql"]);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
{
var con = new SqlConnection("connectionString");
con.Open();
var cmd = new SqlCommand(context.Request.QueryString["sql"]);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}