我在cell表单中有一个网格视图,我想知道如何使用鼠标单击来选择该网格视图中任何位置的单个单元格。
然后,所选单元格的背景颜色变为特定颜色,表单上的文本框显示行号和列号,我将把它们作为参数传递给存储过程。
当选择后续单元格时,最后选择的单元格将恢复为其原始颜色,新单元格的背景颜色将更改,文本框将更新为新单元格的行号和列号。
到目前为止,我得到的最接近的结果是选择整行,但即使这样也只影响该行的第一个单元格。下划线会影响行中的所有单元格。
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
For Each row As GridViewRow In gvProgressGrid.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Attributes("onclick") = "this.style.cursor='pointer';this.style.ine';this.style.backgroundColor ='#EEE'"
End If
Next
MyBase.Render(writer)
End Sub发布于 2013-05-16 23:11:15
基本上:在后面的代码中,您为每个单元格设置一个onclick脚本,传递单元格的坐标和结果的文本框。
在aspx中,js脚本将单击单元格的坐标写入文本框中,迭代表中的所有单元格,将颜色设置为白色,最后仅将单击单元格的背景颜色设置为红色。
aspx代码:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestEvidenziazione.aspx.vb" Inherits="Web_Test_2010.TestEvidenziazione" %>
<!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>
<script type="text/javascript">
function highlight(my_row, cell, textbox) {
document.getElementById("<%=txtCellSelected.ClientID%>").value = my_row + ',' + cell;
var table = document.getElementById("<%=GridView1.ClientID%>");
for (var i = 0, row; row = table.rows[i]; i++) {
//iterate through rows
//rows would be accessed using the "row" variable assigned in the for loop
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns
//columns would be accessed using the "col" variable assigned in the for loop
col.style.backgroundColor = '#ffffff';
if (i == my_row && j == cell) {
col.style.backgroundColor = '#ff0000';
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtCellSelected" runat="server"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeader="false">
</asp:GridView>
</div>
</form>
</body>
</html>vb.net代码:
Public Class TestEvidenziazione
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadData()
End Sub
Private Sub LoadData()
Dim list As New ArrayList
Dim row1 As New myRowClass
Dim row2 As New myRowClass
Dim row3 As New myRowClass
row1.a = "0,0"
row1.b = "0,1"
row1.c = "0,2"
row2.a = "1,0"
row2.b = "1,1"
row2.c = "1,2"
row3.a = "2,0"
row3.b = "2,1"
row3.c = "2,2"
list.Add(row1)
list.Add(row2)
list.Add(row3)
GridView1.DataSource = list
GridView1.DataBind()
End Sub
Private Class myRowClass
Public Property a As String
Public Property b As String
Public Property c As String
End Class
Private Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
For index_row = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(index_row).RowType = DataControlRowType.DataRow Then
For index_cell = 0 To GridView1.Rows(index_row).Cells.Count - 1
GridView1.Rows(index_row).Cells(index_cell).Attributes("onclick") = "highlight(" & index_row.ToString & "," & index_cell.ToString & ", " & txtCellSelected.ClientID & "); "
Next
End If
Next
End Sub
End Class发布于 2013-05-17 23:22:15
注意:以下代码未经过测试。您可能需要对其进行修改以满足您的需求。
在回答您的另一个问题时-将单元格恢复为其原始颜色-您可以添加一个包含必要信息的自定义属性。
您可以按如下方式修改Andrea的代码:
Private Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
For index_row = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(index_row).RowType = DataControlRowType.DataRow Then
For index_cell = 0 To GridView1.Rows(index_row).Cells.Count - 1
GridView1.Rows(index_row).Cells(index_cell).Attributes("onclick") = "highlight(" & index_row.ToString & "," & index_cell.ToString & ", " & txtCellSelected.ClientID & "); "
' Change begins here...
Dim l_bg = GridView1.Rows(index_row).Cells(index_cell).BackColor
GridView1.Rows(index_row).Cells(index_cell).Attributes.Add( _
"data-defaultBackground", _
String.Format( "#{0:X2}{1:X2}{2:X2}{3:X2}", l_bg.A, l_bg.R, l_bg.G, l_bg.B )
)
Next
End If
Next
End Sub然后切换这个javascript代码:
col.style.backgroundColor = '#ffffff';要这样做:
col.style.backgroundColor = col.getAttribute("data-defaultBackground");这段代码使用HTML5数据属性标准。有关更多信息,请参阅John Resig的博客文章HTML 5 data- Attributes。该标准相对较新,支持可能参差不齐。
请注意,您可能有一些trouble with getAttributes。
我从an answer by VirtualBlackFox获得了将颜色转换为十六进制字符串的代码
请考虑将答案/奖金授予 ,因为他/她完成了所有实际工作。
发布于 2013-05-18 23:33:13
我将使用jQuery结合Cyborg和Andrea给出的答案来解决这个问题。jQuery将为您提供更具可读性的javascript代码。
https://stackoverflow.com/questions/16543683
复制相似问题