学习笔记
1、 使用ScriptManager注册脚本
我在使用Asp.netAjax时候发现注册脚本到页面(Page)时候,居然不生效
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Error", string.Format("alert('{0}');", msg), true);
经过多方查找发现既然不能注册到Page,那就注册到UpdatePanel
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "Error", string.Format("alert('{0}');", msg), true);
2、生成随机字符串(当需要随机码的时候比较有用)
static string GenerateDynamicKey(int? length)
{
char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
int size = 12;
if (length > 0)
size = (int)length;
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
byte[] data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
3、向服务器递交数据,接收返回的数据
public static string GetWebResponse(string url, string postData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string returnString = sr.ReadToEnd();
sr.Close();
response.Close();
return returnString;
4、WebService 放到服务器之后出现“测试窗体只能用于来自本地计算机的请求”
我们需要在<system.web>节点内加入以下代码
Code
好了,今天就先贴这些出来,嘿嘿...
posted on 2008-09-27 15:42 jerreychen 阅读(254) 评论(0) 编辑 收藏 举报