Pesquisa

segunda-feira, 7 de março de 2011

Formatar dinheiro R$

Para exibir na forma de String uma variável float ou decimal basta aplicar as regras do String.Format com a utilização de duas casas decimais.

Abaixo um exemplo de como pode ser formatado:

System.Globalization.CultureInfo culturaBrasileira = 
new System.Globalization.CultureInfo("pt-BR");
String dinheiroFormatado = String.Empty;
float dinheiro = 45612.54F;

dinheiroFormatado = String.Format(culturaBrasileira, "R$ {0:F2}", dinheiro);




Customização RadioButtonList

O controle RadioButtonList gera uma série de tags HTML que para o desenvolvimento do CSS acaba dificultando sua estilização. Além disso, semânticamente o HTML gerado deveria ser baseado em UL (<UL>) e LI (<LI>).

Assim como na customização do updatepanel, basta sobrescrever o método responsável pela geração do HTML. Neste caso, o método Render.

Abaixo tem um exemplo:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Customizado.ControlLibrary
{
[ToolboxData("<{0}:CustomizadoRadioButtonList runat=server></{0}:CustomizadoRadioButtonList>")]
public class CustomizadoRadioButtonList : System.Web.UI.WebControls.RadioButtonList
{
protected override void Render(HtmlTextWriter writer)
{
// We start our un-ordered list tag.
writer.WriteBeginTag("ul");

// If the CssClass property has been assigned, we will add
// the attribute here in our <ul> tag.
if (this.CssClass.Length > 0)
{
writer.WriteAttribute("class", this.CssClass);
}

// We need to close off the <ul> tag, but we are not ready
// to add the closing </ul> tag yet.
writer.Write(">");

// Now we will render each child item, which in this case
// would be our checkboxes.
for (int i = 0; i < this.Items.Count; i++)
{
// We start the <li> (list item) tag.
writer.WriteFullBeginTag("li");

this.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);

// Close the list item tag. Some people think this is not
// necessary, but it is for both XHTML and semantic reasons.
writer.WriteEndTag("li");
}

// We finish off by closing our un-ordered list tag.
writer.WriteEndTag("ul");
}
}
}



Fonte: http://www.singingeels.com/Articles/Semantic_CheckBoxList__RadioButtonList.aspx

Customização UpdatePanel

Para personalizar a tag HTML que é gerada pelo update panel, basta sobrescrever o método RenderChildren.

Abaixo tem um exemplo baseado de um post estraído do http://weblogs.asp.net.

Pode-se customizar até mesmo a classe que será utlizado no elemento do UpdatePanel.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Customizado.ControlLibrary
{
[ToolboxData("<{0}:CustomizadoUpdatepanel runat=server></{0}:CustomizadoUpdatepanel>")]
public class CustomizadoUpdatepanel : System.Web.UI.UpdatePanel
{
private string cssClass;
private HtmlTextWriterTag elemento = HtmlTextWriterTag.Div;

[DefaultValue("")]
[Description("Applies a CSS style to the panel.")]
public string CssClass
{
get
{
return cssClass ?? String.Empty;
}
set
{
cssClass = value;
}
}

// Hide the base class's RenderMode property since we don't use it
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new UpdatePanelRenderMode ModoRenderizar
{
get
{
return base.RenderMode;
}
set
{
base.RenderMode = value;
}
}

[DefaultValue(HtmlTextWriterTag.Div)]
[Description("A tag para renderizar o painel")]
public HtmlTextWriterTag Elemento
{
get
{
return elemento;
}
set
{
elemento = value;
}
}

protected override void RenderChildren(HtmlTextWriter escrever)
{
if (IsInPartialRendering)
{
// If the UpdatePanel is rendering in "partial" mode that means
// it's the top-level UpdatePanel in this part of the page, so
// it doesn't render its outer tag. We just delegate to the base
// class to do all the work.
base.RenderChildren(escrever);
}
else
{
// If we're rendering in normal HTML mode we do all the new custom
// rendering. We then go render our children, which is what the
// normal control's behavior is.
escrever.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
if (CssClass.Length > 0)
{
escrever.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
}
escrever.RenderBeginTag(Elemento);
foreach (Control filho in Controls)
{
filho.RenderControl(escrever);
}
escrever.RenderEndTag();
}
}
}
}



quarta-feira, 2 de março de 2011

Yield - Retornar uma instância do tipo IEnumerable ou IEnumerator

O Yield é utilizado para retornar uma instância do tipo IEnumerable ou IEnumerator, em casos que eu tenho um método que retorne vários valores, o ganho de performance é considerável se você tem uma coleção de dados extensa, observe abaixo:

Confira abaixo como utilizar o yield:

class Program
{
static void Main(string[] args)
{
foreach (int num in ObterLista())
{
Console.WriteLine(num);
}
}

public static IEnumerable ObterLista()
{
for (int i = 0; i < 100; i++)
{
yield return i;
}
}
}


public IEnumerable GetImpares_Yield(Int32 maxNum)
{
int num = 0;

while (true)
{
System.Threading.Thread.Sleep(1000);
num++;

if (num % 2 == 1)
yield return num;

if (num >= maxNum)
yield break;
}
}


Créditos: http://devrs.net/post/desmistificando-o-yield/

segunda-feira, 3 de janeiro de 2011

Imagem com cantos arredondados

Para fazer imagens com cantos arredondados sem que aconteça problemas em diferentes navegadores, basta renderizar as imagens no servidor.

Segue abaixo um exemplo de como fazer:

Pagina RoundCorner.aspx:

Página responsável por ajustar os cantos das imagens.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<script runat="server">

void Page_Init(Object sender,EventArgs e) {

string imagem = Request.QueryString["Imagem"].ToString();
string path = Server.MapPath(String.Format("~/{0}", imagem));
int roundedDia = 20;

using(System.Drawing.Image imgin = System.Drawing.Image.FromFile(path)){

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgin.Width, imgin.Height);
Graphics g = Graphics.FromImage(bitmap);

//Define a cor do canto arredondado
Color cor = Color.FromArgb(29, 29, 29);
g.Clear(cor);

Brush brush = new System.Drawing.TextureBrush(imgin);

FillRoundedRectangle(g, new Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush);

// done with drawing dispose graphics object.

g.Dispose();

// Stream Image to client.

Response.Clear();

Response.ContentType = "image/pjpeg";

bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();

// dispose bitmap object.

bitmap.Dispose();
}
}

public static void FillRoundedRectangle(Graphics g,Rectangle r,int d,Brush b){

// anti alias distorts fill so remove it.
System.Drawing.Drawing2D.SmoothingMode mode = g.SmoothingMode ;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.FillPie(b,r.X,r.Y,d,d,180,90);
g.FillPie(b,r.X+r.Width-d,r.Y,d,d,270,90);
g.FillPie(b,r.X ,r.Y+r.Height -d,d,d,90,90);
g.FillPie(b,r.X +r.Width-d,r.Y+r.Height -d,d,d,0,90);
g.FillRectangle(b,r.X + d/2,r.Y,r.Width-d,d/2);
g.FillRectangle(b,r.X ,r.Y +d/2,r.Width,r.Height-d);
g.FillRectangle(b,r.X + d/2,r.Y + r.Height-d/2,r.Width-d,d/2);
g.SmoothingMode = mode;
}

</script>

Página que utiliza a RoundCorner.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
<body>
<form id="form1" runat="server">
<div>
<asp:Image runat="server" ID="img" ImageUrl="~/RoundCorner.aspx?Imagem=imagem.jpg" />
</div>
</form>
</body>
</html>

Créditos: Tiago Bandeira


Select em Stored Procedure

Para não ter que reescrever a lógica existente em uma stored procedure para fazer um simples select, basta inserir o conteudo de uma stored procedure para dentro de uma tabela temporária.

É praticamente como utilizar a stored procedure no lugar da tabela.

Segue abaixo o exemplo:

-- DROP PROCEDURE testProc
-- DROP TABLE #testTable

CREATE PROCEDURE testProc AS
BEGIN
SELECT 'AB' as col1, 'CD' as col2
UNION
SELECT 'EF' as col1, 'GH' as col2
END

GO
CREATE TABLE #testTable (col1 varchar(5), col2 varchar(5))

GO
-- this call will succeed
INSERT INTO #testTable EXEC testProc

GO
SELECT * FROM #testTable where col2 like '%C%'



Gerar imagem com texto

Gerar imagem com texto personalizado.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gerar-imagem.aspx.cs" Inherits="gerar_imagem" %>

<!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>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="Text" />
<br>
<br>
<asp:DropDownList runat="server" ID="BackgroundColor">
<asp:ListItem Value="red">Red</asp:ListItem>
<asp:ListItem Value="green">Green</asp:ListItem>
<asp:ListItem Value="navy">Navy</asp:ListItem>
<asp:ListItem Value="orange">Orange</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="Font">
<asp:ListItem Value="Arial">Arial</asp:ListItem>
<asp:ListItem Value="Verdana">Verdana</asp:ListItem>
<asp:ListItem Value="Courier">Courier</asp:ListItem>
<asp:ListItem Value="Times New Roman">Times New Roman</asp:ListItem>
</asp:DropDownList>
<br>
<br>
<asp:Button runat="Server" ID="SubmitButton" Text="Gerar Imagem" />
</div>
</form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;

public partial class gerar_imagem : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
Bitmap oBitmap = new Bitmap(468, 60);
Graphics oGraphic = Graphics.FromImage(oBitmap);
System.Drawing.Color oColor = new Color();

String sColor = Request["BackgroundColor"];
String sText = Request["Text"];
String sFont = Request["Font"];

switch (sColor)
{
case "red":
oColor = Color.Red;
break;
case "green":
oColor = Color.Green;
break;
case "navy":
oColor = Color.Navy;
break;
case "orange":
oColor = Color.Orange;
break;
default:
oColor = Color.Gray;
break;
}
SolidBrush oBrush = new SolidBrush(oColor);
SolidBrush oBrushWrite = new SolidBrush(Color.White);

oGraphic.FillRectangle(oBrush, 0, 0, 468, 60);
oGraphic.TextRenderingHint = TextRenderingHint.AntiAlias;

Font oFont = new Font(sFont, 13);
PointF oPoint = new PointF(5, 5);

oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint);

oBitmap.Save(Server.MapPath("gen_img.jpg"), ImageFormat.Jpeg);

Response.Write("Imagem gerada <a target=\"_blank\" href=\"gen_img.jpg\">aqui</a>");
}
}
}



Desabilitar botão após o click

Solução muito simples para desabilitar o botão após o click.

Essa solução pode ser utilizado em requisições ajax, com update panel.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" AsyncPostBackTimeout="0" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server" ID="upPrincipal">
<ContentTemplate>
<asp:Button runat="server" ID="btnSalvar" Text="Salvar"
onclick="btnSalvar_Click" />
<asp:Literal runat="server" ID="ltlTexto"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

E no code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnSalvar.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(btnSalvar, String.Empty).ToString());
}
protected void btnSalvar_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
ltlTexto.Text += DateTime.Now.ToString() + " - ";
}
}

terça-feira, 28 de dezembro de 2010

Enviar e-mail de forma assincrona

Um dos maiores problemas ao fazer envio de e-mail é a tempo que demora o seu envio.

Podemos então enviar os e-mails de forma assincrona, sem interromper a execução do programa.

Utiliza-se a classe System.Net.Mail.MailMessage.

public static void SendEmail(System.Net.Mail.MailMessage m) 
{
SendEmail(m, true);
}

public static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{

System.Net.Mail.SmtpClient smtpClient = null;

smtpClient = new System.Net.Mail.SmtpClient("localhost");

if (Async)
{
SendEmailDelegate sd = new SendEmailDelegate(smtpClient.Send);
AsyncCallback cb = new AsyncCallback(SendEmailResponse);
sd.BeginInvoke(m, cb, sd);
}
else
{
smtpClient.Send(m);
}

}

private delegate void SendEmailDelegate(System.Net.Mail.MailMessage m);

private static void SendEmailResponse(IAsyncResult ar)
{
SendEmailDelegate sd = (SendEmailDelegate)(ar.AsyncState);

sd.EndInvoke(ar);
}




domingo, 19 de dezembro de 2010

Configurar D-Link 524

WAN Settings:
- PPPoE: Choose this option if your ISP uses PPPoE. (For most DSL users)

PPP over Ethernet
- Dynamic PPPoE
- Maximum Idle Time: 0
- MTU: 1305

config modem

performance modem