Wednesday, March 28, 2012

Change Themes using Ajax

Hi,

I am changing themes dynamically when i am clicking image button. i write a code to change the theme at Page_PreInit().

I need to reload the page to take effect. so i used response.redirect(SamePage.aspx). hope response.redirect is not supported on update panel.

i need to change themes using Ajax( without postback).

Thanks in Advance.

I don't think you can change the theme directly using ajax; but what you can do is change the href on your link tag(s) to give you new stylesheets.


Hi,

I think you dont need to Redirect to the same page to change the theme.

Just put

if(Page.IsPostBack)

GridView1.SkinID ="newskinID" in the Page.PreInit event.

Thanks,

Regards

Aruna


Hi,

My previous reply was to change the Skin of a particular control.

If you want to change the theme u can put this code

if (Page.IsPostBack)

this.Page.Theme ="NewSkinFile"

in the Page.PreInit Event Handler

Regards,

Aruna


Aruna,

Thanks for your response.

This is my code.

protectedvoid Page_PreInit(object sender,EventArgs e)

{

if (Request.Cookies["theme"] !=null)

{

this.Theme = Request.Cookies["theme"].Value.ToString();

}

else

{

this.Theme ="Black";

}

}

protectedvoid ImgGreen_Click(object sender,ImageClickEventArgs e)

{

HttpCookie cook=newHttpCookie("theme");

cook.Value=((ImageButton)sender).CommandName;

cook.Expires =DateTime.MaxValue;

Response.AppendCookie(cook);

string[] path = Request.Path.Split(char.Parse("/"));

Response.Redirect(path[path.Length-1].ToString() );

}

when i click image button, i stored theme name in a cookie. based on a cookie i am loading theme.

Page_PreInit() method never fire after image button click until the page reload ( Page_PreInit() will fire first).

so, i redirected to same page.

It is immediately taking theme.

Actually my problem is the page is get postback even i am using update panel.

you got my point.


Paul,

Thanks.

I referred two sites. Live.com and msn india. they are changing page themes without postback. so, i tried.

If you will get resource, post here.

once again thanks paul.


Hi Nanda,

Have u set the commandName of Image Button to the name of the Theme, as you mentioned cook.Value=((ImageButton)sender).CommandName you are trying to put the name of Command in the cookie.

Regards,

Aruna


Aruna,

I am using 4 themes in my project. so, i put 4 imagebutton in my page. all 4 buttons are sharing one methodImgGreen_Click(). so, based on the command name, i am trapping which button is clicking by the user ( i assigned theme name as command name for all four button.)

Thanks.


As I said before, you can't do it the way you're trying to do it. The ASP.Net 'theme' is a pretty rigid thing, and it's set during the Init cycle, which does not fire if you're using and updatepanel (see the documentation on page lifecycle).

If you want to change the look and feel of your site without postback, the way to do it is to build a CSS-based design, and then swap stylesheets from the client.


Dear,

You have redirected to the same page that's y ur page is posted back.

The major problem in your code is PreInit Fires before the button click event, so u cant modify the theme on click of the button by using this code as preinit fires first so it takes the theme of previous click.


Hi,

Hope u have solved ur problem.

If not then let me know I have a solution of ur problem.

Regards,

Aruna


Is that it ? Is CSS-based design the only way to achieve themes without postbacks ?

Aruna, do you have an alternate solution ?

Thanks

Jai

No comments:

Post a Comment