Make User Control load before page - c#

I have 3 parts to my site, Site Master, User Control (inside Site Master) and General page.
When a user logs in they are redirected to the General Page. The User Control is a dropdownlist of different accounts (Auto select first account) that get's their username and runs stored procedure to pull their information into a SiteID session Variable.
Then in the General page I set all the labels to the users information. with this code.
if (Session["SiteID"] != null)
{
SiteID = int.Parse(Session["SiteID"].ToString());
PopulateAccountData();
PopulateAccountInformation2();
PopulateSiteNodes();
PopulateSiteMap();
}
else
{
LabelSiteName.Text = "No Site Selected";
}
The problem is when the page loads for the first time it does not have the Session["SiteID"], I have to hit refresh for everything to load.
I am new to ASP.net so I'm not sure if I'm doing this right, but how do I get everything to load the first time?

Use : IsPostBack
Look : //www.java-samples.com/showtutorial.php?tutorialid=1083

Related

Ways to avoid user from accessing page with querystring

I have a page that I want it to be accessed when the user finishes saving the form. I have a Response.Redirect("MyPage.aspx?queryStringParam=123") (the value chages, based on field selection) .
On the page I have a check to see if the value is being passed and everything is working:
if (Request.QueryString["queryStringParam"] != null)
{
int queryStringParam;
if(int.TryParse(Request.QueryString["queryStringParam"], out id))
{
hfQueryStringParam.Value = queryStringParam.ToString();
}
else
{
Response.Redirect(ResolveUrl("~/Default.aspx"));
}
}
else
{
Response.Redirect(ResolveUrl("~/Default.aspx"));
}
If the user tryes to type the page, it checks for the queryString and if it's not being passed, it will redirect him to the default page, but hte problem is, if he types the queryStringParam into the url, the page will load. I don't want that to happen.
Is there any way to block the user from accessing the page by typing the url and only access it with Response.Redirect?
From the page where you are saving the form,store something in the session just before the redirection to MyPage.aspx. In the MyPage.aspx first check for the session value. If it does not have a value which you set from the previous page, redirect the user to the default page.

How to get current username information from MySql Database ,C#

I have an application with login form, and I want after the login to show on a label or something , the username and other information from my database with that user.
For example , after you login with username "admin" , to show me on next form a text "Welcome Administrator!" . Or if I login with username "john", to show me on next form a text "Welcome John Snow!" .
How I can do that ?
You'd have to keep track of a session identifier and use that to track a user through a current session and you could then display that on your GUI through whatever language/api you are using in your application. Alternatively if you don't want to or need to keep track of your user past the login page you could just forward that to the class that handles the next page by allowing the next class to inherit the previous.
However it's hard to get much more specific without more details on your project.
Best way is to put the UserName variable in Session and access it in the next page.
Pseudo Code...
In the login page after authentication check write this (assuming 2nd page is Home.aspx):
Session["USER_NAME"] = userName;
Respinse.Redirect("Home.aspx");
In Home.aspx page_load function, write this to fetch the name from session and display, and redirect user to login page if no session value present.
if(Session["USER_NAME"] != null)
{
lblUserName.Text = Session["USER_NAME"].ToString();
}
else
{
Respinse.Redirect("Login.aspx");
}

login and userID toolbar

I looking for a tutorial of how i can make my wesite toobar show the name of the user after he login so he can just press on his name in the toolbar and move him to his personal page such as your user name and your photo on the top of the forums.
Many thanks
It can be done in several different ways, however this is probably the simplest. Save the username in a session variable:
Session["UserName"] = txtUsername.text;
and in all the other pages , you will need to have a Label that shows the name of the user who is currently logged in. You can get the value from the "session" in this way:
labelUser.Text = Session["UserName"].Tostring();
Do not forget to set the session to null when the user logs out:
Session["Username"] = null
How to redirect on his page, just use again the value of session variable to redirect to his page.

Is it possible to send a user to a specific page depending on what account they sign in with?

In the properties tab of the login form, I set the DestinationPageUrl setting to the adminmenu.aspx page, but this will mean that even if a student log's on they will be taken to this page, I am looking for a solution that allows the login form to take the user to whatever page they are allowed to view.
So as an example, the login page shows up, and the user that logs in happens to have a student role, the log in form will then take them to the student.aspx page, and if another person uses the same login form and they have the admin credentials it takes them to the adminmenu.aspx page.
I am having trouble googling for this solution, I was hoping I could get some guidance with this, the only current workaround I have for this would be to have multiple login forms, and each one pointing to a different page.
This is of course using the asp.net website management tool and roles/users ect.
You can add some logic to your login form to redirect based on user role.
protected void Login1_LoggedIn(object sender, EventArgs e)
{
if (Roles.IsUserInRole(Login1.UserName, "Admin"))
{
Response.Redirect("~/Admin/Default.aspx");
}
else if (Roles.IsUserInRole(Login1.UserName, "Student"))
{
Response.Redirect("~/Student/Default.aspx");
}
else
{
Response.Redirect("~/Login.aspx");
}
}
I would add logic to your login page to redirect you based on role. One idea is to pull the user and the role as part of the authentication.
Example code:
if(user.Role == "Student")
{
Response.Redirect("Student.aspx");
}
else if(user.Role == "Admin")
{
Response.Redirect("Admin.aspx");
}
You can use Role-Management of asp.net.
You can get good example on msdn for the same.
Here is a good link http://www.asp.net/web-forms/tutorials/security/roles/creating-and-managing-roles-cs

How to go back to previous page and preserve the previous page data in c#

I am working on a Retail Website which is developed in Asp.net 3.0. I have a cart system so when user add some items to their cart they are being redirect to Basket page. And i have a button on Basket page for them to go back add some more items. i am not sure how to do that.
here is the case.
Home Page -> Mens Clothing Product List -> Jeans Page -> Basket pgae
Now when users go to Mens Clothing they see all the items, then they select one Jeans they want to buy and they are redirected to Jeans Page. Now they add items to their basket and they are redirected to Basket Page, Now i have a button on Basket page called "Keep Shopping", When they click (as of now they are redirected to Home page) BUT i need them to redirect to Mens Clothing Product List instead of Home Page.
CASE 1
I have googled it and found out that we can use URLReferrer and check it if it is null or not. BUT in my case it is always Basket Page URL as i am using Response.Redirect on Jeans Page. So i can not use URLReferrer.
CASE 2
I have also found out that using Javascript History we can go back to 1 or 2 page. BUT as i want to preserve the user data and CART i can not use that as well because its gonna use the browser cache and will not save any users data.
CASE 3
I was thinking of using the Context Or Session to store the current page URL on Jeans page and i can check it in Basket Page, if it not null then redirect to that page. BUT i want to go back one more step ie. Mens Clothing page not Jeans Page.
I am really not sure whether this will be a good idea or not. Please suggest.
Thanks...
Use Response.Redirect to go the required page and use session store the Basketdata.
For redirecting use:
protected void btn_click(object sender, EventArgs e)
{
Response.Redirect("desired page.aspx");
}
For storing use dataset or datatable and store it in ssession.
Session["items"] = ur_DataTable as DataTable;
or
Session["items"] = ur_DataSet as DataSet;
ur_Datatable or ur_DataSet will contain the product which you have added.
To render the another page and save its data to access in current page ,do as below
protected void button_Click(Object sender,EventArgs e )
{
session["give your prefered Name to session"]=your value.its whatever
response.redirect("your Page URL to Redirect from this page to that page");
}
Now access the value that you have stored in session on another page. Do as below
bye default value return by session are of "Object" type, so you can convert in whatever data type using explicit conversion
Here I am converting object value in string,
protected void Page_Load(object sender, EventArgs e)
{
string value=session["Use Same Name as u have given to it"].ToString();
}
One approach to simple state management in asp.net is to use a hidden field.
Use a hidden field to store and pass a location to the basket page.
Now when users go to "Mens Clothing" they see all the items, then they select one "Jeans" they want to buy and they are redirected to "Jeans" Page. Now they add items to their basket and they are redirected to "Basket" Page.
Now I have a button on "Basket" page called "Keep Shopping". When they click I need to redirect them to Mens Clothing Product List instead of Home Page.
Now you say you want to redirect to the "Mens Clothing" page, but you have to determine why you want to redirect to that page. If it always is that page, then hardcode that:
Response.Redirect("Shop/Categories/Mens-Clothing");
Though when the page you want to return to depends on what item was added to the basket, you need to realize what page you want to redirect to and how the relation of that page is with regard to the item.
The Basket page knows what item is being added to the basket. If you for example want to redirect to the category page for that item, look up the category this item is in, and redirect to that page:
Response.Redirect("Shop/Categories/" + item.Category.Name);
Although this will redirect you to the "Jeans" page, because that is the choosen item's category.
Now it looks like you want to redirect to the parent category of the category the item is in. Then you user that:
Response.Redirect("Shop/Categories/" + item.Category.ParentCategory.Name);
Or whatever your data model looks like.

Categories