数据绑定控件ListView事件
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using ListViewTest.DAL.UserSTableAdapters;
namespace ListViewTest
{
public partial class List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 数据插入之前调用,时间是在用户完成输入操作以后,由程序向数据库中插入的时候调用
/// 可以用此事件的方法为数据字段设置初始值,比如判断用户是否输入,没有输入的话给一个值,或者不允许插入;再比如有关Guid的值,用户是没有办法输入的,所以由此方法进行赋值,而有人则说可以在ItemCreate中设置的嘛,不
/// 过,对于Guid之类的数据,是没有必要显示给用户的,所以在没有 显示的时候在ItemCreate使用FindControl是找不到控件的,所以就只有在这个方法中设置喽
/// 此方法也可以做后台验证,比如插入数据的时候某个字段不应该为空,则需要在此方法中再做一次验证,在前台可以使用数据验证控件进行验证,不过后台还是应该在验证一次
/// 也可以对用户的输入数据进行一些调整
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
e.Values["Id"] = Guid.NewGuid();
//某不应该为空的字段没有赋值的时候则取消插入
if (e.Values["Age"] == null)
{
e.Cancel = true;
}
DropDownList SexDropDownList = (DropDownList)e.Item.FindControl("SexDropDownList");
e.Values["Sex"] = SexDropDownList.SelectedValue;
}
/*数据更新的时候调用
* 要判断一下e.item.type的类型,总共有三,种类型DataItem、InsertItem、EmptyItem 为 ListViewItemType的枚举元素类型
* 在显示页面的元素的时候,如果是自定义的服务端控件,则需要在AlternatingItemTemplate与ItemTemplate均做修改,可以认为ItemTemplate是显示奇数行数据,AlternatingItemTemplate显示的事偶数行数据。而当没有AlternatingItemTemplate的时候,ItemTemplate操作的是全部数据
*/
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DropDownList SexDropDownList = (DropDownList)e.Item.FindControl("SexDropDownList");
DataRowView rowView = (DataRowView)e.Item.DataItem;
var usersRow = (ListViewTest.DAL.UserS.UserSRow)rowView.Row;
if (usersRow.Age > 20)
{
HtmlTableRow trRow = (HtmlTableRow)e.Item.FindControl("trRow");
trRow.Attributes["class"] = "error";
}
//当进行字符串赋值的时候最好能够进行Trim去空格处理,不然会发生错误
SexDropDownList.SelectedValue = usersRow.Sex.Trim();
}
}
/// <summary>
/// 每一行在页面上创建的时候都会调用,没错是每一行,所以要先判断e.item.itemtype的类型,然后再对不同类型的事件做不同的操作,不然会使做很多无用功,降低显示效率。比如在显示数据的时候,会造成覆盖更新
/// 可以用此为控件设置默认值,记住是默认值
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.InsertItem)
{
TextBox userNameTextBox = (TextBox)e.Item.FindControl("UserNameTextBox");
userNameTextBox.Text = "小耗子";
TextBox AgeTextBox = (TextBox)e.Item.FindControl("AgeTextBox");
AgeTextBox.Text = "20";
TextBox SexTextBox = (TextBox)e.Item.FindControl("SexTextBox");
SexTextBox.Text = "man";
}
}
/// <summary>
/// 在更新到数据库之前的操作
/// 对用户输入的数据做处理
/// 与inserting作用类似
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int index = e.Item.DisplayIndex;//获得行号
Guid id = (Guid)ListView1.DataKeys[index].Value;//获得行的主键
T_WareMessageTableAdapter adapter = new T_WareMessageTableAdapter();
if (e.CommandName == "ArcName")
{
adapter.ArcPrice(id);
ListView1.DataBind();
}
}
} }
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="ListViewTest.List" %>
<!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">
.error
{
background-color:Blue;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData"
TypeName="ListViewTest.DAL.UserSTableAdapters.UserSTableAdapter">
<InsertParameters>
<asp:Parameter DbType="Guid" Name="Id" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Age" Type="Int32" />
<asp:Parameter Name="Sex" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
<div>
</div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"
InsertItemPosition="LastItem" oniteminserting="ListView1_ItemInserting"
onitemdatabound="ListView1_ItemDataBound"
onitemcreated="ListView1_ItemCreated" onitemupdating="ListView1_ItemUpdating">
<AlternatingItemTemplate>
<tr runat="server" id="trRow" >
<td>
</td>
<td>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label ID="SexLabel" runat="server" Text='<%# Eval("Sex") %>' />
<asp:DropDownList ID="SexDropDownList" runat="server" >
<asp:ListItem Value="man">男</asp:ListItem>
<asp:ListItem Value="woman">女</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="background-color: #999999;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
</td>
<td>
<asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
</td>
<td>
<asp:TextBox ID="UserNameTextBox" runat="server"
Text='<%# Bind("UserName") %>' />
</td>
<td>
<asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
</td>
<td>
<asp:TextBox ID="SexTextBox" runat="server" Text='<%# Bind("Sex") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>
未返回数据。</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
</td>
<td>
<asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
</td>
<td>
<asp:TextBox ID="UserNameTextBox" runat="server"
Text='<%# Bind("UserName") %>' ValidationGroup='插入' /><asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*" ControlToValidate="UserNameTextBox"></asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
</td>
<td>
<asp:TextBox ID="SexTextBox" runat="server" Text='<%# Bind("Sex") %>' />
<asp:DropDownList ID="SexDropDownList" runat="server" >
<asp:ListItem Value="man">男</asp:ListItem>
<asp:ListItem Value="woman">女</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr runat="server" id="trRow" >
<td>
</td>
<td>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label ID="SexLabel" runat="server" Text='<%# Eval("Sex") %>' />
<asp:DropDownList ID="SexDropDownList" runat="server" >
<asp:ListItem Value="man">男</asp:ListItem>
<asp:ListItem Value="woman">女</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #E0FFFF;color: #333333;">
<th runat="server">
</th>
<th runat="server">
Id</th>
<th runat="server">
UserName</th>
<th runat="server">
Age</th>
<th runat="server">
Sex</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="background-color: #E2DED6;font-weight: bold;color: #333333;">
<td>
</td>
<td>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label ID="SexLabel" runat="server" Text='<%# Eval("Sex") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</form>
</body>
</html>
闲话:
今天发现把学习笔记直接注释到代码里面会看着更舒服,也更加方便。
昨天大概是因为前天晚上睡觉睡得太晚的缘故,一整天大脑的运行速度都不够正常,导致学习的时候效率不是很高,临睡觉的时候脑袋昏昏。
以后还是不能够睡得太晚。

浙公网安备 33010602011771号