此文仅供个人笔记!
先建一个数据源类。 控件 -> 数据源类 -> 数据库操作。
这里为了简便,省去了数据库的操作。只模拟数据绑定的实现。
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保存的还是最初的数据
}
}
分享到:
相关推荐
给ASP.NET中的TreeView绑定DataSet或ObjectDataSource对象
以下为设计步骤: 1、在C# 中连接数据库。...4、在aspx的webform上放一个Repeater控件,用它的向导设定它的DataSourceID为上面的ObjectDataSource5、在网页中设定它的源码,即加上<itemTemplate>等
这个是《在Asp.net 2.0中操作数据:使用ObjectDataSource展现数据》相应的源码,大家可以下来一起看看。
在ASP.NET 2.0中操作数据:用ObjectDataSource缓存数据(源码)
在ASP.NET 2.0中操作数据:用ObjectDataSource缓存数据
讲述ASP.NET2.0 ObjectDataSource的使用
包括SqlDataSource、ObjectDataSource 添加、编辑,显示等等 的项目网站代码示例
使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现...
使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现...
ASP.NET 3.5中新增加的ListView控件是一个用于页面数据绑定和界面布局的非常棒的控件,它在ASP.NET 2.0 GridView的基础上做了很多改进,用户在使用时可以控制的元素更多,开发时的灵活性更大了,个人觉得ListView在...
通过将模板化控件的 DataSourceID 属性设置为数据源控件的 ID,可以将模板化控件绑定到数据源控件(例如 LinqDataSource、ObjectDataSource 或 SqlDataSource 控件)。然后可以在模板中使用 Eval 和 Bind 函数绑定到...
这个是asp.net 2.0 教程之三层开发教程中的第四篇,从头到尾都看过,也做过相应的实验,觉得没有什么问题,发上来大家一起看看吧。
获取或设置某个类的名称,ObjectDataSource 控件将该类用于更新、插入或删除数据操作中的参数,而不是从数据绑定控件传递个别的值。 您不用指定传递给 Update、Insert 和 Delete 方法的多个参数,而是可以创建一个...
SPGridView绑定数据(ObjectDataSource,SPDataSource)实例。 说明ObjectDataSource,SPDataSource作为数据源的有点与缺点。。。 文档为office2010的。。
11.1.9 在gridview控件中使用viewstate397 11.2 在gridview控件中使用字段398 11.2.1 使用boundfield398 11.2.2 使用checkboxfield401 11.2.3 使用commandfield402 11.2.4 使用buttonfield404 11.2.5 使用...
这个是《在Asp.net 2.0中操作数据:编程设置ObjectDataSource的参数值》相应的源码,大家可以下来一起看看。
利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法 asp.net报表解决方法 SQLDMO类的使用 SQL过程自动C#封装,支持从表到基本存储过程生成 使用SQLDMO控制 SQL Server 使用SQL...
11.1.9 在GridView控件中使用ViewState 349 11.2 在GridView控件中使用字段 349 11.2.1 使用BoundField 350 11.2.2 使用CheckBoxField 352 11.2.3 使用CommandField 353 11.2.4 使用ButtonField 355 11.2.5 使用...
本程序详细的讲述了如何通过objectdatasource来实现gridview的各种功能。本程序无任何错误,下载即可运行
然而,ASP.NET 2.0的数据源控件提供了一种新的方式,使用这些控件可以让你轻松从上文中创建的业务逻辑层中进行数据绑定,甚至不需要手写一行的代码。 ASP.NET 2.0提供了五种内置的数据源控件:SqlDataSou