ASP.NET ListView全行选择
因此,我正在尝试使用ListView而不是GridView来实现一个复杂的目标。ListView在很多方面都很有帮助,但是我习惯于在GridView中使用的一段代码在ListView中不起作用 如果我必须在GridView中的行上有一个漂亮的鼠标悬停动作,并且如果我想让用户单击行中的任意位置来选择它,我将使用OnRowDataBound事件并执行类似的操作ASP.NET ListView全行选择,.net,asp.net,gridview,listview,.net,Asp.net,Gridview,Listview,因此,我正在尝试使用ListView而不是GridView来实现一个复杂的目标。ListView在很多方面都很有帮助,但是我习惯于在GridView中使用的一段代码在ListView中不起作用 如果我必须在GridView中的行上有一个漂亮的鼠标悬停动作,并且如果我想让用户单击行中的任意位置来选择它,我将使用OnRowDataBound事件并执行类似的操作 e.Row.Attributes["onmouseover"] = "this.oldClass=this.className;this.c
e.Row.Attributes["onmouseover"] = "this.oldClass=this.className;this.className='hover';";
e.Row.Attributes["onmouseout"] = "this.className=this.oldClass;";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gdvBuildings, "Select$" + e.Row.RowIndex.ToString());
它在GridView中非常有效。对于ListView,我可以使用OnItemDataBound事件,但似乎没有任何控件具有要添加的属性数组
是否有人知道一种等效的解决方案,允许鼠标悬停并使用ListView全行单击?在ListView上,您自己创建行,以便可以直接在行上添加此功能,类似这样
<asp:ListView ID="ListView3" runat="server">
<ItemTemplate>
<tr onmouseover="this.oldClass=this.className;this.className='hover';" onmouseout="this.className=this.oldClass;" onclick='<%# Page.ClientScript.GetPostBackClientHyperlink(gdvBuildings, "Select$" + Container.DataItemIndex.ToString()) %>' >
<td>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("ClientNumber") %>' />
</td>
<td>
<asp:Label ID="CityNameLabel" runat="server" Text='<%# Eval("FullName") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
来自:
我们使用一个名为MyGridView的CSS类将CSS文件添加到项目中,该类仅包含字体设置:
.MyGridView { font-family: Arial; font-size: 12px; }
我们必须定义的下一个thng是GridView行的CSS类。这样的行在内部由HTML TR标记表示。因此,我们必须为普通行和悬停时的行定义类似的类:
.MyGridView tr.row { color: #000000; background-color: #FFFFFF; }
.MyGridView tr.row:hover { background-image: url('../img/GridViewBG.jpg'); background-repeat: repeat-x; color: #333333; }
对于悬停效果,我们创建了一个名为GridViewBG.jpg的小图像,大小为2px x 30px。这是鼠标悬停在一行上时可以看到的绿色渐变
之后,我们将CSS文件添加到ASP.NET表单中。以下是表单的完整标记代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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>Untitled Page</title>
<link href="css/GridViewStyles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="0px" CellPadding="8" CssClass="MyGridView" Width="400px" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Firstname" HeaderText="Firstname">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<HeaderStyle BackColor="Green" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
(3) 显示数据
现在唯一要做的就是用一些示例数据填充GridView,这样我们就可以看到鼠标悬停的效果了。下面是我们的DataSourceProvider类,它为我们生成一些数据:
public class DataSourceProvider
{
public static DataTable GetPersons()
{
DataTable result = new DataTable();
result.Columns.Add("Name");
result.Columns.Add("Firstname");
AddPerson(result, "Matthias", "Pieroth");
AddPerson(result, "Mark", "Twain");
AddPerson(result, "Charles", "Bukowski");
AddPerson(result, "Francois", "Villon");
return result;
}
private static void AddPerson(DataTable table, string firstName, string name)
{
DataRow row = table.NewRow();
row["Name"] = name;
row["Firstname"] = firstName;
table.Rows.Add(row);
}
}
这些数据的绑定在表单的Page_Load-event中完成
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = DataSourceProvider.GetPersons();
GridView1.DataBind();
}
谢谢,悬停效果很好。但是我不能点击工作。在犯了一些愚蠢的错误之后,我确实把它编译了。单击确实会导致回发,但它并不像CommandType的按钮Select那样被称为ListView的选择事件。有什么想法吗?
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = DataSourceProvider.GetPersons();
GridView1.DataBind();
}