<< Click to Display Table of Contents >> Criando teclas de atalho (F8) para inserir string em TextBox |
![]() ![]() ![]() |
Descrição
Neste exemplo, ao pressionar F8 num TextBox (MultiLine = true) irá inserir no final do texto uma variável de sessão e data e hora.
Default.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="ContentCorpo" runat="server">
<script type="text/javascript">
function AdicionarUpdate(elemento, event) {
var key = event.keyCode;
if (event.keyCode == 119) // F8
{
// data corrente
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth();
var year = currentDate.getFullYear() - 2000;
// hora corrente
var hours = currentDate.getHours()
var minutes = currentDate.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
var usuario = "<%=Session["nome"]%>";
// retorna ao elemento
var valor = elemento.value + "---------------------------\n\r" + usuario + " - " + day + "/" + month + "/" + year + " " + hours + ":" + minutes + "\n\r";
elemento.value = valor;
}
return true;
}
</script>
<asp:TextBox ID="txtDetalhes" runat="server" Height="143px" TextMode="MultiLine" Width="697px" onKeyDown="return AdicionarUpdate(this, event);" />