pontoNETpt
A comunidade PontoNetPT está direccionada a todos os programadores que trabalhem com a plataforma .NET.
How to apply current Theme to a CommandField

Facts
I have a gridview in wich the user will do the editing. The GridView has a CommandField with the ButtonType set to Image

The problem
I want the images to be in the Theme Folder, but the CommandField doesn't seem to support this behaviour.

The solution
After searching the web, I realized that other developers had the same problem. Here is my solution to the problem

I've created a class named CommandField that inherits from System.Web.UI.WebControls.CommandField.
In that class I overrided the method Initialize to look like this:

    public override bool Initialize(bool sortingEnabled, System.Web.UI.Control control)

        {

            if (EditImageUrl.StartsWith("^"))

                base.EditImageUrl = "~/App_Themes/" + control.Page.Theme + EditImageUrl.Substring(1);

            if (CancelImageUrl.StartsWith("^"))

                base.CancelImageUrl = "~/App_Themes/" + control.Page.Theme + CancelImageUrl.Substring(1);

            if (UpdateImageUrl.StartsWith("^"))

                base.UpdateImageUrl = "~/App_Themes/" + control.Page.Theme + UpdateImageUrl.Substring(1);

            if (DeleteImageUrl.StartsWith("^"))

                base.DeleteImageUrl = "~/App_Themes/" + control.Page.Theme + DeleteImageUrl.Substring(1);

            return base.Initialize(sortingEnabled, control);

        }

 

 In the GridView you have to specify the URLs like this:
<skt:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true" EditImageUrl="^/Images/GridViewEdit.gif" UpdateImageUrl="^/Images/GridViewUpdate.gif" CancelImageUrl="^/Images/GridViewCancel.gif" DeleteImageUrl="^/Images/GridViewDelete.gif" />

The basic idea is that the ^ character represents the Current Theme in the App_Themes folder. Notice that I used <skt:CommandField and not <asp:CommandField. This is because <skt: is mapped to the namespace where I created my class CommandField.

If you have another solution feel free to comment.


Posted 4-8-2009 15:31 por Daniel Vinagre

Comments

Nuno Gomes wrote re: How to apply current Theme to a CommandField
on 6-8-2009 1:28

Olá Daniel,

embora a solução que apresentas seja muito boa, eu acabo sempre por ter a sensação que não é a melhor. Na minha opinião esta solução levanta dois problemas:

1 - requer o uso de uma sintaxe diferente da sintaxe standard para lidar com Urls ('^')

2 - nada no markup indica claramente que o controlo em questão tem um comportamento diferente do standard, i.e., que as path são relativas ao tema

Para ultrapassar esta questão tenho várias sugestões:

1 - Alterar o nome do novo controlo para reflectir o novo comportamento, por exemplo ThemedCommandField e manter as propriedades tal como estão

2 - ou Criar novas propriedades com um nome apropriado para a função, por exemplo EditThemeImageUrl, e manter ou invalidar as as propriedades da base (override com sealed e throw new NotImplementedException)

Pessoalmente gosto mais da primeira alternativa.

Atenção que apesar de sugerir estas alternativas eu também alguma vezes acabo por seguir a tua sugestão ;)

Emaneule Zambrano wrote re: How to apply current Theme to a CommandField
on 7-8-2009 19:42

Add this code to complete your function:

           if (InsertImageUrl.StartsWith("^"))

               base.InsertImageUrl = "~/App_Themes/" + control.Page.Theme + InsertImageUrl.Substring(1);

           if (NewImageUrl.StartsWith("^"))

               base.NewImageUrl = "~/App_Themes/" + control.Page.Theme + NewImageUrl.Substring(1);

Add a Comment

(requerido)  
(opcional)
(requerido)  
Remember Me?
If you can't read this number refresh your screen
Enter the numbers above:  
Powered by Community Server (Commercial Edition), by Telligent Systems