本来想做鼠标经过此行变色,在DataGrid上可以,但在DataList上没反应,查看源代码连<tr onmouseover="c=this.style.backgroundColor;this.style.backgroundColor='#cccccc'"......都没看见,只有<tr>,说明没获取到,下面是源码:datalist.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class dataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.BinderToDataList();
}
}
private void BinderToDataList()
{
SqlConnection conn = DB.CreateCon();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from Employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.DataList1.DataKeyField = "EmployeeID";
this.DataList1.DataSource = ds.Tables["emp"];
this.DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
this.DataList1.SelectedIndex = e.Item.ItemIndex;
this.BinderToDataList();
}
if (e.CommandName == "back")
{
this.DataList1.SelectedIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "edit")
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.BinderToDataList();
}
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "cancel")
{
this.DataList1.EditItemIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "update")
{
string empID = (this.DataList1.DataKeys[e.Item.ItemIndex]).ToString();
string LName = ((TextBox)e.Item.FindControl("txtEditLN")).Text;
string FName = ((TextBox)e.Item.FindControl("txtEditFN")).Text;
string Birth = ((TextBox)e.Item.FindControl("txtEditBD")).Text;
string Add = ((TextBox)e.Item.FindControl("txtEditAD")).Text;
string city = ((TextBox)e.Item.FindControl("txtEditCY")).Text;
SqlConnection conn = DB.CreateCon();
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update Employees set LastName='" + LName + "',FirstName='" + FName + "',BirthDate='" + Birth + "',Address='" + Add + "',City='" + city + "' where EmployeeID='"+empID+"'" ;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor=#'cccccc'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}datalist.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dataList.aspx.cs" Inherits="dataList" %><!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:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" Height="242px" Width="430px" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand" OnItemDataBound="DataList1_ItemDataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
<div align=center>模板页眉</div>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<asp:LinkButton ID="Hselect" runat="server" CommandName="select">查看详细内容</asp:LinkButton>
<asp:LinkButton ID="Hedit" runat="server" CommandName="edit">编辑</asp:LinkButton>
</ItemTemplate>
<AlternatingItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<asp:LinkButton ID="Hselect" runat="server" CommandName="select">查看详细内容</asp:LinkButton>
<asp:LinkButton ID="Hedit" runat="server" CommandName="edit">编辑</asp:LinkButton>
</AlternatingItemTemplate>
<SelectedItemTemplate>
<div>编号:<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %></div>
<div>姓氏:<%# DataBinder.Eval(Container.DataItem,"LastName") %></div>
<div>名字:<%# DataBinder.Eval(Container.DataItem,"FirstName") %></div>
<div>生日:<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:D}") %></div>
<div>住址:<%# DataBinder.Eval(Container.DataItem,"Address") %></div>
<div>城市:<%# DataBinder.Eval(Container.DataItem,"City") %> <asp:LinkButton ID="Hback" runat="server" CommandName="back">返回</asp:LinkButton></div>
</SelectedItemTemplate>
<FooterTemplate>
<div align=center>模板页脚</div>
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton ID="Hupdate" runat="server" CommandName="update">保存</asp:LinkButton>
<asp:LinkButton ID="Hcancel" runat="server" CommandName="cancel">取消</asp:LinkButton>
<div><asp:TextBox ID="txtEditID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>' Enabled=false></asp:TextBox></div>
<div><asp:TextBox ID="txtEditLN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditFN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditBD" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"BirthDate") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditAD" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Address") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditCY" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"City") %>'></asp:TextBox></div>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class dataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.BinderToDataList();
}
}
private void BinderToDataList()
{
SqlConnection conn = DB.CreateCon();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from Employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.DataList1.DataKeyField = "EmployeeID";
this.DataList1.DataSource = ds.Tables["emp"];
this.DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
this.DataList1.SelectedIndex = e.Item.ItemIndex;
this.BinderToDataList();
}
if (e.CommandName == "back")
{
this.DataList1.SelectedIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "edit")
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.BinderToDataList();
}
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "cancel")
{
this.DataList1.EditItemIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "update")
{
string empID = (this.DataList1.DataKeys[e.Item.ItemIndex]).ToString();
string LName = ((TextBox)e.Item.FindControl("txtEditLN")).Text;
string FName = ((TextBox)e.Item.FindControl("txtEditFN")).Text;
string Birth = ((TextBox)e.Item.FindControl("txtEditBD")).Text;
string Add = ((TextBox)e.Item.FindControl("txtEditAD")).Text;
string city = ((TextBox)e.Item.FindControl("txtEditCY")).Text;
SqlConnection conn = DB.CreateCon();
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update Employees set LastName='" + LName + "',FirstName='" + FName + "',BirthDate='" + Birth + "',Address='" + Add + "',City='" + city + "' where EmployeeID='"+empID+"'" ;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
this.BinderToDataList();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor=#'cccccc'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}datalist.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dataList.aspx.cs" Inherits="dataList" %><!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:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" Height="242px" Width="430px" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand" OnItemDataBound="DataList1_ItemDataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
<div align=center>模板页眉</div>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<asp:LinkButton ID="Hselect" runat="server" CommandName="select">查看详细内容</asp:LinkButton>
<asp:LinkButton ID="Hedit" runat="server" CommandName="edit">编辑</asp:LinkButton>
</ItemTemplate>
<AlternatingItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<asp:LinkButton ID="Hselect" runat="server" CommandName="select">查看详细内容</asp:LinkButton>
<asp:LinkButton ID="Hedit" runat="server" CommandName="edit">编辑</asp:LinkButton>
</AlternatingItemTemplate>
<SelectedItemTemplate>
<div>编号:<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %></div>
<div>姓氏:<%# DataBinder.Eval(Container.DataItem,"LastName") %></div>
<div>名字:<%# DataBinder.Eval(Container.DataItem,"FirstName") %></div>
<div>生日:<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:D}") %></div>
<div>住址:<%# DataBinder.Eval(Container.DataItem,"Address") %></div>
<div>城市:<%# DataBinder.Eval(Container.DataItem,"City") %> <asp:LinkButton ID="Hback" runat="server" CommandName="back">返回</asp:LinkButton></div>
</SelectedItemTemplate>
<FooterTemplate>
<div align=center>模板页脚</div>
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton ID="Hupdate" runat="server" CommandName="update">保存</asp:LinkButton>
<asp:LinkButton ID="Hcancel" runat="server" CommandName="cancel">取消</asp:LinkButton>
<div><asp:TextBox ID="txtEditID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>' Enabled=false></asp:TextBox></div>
<div><asp:TextBox ID="txtEditLN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditFN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditBD" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"BirthDate") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditAD" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Address") %>'></asp:TextBox></div>
<div><asp:TextBox ID="txtEditCY" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"City") %>'></asp:TextBox></div>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货