it's a easy essay about the difference in  Response.Write、 ClientScript.RegisterStartupScript、 ClientScript.RegisterClientScriptBlock.

 code1:     
        string message="I have a test now";
        StringBuilder sb = new StringBuilder();
        sb.Append(" <script type=\"text/javascript\"> \n ");
        sb.Append("    alert(\"" + message + "\") \n");
        sb.Append(" </script> \n");
        Response.Write(sb.ToString());

 code2:   
        string function = string.Format("alert('{0}')", message);
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", function, true);

 code3:
        string function = string.Format("alert('{0}')", message);
        ClientScript.RegisterClientScriptBlock(this.GetType(), "onclick", function, true);


 as you konw ,these three code3 have a the same funtion .

 if you have a test,you will find the differece in them. Actually, the code1 block is equal with code3 block .

 but the code3 block  can help you to improve User Experience,if you don't use the ajax in your solution.

 it's also a dev log for someone who need it.


 BTW,
 the differece in ClientScript.RegisterStartupScript and ClientScript.RegisterClientScriptBlock:

 ClientScript.RegisterClientScriptBlock  adds javascript function block at the right endings of "<form runat="server">" tag of web form.

 and  ClientScript.RegisterStartupScript just appends it above "</form>".
 
 what about Response.Write? you can figure it out.

 or what about  just  design a demo about it.

Posted on 2008-07-25 17:44 {:) 阅读(...) 评论(...)  编辑 收藏