国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Asp.net回調(diào)技術(shù)Callback學(xué)習(xí)

2019-11-17 01:40:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

asp.net回調(diào)技術(shù)Callback學(xué)習(xí)

.aspx:

Html代碼收藏代碼
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>無(wú)標(biāo)題頁(yè)</title>
  6. <scrjavascript">
  7. //向服務(wù)器傳遞參數(shù)
  8. functionDoSearch(){
  9. varfirstName=document.getElementById("TextBox1").value;
  10. CallServer(firstName,"");
  11. }
  12. //得到服務(wù)器的數(shù)據(jù)
  13. functionReceiveServerData(txtUserInfo){
  14. Results.innerHTML=txtUserInfo;
  15. }
  16. //設(shè)置每1秒執(zhí)行一次
  17. setInterval("DoSearch()",1000);
  18. </script>
  19. </head>
  20. <body>
  21. <formid="form1"runat="server">
  22. <div>
  23. 姓名:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
  24. <br/>
  25. <spanid="Results"style="width:500px;"></span>
  26. </div>
  27. </form>
  28. </body>
  29. </html>

.aspx.cs

C#代碼收藏代碼
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Configuration;
  4. usingSystem.Data;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.HtmlControls;
  9. usingSystem.Web.UI.WebControls;
  10. usingSystem.Web.UI.WebControls.WebParts;
  11. usingSystem.Data.SqlClient;
  12. publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
  13. {
  14. }
  15. }
  16. comm.Dispose();
  17. reader.Dispose();
  18. conn.Dispose();
  19. }
  20. }
  21. //得到回調(diào)的結(jié)果,返回給客戶端
  22. publicstringGetCallbackResult()
  23. {
  24. returntxtUserInfo;
  25. }
  26. }

簡(jiǎn)化版(偷懶一下):

Html代碼收藏代碼
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>無(wú)標(biāo)題頁(yè)</title>
  6. <scripttype="text/Javascript">
  7. functionOnCallBack(txtUserInfo,context){
  8. Results.innerHTML=txtUserInfo;
  9. }
  10. </script>
  11. </head>
  12. <body>
  13. <formid="form1"runat="server">
  14. <div>
  15. 姓名:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
  16. <inputid="Button2"type="button"value="button"
  17. onclick="<%=Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('TextBox1').value","OnCallBack",null)%>"/>
  18. <br/>
  19. <spanid="Results"style="pink;width:500;"></span>
  20. </div>
  21. </form>
  22. </body>
  23. </html>

.aspx.cs

C#代碼收藏代碼
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Configuration;
  4. usingSystem.Data;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.HtmlControls;
  9. usingSystem.Web.UI.WebControls;
  10. usingSystem.Web.UI.WebControls.WebParts;
  11. usingSystem.Data.SqlClient;
  12. usingSystem.Text;
  13. publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
  14. {
  15. protectedStringBuildertxtUserInfo;
  16. protectedvoidPage_Load(objectsender,EventArgse)
  17. {
  18. }
  19. publicstringGetCallbackResult()
  20. {
  21. returntxtUserInfo.ToString();
  22. }
  23. publicvoidRaiseCallbackEvent(stringtxtFirstName)
  24. {
  25. txtUserInfo=newStringBuilder();
  26. StringconnString=ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
  27. SqlConnectionconn=newSqlConnection(connString);
  28. conn.Open();
  29. SqlCommandcomm=newSqlCommand("select*fromzzxwhere[name]=@name",conn);
  30. comm.Parameters.Add("@name",SqlDbType.VarChar).Value=txtFirstName;
  31. SqlDataReaderreader=comm.ExecuteReader(CommandBehavior.CloseConnection);
  32. if(reader.Read())
  33. {
  34. txtUserInfo.Append("員工編號(hào):"+reader["id"].ToString()+"<br>");
  35. txtUserInfo.Append("員工姓名:"+reader["name"].ToString()+"<br>");
  36. txtUserInfo.Append("地址:"+reader["address"].ToString()+"<br>");
  37. txtUserInfo.Append("查詢時(shí)間:"+DateTime.Now.ToString());
  38. }
  39. else
  40. {
  41. if(txtFirstName==string.Empty)
  42. {
  43. txtUserInfo.Append("請(qǐng)輸入姓名");
  44. }
  45. else
  46. {
  47. txtUserInfo.Append("查無(wú)此人");
  48. }
  49. reader.Dispose();
  50. comm.Dispose();
  51. conn.Dispose();
  52. }
  53. }
  54. }

示例3:

Html代碼收藏代碼
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default3.aspx.cs"Inherits="Default3"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>無(wú)標(biāo)題頁(yè)</title>
  6. <scripttype="text/javascript">
  7. //客戶端執(zhí)行的方法
  8. //下面的方法是接收并處理服務(wù)器方法返回的結(jié)果
  9. functionSuccess(args,context){
  10. message.innerHTML=args;
  11. }
  12. //下面的方式是當(dāng)接收服務(wù)器方法處理的結(jié)果發(fā)生異常時(shí)調(diào)用的方法
  13. functionError(){
  14. message.innerHTML="發(fā)生了異常!";
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <formid="form1"runat="server">
  20. <div>
  21. 用戶名:<inputtype="text"id="txtUserName"onblur="CallServerMethod(txtUserName.value,nu
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郴州市| 毕节市| 东源县| 清水县| 黔西| 临海市| 呈贡县| 惠东县| 翁牛特旗| 潼关县| 海盐县| 莆田市| 海南省| 蓬安县| 乐至县| 汶川县| 邳州市| 景宁| 同心县| 丰都县| 营口市| 潼关县| 新营市| 江山市| 云安县| 禄丰县| 海晏县| 花莲县| 屏边| 岳池县| 错那县| 鹤庆县| 宜都市| 平阴县| 荣昌县| 皋兰县| 贵南县| 新丰县| 新丰县| 逊克县| 绥芬河市|