JS操作Iframe獲取到Iframe中的內容+ListView+DataPager分頁

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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 runat="server">
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var txt = document.getElementById("txtShowIframe");
            txt.onmousedown = function () {
                document.getElementById("div_showIfrmae").style.display = "block";
            }
            txt.onmouseover = function () {
                document.getElementById("div_showIfrmae").style.display = "none";
            }
            //按鈕點擊事件
            document.getElementById("btnShowValue").onclick = function () {
                alert(document.getElementById("txtShowIframe").value);
            }
            f();
        }
        //此方法用來獲取iframe中src嵌入頁面的內容
        function f() {
            var doc;
            //判斷瀏覽器
            if (document.all) {//IE   
                doc = document.frames["MyIFrame"].document;
            } else {//Firefox      
                doc = document.getElementById("MyIFrame").contentDocument;
            }
            document.getElementById("txtShowIframe").value = "<%=stuAddress %>";//將iframe中拿到的值放到文本框中(值在後臺接受)
        }   
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
         <input type="text" id="txtShowIframe" style="text-align:right;" />
         <input type="button" id="btnShowValue" value="顯示從iframe中點擊到的值" />
        <div id="div_showIfrmae" style="display:none; width:880px; height:445px;">
            <iframe src="Default2.aspx" id = "MyIFrame"  style=" color: #990099;  width: 470px; height: 180px; border:1px solid green; margin:10px 123px;" name = "MyIFrame" frameborder="0"></iframe>
        </div>
    </div>

    </form>
</body>
</html>

Default.aspx後臺代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public string stuAddress = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["stuAddress"] != null)
            {
                stuAddress = Request.QueryString["stuAddress"].ToString();
            }
        }

    }
 
}


 

//Default2.aspx前臺代碼

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

<!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 runat="server">
    <title></title>
    <style type="text/css">
        .div_show{ border:1px solid red;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div_show">
        <table>
        <tr>
            <td>學生郵箱</td>
            <td>學生地址</td>
        </tr>
        <asp:ListView ID="lsvShowStudentInfo" runat="server">
            <LayoutTemplate>
            <tr id="itemPlaceholder" runat="server"></tr>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#Eval("StuEmail") %></td>
                    <td οnclick="parent.location.href='Default.aspx?stuAddress=<%#Eval("StuAddress") %>'" id="stuAddress" style=" cursor:pointer;"><%#Eval("StuAddress") %></td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
        </table>
        <asp:DataPager runat="server" ID="dpg" PageSize="6" PagedControlID="lsvShowStudentInfo">
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowNextPageButton="false" ShowLastPageButton="false" />
                <asp:NumericPagerField ButtonCount="3" />
                <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowLastPageButton="true" ShowPreviousPageButton="false" />
            </Fields>
        </asp:DataPager>
    </div>
    </form>
</body>
</html>

//Default2.aspx後臺代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString; //獲取webconfig裏面的連接串
    
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        string sql = "select * from student";
        SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        lsvShowStudentInfo.DataSource = ds;
        
        conn.Close();
    }
    protected void Page_PreRender(object sender, EventArgs e) {
        lsvShowStudentInfo.DataBind();
    }
}

 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章