.net中的資料繫結,ObjectDataSource,Repeater,ItemTemplate,viewstate初步學習

來源:互聯網
上載者:User

此文僅供個人筆記!

先建一個資料來源類。  控制項 -> 資料來源類 -> 資料庫操作。

這裡為了簡便,省去了資料庫的操作。只類比資料綁定的實現。

using System;using System.Collections.Generic;using System.Linq;using System.Web;/// <summary>///Student 的摘要說明/// </summary>public class Student{    private string name;    private int id;    private string info;        //類比資料源    public static List<Student> list = new List<Student>();    //提供資料來源的方法.  每次重新整理資料的時候都要調用此方法    public List<Student> getAll()    {        Student stu1 = new Student();        stu1.name = "gaotong";        stu1.id = 0;        stu1.info = "value1";        Student stu2 = new Student();        stu2.name = "helloworld";        stu2.id = 1;        stu2.info = "info2";        Student stu3 = new Student();        stu3.name = "helloworld2";        stu3.id = 2;        stu3.info = "info3";        list.Add(stu1);        list.Add(stu2);        list.Add(stu3);        return list;    }    //類比刪除資料的操作    public static void del(int id)    {        list.RemoveAt(id);    }    public string Info    {        get { return info; }        set { info = value; }    }    public string Name    {        get { return name; }        set { name = value; }    }    public int Id    {        get { return id; }        set { id = value; }    }    public Student()    {    }}

<asp:ObjectDataSource > 就是對象資料來源。

下面是綁定的頁面控制項。TypeName 是綁定的類。 SelectMethod是調用哪個方法來擷取資料。

Repeater 迭代資料來源中的每個對象。

通過 ItemTemplate 進行呈現。

LinkButton 中使用CommandArgument來傳入一些必要的參數來進行相應的操作。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %><!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>    <form id="form1" runat="server">        <asp:ObjectDataSource ID="studentDS" runat="server"         SelectMethod="getAll" TypeName="Student"></asp:ObjectDataSource>        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="studentDS">        <HeaderTemplate>            <table>            <tr><th>ID</th> <th>姓名</th> <th>資訊</th> <th>操作</th> </tr>        </HeaderTemplate>                <ItemTemplate>        <tr>            <td><%#Eval("id")%>  </td>             <td><%#Eval("name")%>  </td>             <td><%#Eval("info")%>  </td>             <td>                 <asp:LinkButton ID="removeBtn" runat="server" OnClick="removeClick" CommandName="del" CommandArgument='<%#Eval("id")%>'>                 刪除</asp:LinkButton>             </td>        </tr>                  </ItemTemplate>        <FooterTemplate>        </table>        </FooterTemplate>    </asp:Repeater>    </form></body></html>

關於viewstate: 用戶端頁面的viewstate會儲存當前頁面所用的所有資料。在進行回傳的時候,會使用viewstate儲存的資料,而不去讀取資料庫。

需使用DataBind() 方法進行資料的更新。

後台代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class index : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    protected void removeClick(object sender, EventArgs e)    {              LinkButton lbtn = (LinkButton)sender;        Response.Write(lbtn.CommandName +","+lbtn.CommandArgument);        Student.del(Int32.Parse(lbtn.CommandArgument)); //模仿資料的刪除操作        this.Repeater1.DataBind(); //重新整理資料來源。 當前的頁面資料不會更新。因為頁面中的viewstate儲存的還是最初的資料    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.