public int Suma(params int[] values)
{
int suma = 0;
for (int i = 0; i < values.Length; i++)
suma += values[i];
return suma;
}
Bastante fácil, ¿verdad?Happy coding!
Diario de un desarrollador al que también le gusta correr en sus ratos libres...
public int Suma(params int[] values)
{
int suma = 0;
for (int i = 0; i < values.Length; i++)
suma += values[i];
return suma;
}
Bastante fácil, ¿verdad?$(document).ready(function(){
$(document).bind("contextmenu", function(e){
return false;
});
});
Si lo quisiéramos poner en todas las páginas de nuestra aplicación web tan solo debemos poner el código anterior en nuestra MasterPage (ASP.NET) o Layout Page (ASP.NET MVC).<asp:TextBox ID="TextBoxMultiLine" MaxLength="1" TextMode="MultiLine" runat="server"></asp:TextBox>Que en html quedaría
<textarea name="ctl00$MainContent$TextBoxMultiLine" rows="2" cols="20" id="MainContent_TextBoxMultiLine"></textarea>Para hacer que funcione podemos usar el siguiente código en jQuery
$(document).ready(function () {
$('[id$=_TextBoxMultiLine]').keypress(function (e) {
if ($(this).val().length + 1 > 1)
e.preventDefault();
});
});
El motivo de usar en el selector de jQuery el identificador _TextBoxMultiLine, es por la forma que tiene ASP.NET de asegurarse que un identificador sea único en la página. Si usamos ASP.NET 4.0 podemos hacer uso del modificador ClientIDMode y asignarlo con Static para evitar que el ID cambie. En se caso el script quedaría de la siguiente
<asp:TextBox ID="TextBoxMultiLine" MaxLength="1" TextMode="MultiLine" ClientIDMode="Static" runat="server"></asp:TextBox>
$(document).ready(function () {
$('#TextBoxMultiLine').keypress(function (e) {
if ($(this).val().length + 1 > 1)
e.preventDefault();
});
});
Otra posibilidad es no poner usar el atributo ClientIDMode y usar el ClientID de la siguiente manera
$(document).ready(function () {
$('#<%= TextBoxMultiLine.ClientID %>').keypress(function (e) {
if ($(this).val().length + 1 > 1)
e.preventDefault();
});
});
Si no queremos usar jQuery, podríamos llamar al un método en el evento keyUp. Para eso añadimos esto en el Page_Load de nuestra páginaTextBoxMultiLine.Attributes.Add("onKeyUp", "Count(this, 1)");
function Count(text, long) {
var maxlength = new Number(long);
if (text.value.length > maxlength)
text.value = text.value.substring(0, maxlength);
}
$('input').keydown(function(e) {
// Admite [0-9], BACKSPACE y TAB
if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105) && e.keyCode != 8 && e.keyCode != 9)
e.preventDefault();
});
Pueden probar el código aquí ( http://jsfiddle.net/z2aEL/ )
public string Encrypt(string input, HashAlgorithm hashAlgorithm)
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashedBytes = hashAlgorithm.ComputeHash(inputBytes);
StringBuilder output = new StringBuilder();
for (int i = 0; i < hashedBytes.Length; i++)
output.Append(hashedBytes[i].ToString("x2").ToLower());
return output.ToString();
}
public string MD5Encrypt(string input)
{
return Encrypt(input, new MD5CryptoServiceProvider());
}
public string SHA256Encrypt(string input)
{
return Encrypt(input, new SHA256CryptoServiceProvider());
}
No debemos olvidarnos el importar el espacio de nombres System.Security.Cryptography.
using System.Security.Cryptography;Happy coding!
public string SHA256Encrypt(string input)
{
SHA256CryptoServiceProvider provider = new SHA256CryptoServiceProvider();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashedBytes = provider.ComputeHash(inputBytes);
StringBuilder output = new StringBuilder();
for (int i = 0; i < hashedBytes.Length; i++)
output.Append(hashedBytes[i].ToString("x2").ToLower());
return output.ToString();
}
También hay que tener en cuenta que debemos importar el espacio de nombres System.Security.Cryptography.
using System.Security.Cryptography;
SHA256('') = 4574890e042bd574ae8d75562fe5bca34e7e5b518d9bbe2a7d5b39d1f6ed63c1
SHA256('SHA256') = b3abe5d8c69b38733ad57ea75e83bcae42bbbbac75e3a5445862ed2f8a2cd677
public static string MD5Encrypt(string value)
{
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.ASCII.GetBytes(value);
data = provider.ComputeHash(data);
string md5 = string.Empty;
for (int i = 0; i < data.Length; i++)
md5 += data[i].ToString("x2").ToLower();
return md5;
}
using System.Security.Cryptography;
MD5('') = d41d8cd98f00b204e9800998ecf8427e
MD5('MD5') = 7f138a09169b250e9dcb378140907378
Happy coding!
using (FooEntities entities = new FooEntities())
{
var result = (from p in entities.Bar
where p.IdBar.Equals(1001)
select p);
System.Diagnostics.Trace.Write(string.Format("SQL: {0}", ((System.Data.Objects.ObjectQuery)result).ToTraceString()));
}
Esto nos devolverá en nuestra ventana de Resultados lo siguienteSQL: SELECT [Extent1].[IdBar] AS [IdBar], [Extent1].[BarDesc] AS [BarDesc], [Extent1].[Valor] AS [Valor], [Extent1].[FC] AS [FC], [Extent1].[UC] AS [UC] FROM [dbo].[Bar] AS [Extent1] WHERE 1001 = [Extent1].[IdBar]Algo bastante simple que nos puede ayudar bastante en algún momento.
<img src="@Url.Action("showimage", "rest", new { id = 1 })" alt="" />
public ActionResult showimage(int id)
{
var path = Server.MapPath("~/content/image");
var file = string.format("{0}.png", id);
var fullPath = Path.Combine(path, file);
return File(fullPath, "image/png", file);
}