`
从此醉
  • 浏览: 1044585 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

.net中的数据绑定,ObjectDataSource,Repeater,ItemTemplate,viewstate初步学习

 
阅读更多

此文仅供个人笔记!

先建一个数据源类。 控件 -> 数据源类 -> 数据库操作。

这里为了简便,省去了数据库的操作。只模拟数据绑定的实现。


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保存的还是最初的数据
    }
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics