I have an asp page employeeHome.aspx and I have two master pages adminMasterPage.master for Admin login and userMasterPage.master for normal user login as I have 2 types of user login one as Admin and other normal user. And I want to set adminMasterPage.master as a master page for employeeHome.aspx in case of normal user login (just for this page).
How can I do this?
Put your code to change the master page in Page_PreInit event.
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MasterPage.master";
}
Put the code at employeeHome.aspx page
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["empCode"] != null)
{
if (Session["empCode"].ToString() != "0")
{
this.MasterPageFile = Server.MapPath("adminMasterPage.master");
}
}
}
You can change that by having the required master page file specified in the PreInit event, which is a part of the page life cycle..
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "adminMasterPage.master"; //For Admin
//this.MasterPageFile = "userMasterPage.master"; - For Normal User
}
This worked this way and here what I did as suggest by #Iswanto San and made changes in the path.
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["IsEmporAdm"].ToString() == "0")
{
this.MasterPageFile = "~/StyleLibrary\\layout\\AdminMaster.Master";
}
else
{
this.MasterPageFile = "~/StyleLibrary\\layout\\UserMaster.Master";
}
}
Related
I have a MasterPage, and a Content page.
Whenever a method is called on the content page that does a redirect, EG:
protected void DoLogin(object sender, EventArgs e)
{
var result = AttemptLogin();
if (result.Success)
{
Response.Redirect("~/home");
}
else
{
... show error message
}
}
It appears Page_LoadComplete is not fired. The master page is set up as:
protected void Page_Init(object sender, EventArgs e)
{
Page.LoadComplete += Page_LoadComplete;
}
void Page_LoadComplete(object sender, EventArgs e)
{
DoSomething();
}
Should a redirect be stopping this from firing?
Never needed to do this before but is it possible to dynamically set/change which master page a page is using? Have an old asp.net web forms project which I have created a new bootstrap template for but the boss wants to give people the opportunity to switch on the new one instead of forcing it upon them.
I would recommend you to create a BasePage class than write this method in that class and inherit all of your pages from this class whose master page can be changed dynamically.
public class BasePage: System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
}
And then in your page inherit page from BasePage like this
public partial class _Default:BasePage
The master page is changed only in preint event
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
or
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
page.MasterPageFile = "string location of masterpage";
}
I want to update my label content when the user submits a form but it doesn't get updated. Although I have put it in if (!IsPostBack) condition in form load It doesn't show the changes. The only solution that I came up with was defining a counter and increase it in button_click event and check it before label update in !IsPostBack condition. Which is working fine with that.
Is there any other way to update the label text?
Here is my solution:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (count > 0)
lblSuccessMsg.Text ="A Message!";
count = 0;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Save();
count = count + 1;
}
please update your label code outside of !IsPostBack evnt and inside a pageload.
Hard to tell but by the sounds of it you have a submit button and the onclick needs to update this label, something like this should work. I'm using viewstate but session would work here, as would a redirect to the same page with a querystring parameter. Not sure if I've understood your question correctly though.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if(Viewstate["updateLabel"] == "true")
{
lblYourLabel.Text = "I'm updated now!";
Viewstate["updateLabel"] = "";
}
}
}
protected void btnYourButton_Click(Object sender, Eventargs e)
{
ViewState["updateLabel"] = "true";
//Do other stuff here if you want
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache );
Response.Cache.SetAllowResponseInBrowserHistory(false);
}
This code i have used to authenticate the website,its working but only when the user copies the url and paste it in a new window,but opening in the new window leads to the next page without any login credentials.
For authentication:
In page_Load Events
if (session(user_id) != null) {
response.redirect("the_page_which_you_want_to_go.aspx");
} else {
response.redirect("Login.aspx");
}
i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}