gridview mouse click 後 cell變色 並帶入 text

MStone Lin
請問

我參考這篇

https://stackoverflow.com/questions/16543683/vb-net-mouse-click-single-cell-of-gridview-change-backcolor-and-output-cell-pos

執行後 卻一直出現JavaScript 執行階段錯誤: 'txtCellSelected' 未經定義

請問 要如何排除



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <style type="text/css">

        .style11
        {
            font-size: large;
        }
        </style>
        
  <script type="text/javascript">
      function highlight(my_row, cell, textbox) {
          document.getElementById("<%=txtCellSelected%>").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 style="font-family: 微軟正黑體" background="BG.png" topmargin="0">
    <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>





 Private Sub GridView1_PreRender(ByVal sender As Object, ByVal 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




阿恢

document.getElementById("<%=txtCellSelected%>").value = my_row + ',' + cell;
改成
document.getElementById("<%=txtCellSelected.ClientID%>").value = my_row + ',' + cell;
試試...
回到頂部