Untuk mengetikkan tombol tab di kotak teks, Anda dapat menggunakan skrip seperti ini (kotak teks yang menerima tombol tab dinamai txtLongText
):
[VB.NET]
txtLongText.Attributes.Add("onkeydown", _
"if(event.which || event.keyCode){if ((event.which == 9)" & _
"|| (event.keyCode == 9)) {document.getElementById('" & _
txtLongText.ClientID + "').selection = " & _
document.selection.createRange();" & _
txtLongText.ClientID & ".selection.text = " & _
" String.fromCharCode(9);return false;}} else {return true}; ")
[C #]
txtLongText.Attributes.Add("onkeydown",
"if(event.which || event.keyCode){if ((event.which == 9)" +
"|| (event.keyCode == 9)) {document.getElementById('"+
txtLongText.ClientID + "').selection = document.selection.createRange();" +
txtLongText.ClientID + ".selection.text = String.fromCharCode(9);return false;}} else {return true}; ");
Atau lebih baik, untuk menghindari pengkodean yang sulit, Anda dapat memasukkan kode ini ke dalam suatu fungsi bernama EnableTabType
. Fungsi hanya memiliki satu parameter, yang menentukan apa yang TextBox
mengontrol di mana Anda perlu mengaktifkan pengetikan karakter Tab.
[VB.NET]
Public Sub EnableTabType(tb As TextBox)
tb.Attributes.Add("onkeydown", _
"if(event.which || event.keyCode){if((event.which == 9)" & _
"|| (event.keyCode == 9)) {document.getElementById('" & _
tb.ClientID & "').selection=document.selection.createRange();" & _
tb.ClientID & ".selection.text = " & _
" String.fromCharCode(9);return false;}}else{return true};")
End Sub
[C #]
public void EnableTabType(TextBox tb)
{
tb.Attributes.Add("onkeydown",
"if(event.which || event.keyCode){if ((event.which == 9)" +
"|| (event.keyCode == 9)) {document.getElementById('"+
tb.ClientID + "').selection = document.selection.createRange();" +
tb.ClientID + ".selection.text = String.fromCharCode(9);return false;}} else {return true}; ");
}
Sumber: http://www.beansoftware.com/ASP.NET-Tutorials/Access-Tab-Key.aspx