亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

GridView綁定List、頁面返回值的實現方法

發布時間:2021-07-26 13:50:55 來源:億速云 閱讀:148 作者:chen 欄目:開發技術

本篇內容主要講解“GridView綁定List、頁面返回值的實現方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“GridView綁定List、頁面返回值的實現方法”吧!

用慣了WPF的綁定,ASP.NET的綁定貌似不是很好用。下面看看ASP.NET綁定的用法。一般來說可以直接綁定DataTable的,不過我覺得綁定List比較符合面向對象編程。
綁定的方法是兩句代碼:

復制代碼 代碼如下:


GridView名.DataSource = List<自定義類>; 

GridView名.DataBind(); 

直接看例子吧,以下是一個綁定一個PersonModel類的例子。其中用到了頁面返回參數,使用js傳遞,js可寫在前端也可直接寫在后臺代碼里。
項目結構:
GridView綁定List、頁面返回值的實現方法

效果:
GridView綁定List、頁面返回值的實現方法

實體類

復制代碼 代碼如下:


public class PersonModel
    {
        private int personIndex;


        public int PersonIndex
        {
            get { return personIndex; }


            set { personIndex = value; }
        }


        private string personID;


        public string PersonID
        {
            get { return personID; }


            set { personID = value; }
        }


        private string personName;


        public string PersonName
        {
            get { return personName; }


            set { personName = value; }
        }


        private string personSex;


        public string PersonSex
        {
            get { return personSex; }


            set { personSex = value; }
        }


        private int personAge;


        public int PersonAge
        {
            get { return personAge; }


            set { personAge = value; }
        }


        private bool personSelected = false;


        public bool PersonSelected
        {
            get { return personSelected; }


            set { personSelected = value; }
        }
    }

針對綁定的aspx頁面寫一個管理類,用于操作數據

復制代碼 代碼如下:


public class ChildFrmManager
    {
        private List<PersonModel> personCollect = new List<PersonModel>();


        private static ChildFrmManager instance = null;


        public List<PersonModel> PersonCollect
        {
            get { return personCollect; }


            set { personCollect = value; }
        }


        public static ChildFrmManager DoGetInstance()
        {
            if (instance == null)
            {
                instance = new ChildFrmManager();
            }


            return instance;
        }


        public void DoAddPersons()
        {
            for (int i = 0; i < 20; i++)
            {
                PersonModel model = new PersonModel();


                model.PersonIndex = i + 1;


                model.PersonID = System.Guid.NewGuid().ToString();


                model.PersonName = "測試" + i;


                model.PersonAge = 27 + i;


                model.PersonSex = i % 2 == 0 ? "男" : "女";


                model.PersonSelected = false;


                this.PersonCollect.Add(model);
            }
        }
    }

綁定的頁面前端

復制代碼 代碼如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChildFrm.aspx.cs" Inherits="ASPNetGridView.Pages.ChildFrm" %>


<!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>
</head>
<<body id="MyBody" runat="server" ms_positioning="GridLayout">
    <form id="Form1" runat="server" method="post">
    <div>
    <asp:GridView ID="dgPersons" runat="server" AutoGenerateColumns="False"
     EnableViewState="false"
    CellPadding="4"  ForeColor="#333333" DataKeyNames="PersonID"
     OnSelectedIndexChanged="Selcted_Click">
     <Columns>
       <asp:CommandField ShowSelectButton="True" /> 
       <asp:BoundField DataField="PersonIndex" HeaderText="序號"/>
       <asp:TemplateField>
       <ItemTemplate>
       <input id="radiobutton1" name="pselect" type="radio" />
       </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField DataField="PersonName" HeaderText="姓名" />
       <asp:BoundField DataField="PersonAge" HeaderText="年齡" />
       <asp:BoundField DataField="PersonSex" HeaderText="性別" />
     </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

    </asp:GridView>
    </div>
    </form>
</body>
</html>

綁定頁面后臺

復制代碼 代碼如下:


 public partial class ChildFrm : System.Web.UI.Page
    {
        private ChildFrmManager dManager = null;


        protected PersonModel selectItem = null;


        protected void Page_Load(object sender, EventArgs e)
        {
            dManager = ChildFrmManager.DoGetInstance();


            if (!IsPostBack)
            {
                dManager.DoAddPersons();


                this.dgPersons.DataSource = dManager.PersonCollect;


                this.dgPersons.DataBind();
            }
        }


        protected void Selcted_Click(object sender, EventArgs e)
        {
            int selectIndex = this.dgPersons.SelectedIndex;


            foreach (PersonModel mitem in dManager.PersonCollect)
            {
                if (mitem.PersonIndex - 1 == selectIndex)
                {
                    mitem.PersonSelected = true;
                }
                else
                {
                    mitem.PersonSelected = false;
                }
            }


            selectItem = dManager.PersonCollect[selectIndex];


            string vbCrLf = " ";


            string strScript = "<script>" + vbCrLf;


            strScript += "window.parent.returnValue='" + selectItem.PersonName + "';" + vbCrLf;


            strScript += "window.parent.close();" + vbCrLf;


            strScript += "</script>" + vbCrLf;


            if (!IsClientScriptBlockRegistered("clientScript"))
            {
                RegisterClientScriptBlock("clientScript", strScript);
            }
        }
    }

承載綁定頁面的頁面

復制代碼 代碼如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrameFrm.aspx.cs" Inherits="ASPNetGridView.Pages.FrameFrm" %>


<!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>
</head>
<frameset rows="0,*">
    <frame src="about:blank">
    <frame src="ChildFrm.aspx">
  </frameset>
</html>

主頁面,獲取返回值的js在前端

復制代碼 代碼如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNetGridView._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>
</head>
<body>
    <div align="center">
        <form id="Form1" runat="server" method="post">
            <table runat="server">
              <tr>
                  <td>
                  <asp:Label ID="Label1" runat="server" Font-Bold="true">選擇結果</asp:Label>
                  </td>


                  <td>
                  <asp:TextBox ID="txtShowReturnValue" runat="server" Width="100px" />
                  </td>
                  <td>
                  <asp:Button ID="btnOpenNewFrm" runat="server" Text="選擇" Width="60px" OnClientClick="OpenNewWindow()"/>
                  </td>
              </tr>
            </table>
        </form>
    </div>
</body>
<script type ='text/javascript'>
    function OpenNewWindow() {


        var str = window.showModalDialog('Pages/FrameFrm.aspx', document.Form1.txtShowReturnValue.value, 'dialogWidth=1000px;dialogHeight=900px', 'scroll:yes');


        if (str != null)
        { document.Form1.txtShowReturnValue.value = str; }
    }
</script>
</html>

到此,相信大家對“GridView綁定List、頁面返回值的實現方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

馆陶县| 筠连县| 沾益县| 怀来县| 余庆县| 马关县| 自治县| 夏河县| 益阳市| 沾益县| 津南区| 恭城| 丹寨县| 许昌市| 望奎县| 江华| 紫金县| 林甸县| 稻城县| 菏泽市| 教育| 遂川县| 洛川县| 潞城市| 奇台县| 绥中县| 阆中市| 元阳县| 区。| 乌鲁木齐市| 襄汾县| 天长市| 武宁县| 淮阳县| 重庆市| 宜宾市| 枣庄市| 怀仁县| 杂多县| 莫力| 兴宁市|