在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
大家在想将TextBox控件设置为只读还不容易吗?但如果你是用到Ajax在客户端改变控件的值,再在服务器获取,如果直接将Textbox设置为Enble 设置为false,那么在客户端改变了值,在服务器端还是获取不到。我开始开发的时候,还以为只读控件就直接用Lable控件就好,后来发现一样的在客户端能改变值,但在服务器端获取不到。唯一可行的是用下面的设置: textBox.Attributes["readonly"] = "readonly"; 2.在服务器端控制Html控件的显示&隐藏
}
3在客户端访问服务器 前提实现XMLHttpRequest,可以通过asp.net中的ajax,或JQuery中封装好了的ajax,或自己封装。例如: 1.调用asp.net中ajax 在页面加入ScriptManger,如果是调用页面方法EnablePageMethods要设为true,如果访问webService设置webservice路径。
<asp:ScriptManager id="AjaxMag" runat="server" EnablePageMethods ="true" ScriptMode="Release" >
<Services><asp:ServiceReference Path ="~/WebService/comm.asmx" InlineScript="true" /></Services> </asp:ScriptManager> 调用 Page Method
//xxx.aspx.cs页面定义
[System.Web.Services.WebMethod] public static string[] GetList(string key) { return new string[]{ "AAAAAAAAA","BBBBBBBB","CCCCCCC","DDDDDD" }; }
//javascript 调用PathMethods
var key ="a"; var x PageMethods.GetList(key,function(result){ var x ; document.write ("list count:" + result.length + "<br/>"); for (x in result) { document.write (result[x] +"<br/>"); } }; 调用WebService 中Methods
//Web Service定义
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //加入此 [System.Web.Script.Services.ScriptService] public class comm : System.Web.Services.WebService { public comm () { } [WebMethod] public string[] GetList(string key) { return new string []{"aaa","bbb","cc"}; } }
//javascript 访问web service
var key="A"; comm.GetList(key,function(result){ var x ; document.write ("list count:" + result.length + "<br/>"); for (x in result) { document.write (result[x] +"<br/>"); } }); 2.自定义XMLHttpRequest
var xmlHttp;
var viewModel; //建立异步处理对象 function createXMLHttpRequest() { xmlHttp = false; //开始初始化XMLHttpRequest对象 if(window.XMLHttpRequest) { //Mozilla 浏览器 xmlHttp = new XMLHttpRequest(); if (xmlHttp.overrideMimeType) {//设置MiME类别 xmlHttp.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE浏览器 try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}} } if (!xmlHttp) { // 异常,创建对象实例失败 window.alert("不能创建XMLHttpRequest对象实例."); return false; } } function AsynGet() { createXMLHttpRequest(); viewModel=model; url="Handler.ashx?key=A"; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = callback; xmlHttp.send(null); } function callback() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { var result = xmlHttp.responseText; document.getElementById("lblMsg").innerHTML=result var arr =result.split(','); var x ; for (x in arr) { document.write(arr[x] + "<br />"); } } else { //显示错误信息 } } } 3.使用JQuery Ajax:
var sendData="key=A";
$.ajax({ type: "POST", url:"Handler.ashx", data:sendData, success:function(result){ $("#lblMsg").text(result); var arr = result.split(','); var x; for (x in arr) { document.write(arr[x] + "<br/>"); } } }); 所访问的Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("AAA,BBB,CC"); } public bool IsReusable { get { return false; } } } 动态加载Javascript和CSS 在Head中加Javascript和CSS
HtmlGenericControl script = new HtmlGenericControl("script");
script.Attributes["type"] = "text/javascript"; script.Attributes["src"] = "comm.js"; Page.Header.Controls.Add(script); HtmlGenericControl css = new HtmlGenericControl("link"); css.Attributes["type"] = "text/css"; css.Attributes["href"] = "style.css"; css.Attributes["rel"] = "stylesheet"; Page.Header.Controls.Add(css); 在页尾加Javascript string js = "<script type=\"text/javascript\" src=\"comm.js\"></script>"; ClientScript.RegisterStartupScript(GetType(),"js",js); Ajax防止乱码处理
代码
<script type ="text/javascript">
function OnClientAjax() { var value ="中文测试~!@#$%^&*()-+_`123/45"; $.ajax({ type: "POST", url: "output.aspx", data: "value=" + urlEncode(escape(value)), success: function(msg){ alert(msg); } }); } function urlEncode(s) { return s.replace(/\+/g, '%2b').replace(/\"/g,'%22').replace(/\//g,'%2f'); } </script>
protected void Page_Load(object sender, EventArgs e)
{ string value =Server.HtmlDecode (Request["Value"]); Response.Write(value); }
|
请发表评论