>  기사  >  웹 프론트엔드  >  새로 고치지 않는 linkage_javascript 기술 달성 사례 요약

새로 고치지 않는 linkage_javascript 기술 달성 사례 요약

WBOY
WBOY원래의
2016-05-16 15:58:141120검색

iframe이 새로고침 연결을 달성하지 못했습니다

iframe의 새로 고침 없음은 실제로 부분 새로 고침입니다. 상태 표시줄의 스크롤 막대는 계속 스크롤되지만 페이지는 깜박이지 않습니다. 이는 오래된 기술이므로 처리되는 데이터가 클 경우 속도가 느려집니다. 이 예에서는 index.aspx와 frame.asapx라는 두 페이지가 필요합니다. Index.aspx는 인터페이스를 표시하는 데 사용됩니다. 결과를 표시하기 위해 Frame.aspx 페이지를 가리키는 iframe 태그가 있습니다. index.aspx 프런트엔드 코드

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>

  <script type="text/javascript">
    function Query() {
      var ddlpro = document.getElementById('ddlPro');
      var pro = ddlpro.options[ddlpro.selectedIndex].innerText;
      if (pro != "") {
        document.getElementById("iframe1").src = "frame.aspx&#63;Pro=" + pro;
      }
    }

  </script>

</head>
<body>
  <form id="form2" runat="server">
  <div>
    <table border="1" cellpadding="3" cellspacing="0" width="600px">
      <tr>
        <td colspan="2" align="center">
          Iframe实现局部刷新
        </td>
      </tr>
      <tr>
        <td>
          省份名称:
        </td>
        <td>
          <select id="ddlPro" style="width: 201px">
            <option value="湖北">湖北</option>
            <option value="河北">河北</option>
            <option value="广东">广东</option>
            <option value="河南">河南</option>
          </select>
          <input id="Button3" type="button" value="查询" onclick="Query()" />
        </td>
      </tr>
      <tr>
        <td>
          显示城市列表
        </td>
        <td>
          <iframe src="frame.aspx" style="text-align: center" id="iframe1" width="100%"
            height="100%" frameborder="0" scrolling="no" />
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>

frame.aspx의 프런트엔드 코드:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frame.aspx.cs" Inherits="myframe" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form2" runat="server">
  <div>
    <asp:DropDownList ID="ddlCity" runat="server" Width="179px">
    </asp:DropDownList>
  </div>
  </form>
</body>
</html>

frame.aspx 배경 코드:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class myframe : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string pro = Request.QueryString["pro"];
    switch (pro)
    {
      case "湖北":
        this.ddlCity.Items.Clear();
        this.ddlCity.Items.Add("武汉");
        this.ddlCity.Items.Add("黄冈");
        this.ddlCity.Items.Add("黄石");
        this.ddlCity.Items.Add("襄樊");
        break;
      case "河北":
        this.ddlCity.Items.Clear();
        this.ddlCity.Items.Add("石家庄");
        this.ddlCity.Items.Add("唐山");
        this.ddlCity.Items.Add("承德");
        this.ddlCity.Items.Add("邯郸");
        break;
      case "广东":
        this.ddlCity.Items.Clear();
        this.ddlCity.Items.Add("广州");
        this.ddlCity.Items.Add("佛山");
        this.ddlCity.Items.Add("深圳");
        this.ddlCity.Items.Add("珠海");
        break;
      case "河南":
        this.ddlCity.Items.Clear();
        this.ddlCity.Items.Add("郑州");
        this.ddlCity.Items.Add("新乡");
        this.ddlCity.Items.Add("安阳");
        this.ddlCity.Items.Add("信阳");
        break;

    }
  }
}

JavaScript 새로고침 연결 없음

첫 페이지 코드:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="jacascript_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>

  <script type="text/javascript">
    function FillData(strcity) {

      document.getElementById("ddlCity").options.length = 0;
      var indexofcity;
      var city;
      while (strcity.length > 0) {
        indexofcity = strcity.indexOf(",");
        if (indexofcity > 0) {
          city = strcity.substring(0, indexofcity);

          strcity = strcity.substring(indexofcity + 1);
          document.getElementById("ddlCity").add(new Option(city, city));
        }
        else {
          document.getElementById("ddlCity").add(new Option(strcity, strcity));
          break;
        }

      }
    }
  </script>

</head>
<body>
  <form id="form2" runat="server">
  <div>
    <table width="700px" border="1" cellpadding="5" cellspacing="0">
      <tr>
        <td colspan="2" align="center">
          脚本方法实现刷新
        </td>
      </tr>
      <tr>
        <td>
          选择省份:
        </td>
        <td>
          <select id="ddlPro" style="width: 201px">
            <option value="湖北">湖北</option>
            <option value="河北">河北</option>
            <option value="广东">广东</option>
            <option value="河南">河南</option>
          </select>
          <input id="btnQuery" type="button" value=" 查询" onclick="City()" />
        </td>
      </tr>
      <tr>
        <td>
          城市:
        </td>
        <td>
          <asp:DropDownList ID="ddlCity" runat="server" Width="201px">
          </asp:DropDownList>
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>

백엔드 코드:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class jacascript_Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    StringBuilder myscript = new StringBuilder();
    myscript.Append("function City() {\n");
    myscript.Append("var ddlpro=document.getElementById('ddlPro');\n");
    myscript.Append("var pro=ddlpro.options[ddlpro.selectedIndex].innerText;\n");
    //myscript.Append("var pro=document.getElementById('txtPro').value;\n");
    myscript.Append("switch(pro) { \n");
    myscript.Append("case '湖北':\n");
    myscript.Append("FillData('" + GetCityStr("湖北") + "');\n");
    myscript.Append("break;\n");
    myscript.Append("case '河北':\n");
    myscript.Append("FillData('" + GetCityStr("河北") + "');\n");
    myscript.Append("break;\n");
    myscript.Append("case '广东':\n");
    myscript.Append("FillData('" + GetCityStr("广东") + "');\n");
    myscript.Append("break;\n");
    myscript.Append("case '河南':\n");
    myscript.Append("FillData('" + GetCityStr("河南") + "');\n");
    myscript.Append("break;}\n");
    myscript.Append("}\n");

    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "city", myscript.ToString(), true);

  }

  private string GetCityStr(string pro)
  {
    string city = "";
    switch (pro)
    {
      case "湖北":
        city = "武汉,黄冈,黄石,襄樊";
        break;
      case "河北":
        city = "石家庄,唐山,承德,邯郸";
        break;
      case "广东":
        city = "广州,佛山,深圳,珠海";
        break;
      case "河南":
        city = "郑州,新乡,安阳,信阳";
        break;
    }
    return city;
  }
}


콜백 새로고침 연결 없음

프런트엔드 코드:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="callback_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>

  <script type="text/javascript">
 function FillData()
 {
 var ddlpro=document.getElementById('ddlPro');
 var pro=ddlpro.options[ddlpro.selectedIndex].value;
 <% =ClientScript.GetCallbackEventReference(this,"pro","FillDll",null) %>
  }

 function FillDll(strcity)
 {
    document.getElementById("ddlCity").options.length=0;
 var indexofcity;
 var city;
 while(strcity.length>0)
 {
      indexofcity=strcity.indexOf(",");
 if(indexofcity>0)
 {
        city=strcity.substring(0,indexofcity);

        strcity=strcity.substring(indexofcity+1);
        document.getElementById("ddlCity").add(new Option(city,city));
      }
 else
 {
        document.getElementById("ddlCity").add(new Option(strcity,strcity));
 break;
      }

    }
  }
  </script>

</head>
<body>
  <form id="form2" runat="server">
  <div>
    <table width="700px" border="1" cellpadding="5" cellspacing="0">
      <tr>
        <td colspan="2" align="center">
          callback方法实现刷新
        </td>
      </tr>
      <tr>
        <td>
          选择省份:
        </td>
        <td>
          <select id="ddlPro" style="width: 200px">
            <option value="湖北">湖北</option>
            <option value="河北">河北</option>
            <option value="广东">广东</option>
            <option value="河南">河南</option>
          </select>
          <input id="btnQuery" type="button" value=" 查询" onclick="FillData()" />
        </td>
      </tr>
      <tr>
        <td>
          城市:
        </td>
        <td>
          <asp:DropDownList ID="ddlCity" runat="server" Width="201px">
          </asp:DropDownList>
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>

백엔드 코드:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class callback_Default : System.Web.UI.Page,ICallbackEventHandler
{
 private string _data;
 protected void Page_Load(object sender, EventArgs e)
 {

  }

 ICallbackEventHandler 成员
}

Ajax 새로고침 연결 없음

이 예에서는 oec203index.aspx와 datapage.aspx라는 두 페이지도 사용합니다. datapage.aspx는 주로 표시할 데이터를 다시 보내는 데 사용됩니다.

.aspx 페이지 프런트 엔드 코드:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="ajax_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>

  <script type="text/javascript">
    var xmlhttp;
    function getData() {
      var ddlpro = document.getElementById("ddlPro");
      var pro = ddlpro.options[ddlpro.selectedIndex].innerText;
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.onreadystatechange = statechange;
      xmlhttp.Open("GET", "datapage.aspx&#63;pro=" + pro, true);
      xmlhttp.Send();
    }

    function statechange() {
      if (xmlhttp.readystate == 4) {
        if (xmlhttp.status == 200) {
          FillData(xmlhttp.responseText);
        }
      }
    }
    function FillData(strcity) {
      document.getElementById("ddlCity").options.length = 0;
      var indexofcity;
      var city;
      while (strcity.length > 0) {
        indexofcity = strcity.indexOf(",");
        if (indexofcity > 0) {
          city = strcity.substring(0, indexofcity);
          strcity = strcity.substring(indexofcity + 1);
          document.getElementById("ddlCity").add(new Option(city, city));
        }
        else {
          document.getElementById("ddlCity").add(new Option(strcity, strcity));
          break;
        }
      }
    }
  </script>

</head>
<body>
  <form id="form2" runat="server">
  <div>
    <table width="700px" border="1" cellpadding="5" cellspacing="0">
      <tr>
        <td colspan="2" align="center">
          ajax方法实现刷新
        </td>
      </tr>
      <tr>
        <td>
          选择省份:
        </td>
        <td>
          <select id="ddlPro" style="width: 201px">
            <option value="湖北">湖北</option>
            <option value="河北">河北</option>
            <option value="广东">广东</option>
            <option value="河南">河南</option>
          </select>
          <input id="btnQuery" type="button" value=" 查询" onclick="getData()" />
        </td>
      </tr>
      <tr>
        <td>
          城市:
        </td>
        <td>
          <asp:DropDownList ID="ddlCity" runat="server" Width="201px">
          </asp:DropDownList>
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>

datapage.aspx 배경 코드:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ajax_datapage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string pro = Request.QueryString["pro"];
    Response.Clear();
    switch (pro)
    {
      case "湖北":
        Response.Write("武汉,黄冈,黄石,襄樊");
        break;
      case "河北":
        Response.Write("石家庄,唐山,承德,邯郸");
        break;
      case "广东":
        Response.Write("广州,佛山,深圳,珠海");
        break;
      case "河南":
        Response.Write("郑州,新乡,安阳,信阳");
        break;
    }
  }
}
위 내용은 이 글의 전체 내용입니다. 모두 마음에 드셨으면 좋겠습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.