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/