Set master page file using function - c#

I want to change masterpagefile using another class function
Example:
public class XYZClass
{
public void CheckLogin(object ses,bool ipb,ref MasterPage page)
{
if (!(ses == null))
{
if (ses.ToString() == "Admin")
page.MasterPageFile = "~/Admin.master";
else
page.MasterPageFile = "~/MasterPage.master";
}
else
{
Response.Redirect("~/frmLogin.aspx");
}
}
}
public partial class frmDoctorHistory : System.Web.UI.Page
{
BLLcheckLogin checkLogin = null;
protected void Page_PreInit(object sender, EventArgs e)
{
checkLogin = new BLLcheckLogin();
checkLogin.CheckLogin(Session["usertype"], IsPostBack, ref MasterPageFile);
}
}
and I call this function from another aspx source file, and it gives me an error like
"A property, indexer or dynamic member access may not be passed as an out or ref parameter".

You can set the Master Page no later than in the PreInit event.
See the code sample from MSDN as a reference.
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
You have to rewrite your current code as:
public void CheckLogin(object ses,bool ipb, Page page)
{
if (!(ses == null))
{
if (ses.ToString() == "Admin")
page.MasterPageFile = "~/Admin.master";
else
page.MasterPageFile = "~/MasterPage.master";
}
else
{
Response.Redirect("~/frmLogin.aspx");
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
checkLogin = new BLLcheckLogin();
MasterPage mp;
checkLogin.CheckLogin(Session["usertype"], IsPostBack, this);
}
I altered your code to pass in the Page rather than the MasterPageFile property. Ref isn't necessary any more then.

Related

Accessing a variable from a different code-behind class

EDIT: made variables not-static.
I have a variable in code-behind class and want to access it from another.
Here's the C# code for the "Signup" page:
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
Response.Redirect("Login.aspx");
}
}
}
And here's the code for another code-behind class in an .aspx class "Login":
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
if (loguser == Webform_signup.User)
{
Response.Redirect("Start");
}
}
}
}
The problem is that when I try to access Webform_signup.user, it displays an error saying:
The name Webform_Signup does not exist in the current context.
Anyone has an idea of how to fix it?
The dirt simplest way (and probably not the best way) is to just stick it in a session variable and read it from there instead.
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
HttpContext.Current.Session["user"] = user; //Save to session
Response.Redirect("Login.aspx");
}
}
}
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
var user = HttpContext.Current.Session["user"]; //Read from session
if (loguser == user)
{
Response.Redirect("Start");
}
}
}
}
This is assuming of course that you have session state enabled.
You need to keep in mind lifecycles. You're ignoring them, and the results would be disastrous in this case.
Any time you declare a variable as static, there will only be one instance of that variable in your application. That means no matter what user is using your app, the Webform_Signup.User field will be shared. That could result in users accessing other users' information! That's really, really bad.
Also, page classes should not be referring to other page classes. A page only exists when a user is accessing that page.
If you want to login a user, then perhaps you should research Forms Authentication. Or better yet, learn a modern framework like ASP.NET Core MVC instead of Web Forms, which is a deadend platform.

How to set ASP.NET session variable?

I am new to ASP.NET and I am trying to set session variable. I have one form (SelectPlayer.aspx) where I am trying to set the session but when I try to see the result on second page it does not show me any value. Below is my code.
SelectPlayer.aspx
public partial class SelectPlayer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
lblSelectPlayer.Text = "Select Player 1";
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
Session["player1"] = "PlayerSession";
Response.Redirect("Score.aspx");
}
}
Score.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
Response.Redirect("SelectPlayer.aspx");
}
}
Change Response.Redirect("Score.aspx"); to Response.Redirect("Score.aspx", true);
The second parameter of a redirect indicates that you want to end the response. I've found in the past that setting a Session directly before doing a redirect without this second parameter can cause issues.

Get Public Content Page Variable into Masterpage

I have a public variable in my content page that I need to access in my MasterPage. So I can set a Javascript variable... .
How can I reference a public content page variable from the masterpage?.
I supposed you want to say that you want to access to variable in the MasterPage from Contend Page, if is correct, use this example:
Declare your public or protected variable:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string strEmpresa = "NS";
protected void Page_Load(object sender, EventArgs e)
{
}
}
Set the following directive at the beginning of your content page:
<%# MasterType virtualPath="~/MasterPage.Master"%>
then you can use the public variables of your MasterPage, using Master.NameVariable.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = Master.strEmpresa;
}
}
In other case if you really want access to variable in ContentPage from MasterPage, you just can set the value in Session and then read in MasterPage. For example:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myVariable"] != null)
{
TextBox1.Text = Session["myVariable"].ToString();
}
}
}
}
public partial class WebFormMP_TestPublicVariable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["myVariable"] = "Test";
}
}
}
There are many ways that you can achieve this. check around internet ;).

how to call customized web form in asp.net

in windows application , when i want to call a customized form , i ueses this method :
result frm = new result();
frm.firstparameter = "first parameter";
frm.secondparameter = "second parameter";
frm.showdialog();
but in web application , i do not know how to handle it .
here is my webapplication source code :
in WebForm1.aspx.cs :
protected void sumbitbtn_Click(object sender, EventArgs e)
{
result frm = new result();
frm.firstparameter = "firstparameter";
frm.secondparameter = "secondparameter";
// frm.showpage() ???
// Response.Redirect("~/result.aspx");
}
in result.aspx.cs :
public partial class result : System.Web.UI.Page
{
private string Firstparameter = string.Empty;
public string firstparameter
{
get{return Firstparameter;}
set { Firstparameter = value; }
}
private string Secondtparameter = string.Empty;
public string secondparameter
{
get{return Secondparameter;}
set { Secondparameter = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
firstlbl.Text=Firstparameter;
secondlbl.Text=Secondtparameter;
}
}
ASP.NET does not work that way. If you want to open one page after the other - you need to send a redirect request to do so. Of course you can also open popup, or make server transfer, but in your case redirect seems to be the best thing to do. Also note that parameters are not just passed into constructor or property - you need to attach them to the request, for example with query string.
So, in WebForm1.aspx.cs:
protected void sumbitbtn_Click(object sender, EventArgs e)
{
string url = string.Format("~/result.aspx?fp={0}&sp={1}", "firstparameter", "secondparameter");
Response.Redirect(url);
}
In result.aspx.cs :
public partial class result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
firstlbl.Text = Request["fp"];
secondlbl.Text = Request["sp"];
}
}
Note that this code leaves a lot of things out (parameter null handling for one), it just shows the point.

Preserve value of textbox using View State in Asp.net?

I've a button and a textbox. I want a value to be entered in textbox and when I click on button the page will reload but the value should still be in the textbox. How can I do that. The following code doesn't work
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = ViewState["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}
Response.Redirect does what it says - redirects the request to a NEW page. ViewState won't get applied, ever. If you need a redirection, consider using session instead.
If you don't need a redirection, simply don't redirect and update only parts of the page that need to be updated.
Viewstate can retain the value only till when you are on the same page. You are redirecting to other page. So instead of using viewstate use session.
asp.net webform have already maintained the viewstate on page refresh . don't need any code for handle this operation .
See this : http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_aspnetviewstate
referred from : http://www.w3schools.com/aspnet/aspnet_viewstate.asp
and see this discussion
try this
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}
Since you are redirecting to a new VIEW so VIEWSTATE will not be of any HELP. SO,Use Session
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}

Categories

Resources