--=================================== [頁面] ===============================================--
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test_GridView.aspx.vb" Inherits="Test_GridView" %>
<!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>
<Script type="Text/JavaScript" Language="JavaScript">
var show = function (e) {
var id_var = document.activeElement.id; //讀取ID
id_var = id_var.toLowerCase(); //轉小寫
id_var = id_var.substring(id_var.indexOf("txt_"), id_var.length)//擷取欄位ID
document.getElementById('SelectColumn').value = id_var//放置暫存區
};
function move(e) {
var key = 0;
if (window.event)
key = event.keyCode;
else
key = e.keyCode;
if (document.getElementById('SelectColumn').value != '') {
if (key == 38)
document.getElementById('ButtonUp').click();
if (key == 40)
document.getElementById('ButtonDown').click();
}
}
document.onkeydown = move;
</Script>
<body>
<form id="form1" runat="server">
<div>
<table width="800px" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td>
<asp:Button ID="ButtonUp" runat="server" Text="ButtonUp" style="display:none"></asp:Button>
<asp:Button ID="ButtonDown" runat="server" Text="ButtonDown" style="display:none"></asp:Button>
<asp:TextBox ID="SelectColumn" runat="server" style="display:none"></asp:TextBox>
<asp:Label ID="lab_Message" runat="server" ForeColor="Red"> </asp:Label>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gv_Test" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="True" AllowSorting="True" PageSize="20" DataKeyNames="SerialNo" >
<EditRowStyle BackColor="#2461BF" Font-Size="Small" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle CssClass="font2" BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Left" />
<RowStyle CssClass="font2" BackColor="#EFF3FB" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" >
<ItemStyle Font-Size="Small" Width="40px" />
<ControlStyle Width="30px" />
</asp:CommandField>
<asp:CommandField ShowEditButton="True" >
<ItemStyle Font-Size="Small" Width="30px" />
<ControlStyle Width="30px" />
</asp:CommandField>
<asp:TemplateField HeaderText="SerialNo" SortExpression="SerialNo">
<EditItemTemplate>
<asp:TextBox ID="txt_SerialNo" runat="server" Text='<%# Bind("SerialNo") %>' Width="60px" MaxLength="6"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lab_SerialNo" runat="server" Text='<%# Bind("SerialNo") %>' Width="60px"></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
--=================================== [程式] ===============================================--
Imports System.Data
Partial Class Test_GridView
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("sG_gvName") = "" '要顯示的gvName
Call sub_ShowData()
End If
End Sub
'函式_顯示資料
Private Sub sub_ShowData()
Dim sL_SQL, sP_SItem, sP_STxt As String
sP_SItem = ViewState("sG_SItem")
sP_STxt = ViewState("sG_STxt")
Dim dv As DataView
sL_SQL = ""
sL_SQL += " Select top 10 [SerialNo]"
sL_SQL += " From [XXXXX] Where 1=1 "
dv = GetSQLData(sL_SQL)
dv.Sort = ViewState("_Sort")
gv_Test.DataSource = dv
gv_Test.DataBind()
End Sub
'動作_gv_AIUC_P_[Edit]
Protected Sub gv_Test_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Test.RowEditing
gv_Test.EditIndex = e.NewEditIndex
Call sub_ShowData()
End Sub
'動作_gv_AIUC_P_[Cance]
Protected Sub gv_Test_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv_Test.RowCancelingEdit
gv_Test.EditIndex = -1
SelectColumn.Text = ""
Call sub_ShowData()
End Sub
'動作_gv_AIUC_P_[排序]
Protected Sub gv_Test_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv_Test.Sorting
If e.SortExpression = ViewState("_Sort") Then
If ViewState("_Type") = " ASC" Then
ViewState("_Type") = " DESC"
Else
ViewState("_Type") = " ASC"
End If
ViewState("_Sort") = e.SortExpression & ViewState("_Type")
Else
ViewState("_Type") = " ASC"
ViewState("_Sort") = e.SortExpression
End If
Call sub_ShowData()
End Sub
'動作_gv_AIUC_P_[上下鍵]處理準備
Protected Sub gv_Test_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_Test.RowDataBound
'[上下鍵]處理
Dim txt_ag As TextBox
Dim sL_gvControlsName As String = ""
If e.Row.RowType = DataControlRowType.DataRow Then
'當處理列 = 修改列時
If gv_Test.EditIndex = e.Row.DataItemIndex Mod gv_Test.PageSize Then
'處理每一欄
For i As Integer = 0 To e.Row.Cells.Count - 1
Try '沒有資料時會發生錯誤,故用Try包起來
sL_gvControlsName = e.Row.Cells(i).Controls(1).ID
Catch ex As Exception
End Try
If Not IsNothing(sL_gvControlsName) Then
If InStr(sL_gvControlsName, "txt_") Then '判斷只有txt_開頭的欄位才加入onclick事件
txt_ag = e.Row.FindControl(sL_gvControlsName)
If Not IsNothing(txt_ag) Then
txt_ag.Attributes.Add("onclick", "show(event);")
End If
End If
End If
Next
End If
End If
End Sub
#Region "方向鍵處理"
'按鈕_[向下鍵]
Protected Sub ButtonDown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonDown.Click
s_gvFocus("gv_Test", "Down")
Call sub_ShowData()
End Sub
'按鈕_[向上鍵]
Protected Sub ButtonUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonUp.Click
s_gvFocus("gv_Test", "Up")
Call sub_ShowData()
End Sub
'函式_[gv上下鍵處理]
Private Sub s_gvFocus(ByVal gvName As String, ByVal OnClickType As String)
Dim gv As GridView
Dim bL_Check As Boolean = False
Dim i, iL_sIndex, iL_gvCount, iL_gvPageSize, iL_gvPageCount, iL_gvPageIndex As Integer
Dim txt_AGCode As TextBox
Dim sL_hisName As String = SelectColumn.Text.Trim '目前選到哪一欄
lab_Message.Text = ""
'檢查
If gvName = "" Or OnClickType = "" Then
Exit Sub
Else
gv = FindControl(gvName) '哪一個gv
If IsNothing(gv) Then
Exit Sub
End If
End If
i = gv.EditIndex '目前所選的修改列
If i = -1 Then
Exit Sub
End If
iL_sIndex = 0 '要移至目標列
iL_gvCount = gv.Rows.Count 'gv共幾筆
iL_gvPageCount = gv.PageCount 'gv共幾頁
iL_gvPageSize = gv.PageSize 'gv每頁共幾筆
iL_gvPageIndex = gv.PageIndex 'gv目前在第幾頁
'判斷按鈕類別
Select Case OnClickType
Case "Down" '向下
iL_sIndex = i + 1
Case "Up" '向上
iL_sIndex = i - 1
Case Else
Exit Sub
End Select
'判斷目前的修改列,是否可以移動
If iL_sIndex >= 0 And iL_sIndex < iL_gvPageSize And iL_sIndex < iL_gvCount Then
'在顯示筆數內
bL_Check = True
ElseIf iL_sIndex < 0 And iL_gvPageIndex > 0 Then
'當修改在第一筆時且又按了向[上]鍵 iL_sIndex應該會等於-1 ,且又不在第一頁
iL_sIndex = iL_gvPageSize - 1 '修改最後一筆
gv.PageIndex = iL_gvPageIndex - 1 '換頁
bL_Check = True
ElseIf iL_sIndex = iL_gvPageSize And iL_gvPageIndex < iL_gvPageCount - 1 Then
'當修改最後一筆時且又按了向[下]鍵 iL_sIndex應該會等於iL_gvPageSize ,且又不在最後一頁
iL_sIndex = 0 '修改第一筆
gv.PageIndex = iL_gvPageIndex + 1 '換頁
bL_Check = True
End If
'目標列動作
If bL_Check Then
gv.EditIndex = iL_sIndex '設定目標列為修改列
'移動後處理作業
'..............
'當gv有刪除資料作業時,此段會發生問題,所以包起來
Try
txt_AGCode = gv.Rows(iL_sIndex).FindControl(sL_hisName) '帶入所選的欄位名稱
If Not IsNothing(txt_AGCode) Then
txt_AGCode.Focus() '欄位焦點
txt_AGCode.Attributes.Add("onfocus", "this.select();") '欄位全選
End If
Catch ex As Exception
End Try
End If
End Sub
#End Region
End Class
留言列表