Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
hi how to redirect to particular page.if admin password and id matches it should redirect to admin masters particular page or if User login password and id matches it should redirect to User masters particular page.?can any one help..and admin only can access all pages and..user only can access user pages only
Posted
Updated 6-Feb-12 18:06pm
v2

Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage, as in the following example:
C#
void Page_PreInit(Object sender, EventArgs e)
{
    this.MasterPageFile = "~/NewMaster.master";
}


Extracted from here :
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx[^]

So you can set a master page name in a session variable to let every page know to which master page they should be assigned.

You have to change that session variable based on user access type.

Hope it helps.
 
Share this answer
 
 
Share this answer
 
You can not navigate a Master page. Add a content page and navigate when Session["yourKey"] is null
 
Share this answer
 
Comments
ythisbug 7-Feb-12 0:02am    
ya sorry..i mean to say..in master page contains 3 forms think 1.aspx,2.aspx,3.aspx..and when admin logins i want to redirect to admins 1.aspx page...??
I guess you have worded the question incorrectly. You cannot redirect to a master page, or even request for it for that matter.
I think your question is about how to change the master page at runtime.. If that is the question, it can be done in Page_PreInit event life cycle of the page.

Define the master page in the content page’s Page_PreInit method. Page_PreInit is the last opportunity you have to override the default master page setting, because later handlers (such as Page_Init) reference the master page. For example, the following code defines the master page based on the Session object.

C#
void Page_PreInit(Object sender, EventArgs e)
{
if (Session["masterpage"] != null)
MasterPageFile = (String)Session["masterpage"];
}


The MasterPageFile property can be set with the path of the master page.
 
Share this answer
 
v2
Comments
ythisbug 7-Feb-12 0:03am    
ya sorry..i mean to say..in master page contains 3 forms think 1.aspx,2.aspx,3.aspx..and when admin logins i want to redirect to admins 1.aspx page...??
Abey Thomas 7-Feb-12 0:09am    
yes fahadsheikhji.. I though so.. All you have to set the MasterPageFile property of the page to the path/name of the Master file you want to change. It has to be done in the Page_PreInit handler. Hope this helps.
ythisbug 7-Feb-12 0:18am    
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());
con.Open();
SqlCommand com = new SqlCommand("select * from tblLogin where Username = '" + txtUserName.Text + "' and Password ='" +txtPassword.Text+ con);
SqlDataReader r = com.ExecuteReader();
//r = com.ExecuteReader();
if (r.HasRows)
{
bool isAdmin = false;
while (r.Read())
{
if (Boolean.Parse(r[10].ToString()))
{
isAdmin = Boolean.Parse(r[9].ToString());
Session["LID"] = Int32.Parse(r[0].ToString());
break;
}

}

if (isAdmin == true)
{
//FormsAuthentication.RedirectFromLoginPage("true");
Session["Admin"] = txtUserName.Text.ToString();
Session["Tester"] = txtUserName.Text.ToString();

Response.Redirect("Admin/AdminTestCases.aspx");
}
else
{
//FormsAuthentication.RedirectFromLoginPage(txtUserName.Text);
Session["Tester"] = txtUserName.Text.ToString();
Response.Redirect("Tester/TesterWorkStatus.aspx");
}
}
else
{
Label1.Visible = true;
}
con.Close();
}


this is my code but m getting error like "ExecuteReader: Connection property has not been initialized." can u help..abyey thomas??
Abey Thomas 7-Feb-12 0:36am    
First of all, I cannot help you with this code dump. My answer was very specific to your question as to how to change the master page file dynamically (if at all that was the question), and that as explained can be done in the Pre_Init event of the page (and that alone can be done) - What I again assumed that, you will know where to put the code. It has to be put in the page you requested from a previous page. I can see a lot of issues with the code you dumped over here (for instance opening of an SQL connection in a page - did you put this code in Pre_init?, and then have you ever heard of SQL injection attacks and how to avoid it?), and I am not going into more specifics here. And I am not sure what you are trying to achieve - sorry.
ythisbug 7-Feb-12 1:34am    
ok thanks abey thomas..
Hi,
You can using Database for this one.If you are creating one table contains username and password and take another column i.e flag.You can enter the fields in database and retrieve it.If user page means its value is 0 and admin page means its value is 1.Using this table write the code in your page and in output enter the username and password that can be retrieved from database.So if the user value is 0 it redirects to userpage and 1 its redirects to admin page.
 
Share this answer
 
Comments
ythisbug 7-Feb-12 0:07am    
ya m using database..UserName,Password,Role
ythisbug 7-Feb-12 0:08am    
in role column i gave true fr admin and flse for user
Hi,
I am using cs class for functions and used in my pages.
C#
int roleValue;
            UserLogin user = new UserLogin();
            user.UserName = txtuname.Text;
            user.PassWord = txtpwd.Text;
            Session["username"] = txtuname.Text;
                      Session["PassWord"] = txtpwd.Text;
            roleValue=Convert.ToInt32(UserLogin.LoginDetailsFromDb(user.UserName, user.PassWord));
            if (roleValue == 0)
            {
                Response.Redirect("userpage.aspx");
                

            }
            else if (roleValue == 1)
            {
                Response.Redirect("adminpage.aspx");
            }
            else if (roleValue == -1)   //Exception case
            {
                lbltext.Text = "Exception Occured in Method. Unable to process the method";
            }
            else
            {
                lbltext.Text="The username or password you entered is incorrect.Please Enter the Valid Username";
            }
Login details function :
 public static int LoginDetailsFromDb(string uname, string pwd)
        {
            int role = 0;
            try
            {
                string query;
                
                UserLogin login = new UserLogin();
                 string connection = System.Configuration.ConfigurationManager.ConnectionStrings["QuestionnaireConnection"].ConnectionString;
            
                using (SqlConnection con = new SqlConnection(connection))
                {
                    con.Open();
                    query = "select Role from Questionnaire_UserLogin where UserName='" + uname + "' and PassWord='" + pwd + "'";
                    SqlCommand cmd = new SqlCommand(query, con);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        role = Convert.ToInt32(dr[0]);
                        login.Role = role;

                    }
                    else
                    {
                        role = 2;
                        login.Role = role;
                    }
                    con.Close();
                }
            }
return role;
}
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 7-Feb-12 1:54am    
Added pre tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900