Friday, 25 July 2014

Dynamically Change Page/Master Page background Image in Asp.Net Using C#

7/25/2014 - By Pranav Singh 1

This article will show you how you can how you can change the page background dynamically on the bases of role. In this article I will show you how you can change the background image of the page on selection of role from dropdown list. In this I have placed the dropdown list on masterpage of the project.

So for this article first we will create a new asp.net application and add a dropdown list with curtain role. Now here is the master page code shown below.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="PageBackground.SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title> Dynamically Change Page/Master Page background Image in Asp.Net Using C#
</title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body style="background-image: url(<%= backgroundImage %>)">
    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>
                    My ASP.NET Application
                </h1>
            </div>
            <div class="loginDisplay">
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Value="1">Admin</asp:ListItem>
                    <asp:ListItem Value="2">User</asp:ListItem>
                </asp:DropDownList>
            </div>
            <div class="clear hideSkiplink">
            </div>
        </div>
        <div>
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
       
    </div>
    </form>
</body>
</html>

Now just check the body tag of the above code. In this tag I have added background-image with a public variable whose value we change on the dropdown list index change.

<body style="background-image: url(<%= backgroundImage %>)">

So here is the code of master page.

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

namespace PageBackground
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public string backgroundImage = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "1")
            {
                backgroundImage = "image/Chrysanthemum.jpg";
            }
            else
            {
                backgroundImage = "image/Desert.jpg";
            }
        }
    }
}

Now run the application to check the output.

OUTPUT 1



OUTPUT 2



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

  1. this way of doing it makes 5 warnings to appear. a better way is making a method in master code (Cs file) and calling this method from childs.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top