I think my tittle is inaccurate.
When a user clicks on a button I need it to do this:
Response.Redirect("Login.aspx?userid=XX");
How can I get the "userid?" from ?userid. so I can show a page. Like doing "?page=3" and show page 3, in the same page or something.
The Button code is: (just if you need it)
protected void LoginButton_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
Thanks a lot! Sorry if I didn't ask it good, and sorry for the bad English.
Use Request.QueryString:
First Page Sends them another page with their user id in the url:
Response.Redirect("AfterLogIn.aspx?userid=23");
You then Read it using the below code:
var g = Request.QueryString["userid"] //this value should be 23 now
You could then use this g variable to do any amount of custom things (Hide panels, show controls, etc.)
You can do something like this
protected void LoginButton_Click(object sender, EventArgs e)
{
var id = // whatever userid
Response.Redirect("Login.aspx?userid="+ id);
}
and in the pageload of Login page
var userid = Request.QueryString["userid"];
ASP.NET State Management will explain further.
Hope this helps
Related
I made few link buttons to one of my web forms to link to another web for that has multiview with five views. If the user for example clicked on the link the the word "Toys". This should automatically direct the user to the multi view in another form that is associated with the Toys and the same thing can be with other categories. How do I achieve this since I know I can link multi views in a web form but I ignore how to link a link button to a multi view in another web form! Any help please....
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("MoreDetails.aspx?MultiView1.ActiveViewIndex = 1");
}
This code is not working and doesn't show anything! :(
That's not how query strings work. Here is a good help page for learning querystrings.
Querystrings
You'd have to do something more like this:
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("MoreDetails.aspx?view=1");
}
Then on your MoreDetails page:
protected void Page_Load(object sender, EventArgs e)
{
var qs = Request.Querystring["view"];
if(qs != null)
{
if(qs == "1")
{
MultiView1.ActiveViewIndex = 1;
}
}
}
I'm wondering what is my issue on passing a variable from page to page using asp.net session.
I've stripped the code down to just one text box to see whats going on. I'm just trying to take the value of a text box and display it on a confirmation page. When the button is clicked it transfers me to the second page but there label is blank. Yes my post back url is pointing to the second page.
Here is the button click:
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
}
Here is the page load of the second page:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = (string)(Session["name"]);
}
Unless I've been looking at this to long and missed something. I've already read "How to: Read Values from Session State" from MSDN.
You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:
Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.
This should work for ya.
And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
Response.Redirect("PageTwo.aspx");
}
Then in the second page (You don't REALLY need the ToString()):
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
lblName.Text = Session["name"].ToString();
}
}
EDIT -- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.
<asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />
The code you have posted looks fine, so your problem is probably with setup.
Check this link ASP.NET Session State Overview and pay particular attention to the sections on Cookieless SessionIDs and Configuring Session State.
I don't think you added the session. This is how I have done mine.
First Page
protected void btnView_Click(object sender, EventArgs e)
{
foreach (ListItem li in lbxCheckDates.Items)
{
if (li.Selected == true)
{
lblMessage.Text = "";
string checkDate = lbxCheckDates.SelectedItem.Text;
Session.Add("CheckDates", checkDate);
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('Paystub.aspx','_newtab');", true);
}
}
}
Second Page
protected void Page_Load(object sender, EventArgs e)
{
string checkDate = (string)(Session["CheckDates"]);
//I use checkDate in sql to populate a report viewer
}
So with yours, I think you need..
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session.Add("Name", name);
}
I think what you have in the second page should work, but if it doesn't, add ToString() to it like..
lblName.Text = (string)(Session["name"]).ToString();
Let me know if this helps!
Are you doing a redirect after setting the session variable on the first page, if so you it will not work correctly (unless you know the trick). Checkout this article on making it work. Basically, the way to make this work is to the overload redirect method.
Response.Redirect("~/newpage.aspx", false);
The false parameter prevents .net from terminate processing on the existing page (that actually writes out the session state)
For Second Page
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value"] != null)
{
Label1.Text = Session["value"].ToString();
}
else
{
Label1.Text = "Sorry,Please enter the value ";
}
}
You can use Server.Transfer() instead of Response.Redirect()
For first page, use this:
protected void Button1_Click(object sender, EventArgs e)
{
string value = TextBox1.Text;
Session["value"] = value;
Response.Redirect("~/Sessionpage.aspx");
}
i use asp login control in web application (ASP.NET 4). if user in admin role i want redirect to admin page.
i use this code, but not working:
protected void baseLogin1_LoggingIn(object sender, LoginCancelEventArgs e)
{
if (Page.User.Identity.IsAuthenticated && Roles.IsUserInRole(Page.User.Identity.Name, "Admin"))
{
Page.Response.Redirect("admin/Default.aspx");
}
}
please help me.
LoggingInEvent is raised before the user is authenticated. So the first part of your condition is always false. Move you logics under LoggedIn event.
Try this one:
protected void baseLogin1_LoggedIn(object sender, EventArgs e)
{
if (Context.User.Identity.IsAuthenticated && Context.User.IsInRole("Admin"))
{
Context.Response.Redirect("admin/Default.aspx");
}
}
Use the LoggedIn event: Event description is here
You should really be using Server.Transfer("~/admin/Default.aspx"); as it is a little more efficient (less round trips).
If the page needs to preserve the query string for a bookmark or it is important to preseve the correct URL in the browser then Response.Redirect() is needed but be aware of the extra bandwidth cost.
I am working on an aspx page in VS 2005. I have code like this,
int RepID = 0;
protected void Page_Load(object sender, Eventargs e)
{
if(!Page.Ispostback)
{
get value from database and display it in textbox;
}
else
{
}
}
protected void Save_OnClick(object sender, Eventargs e)
{
Update Database with modified textbox Text ;
Response.Redirect(//To the same page);
}
After the Response.Redirect, i was looking for the page to refresh and get the modified value from database. Instead, it uses the else loop in Page_load and displays the old value because it doesn't go into the if loop as it is the Postback. How can i display from database after the response.redirect is used. I know i am missing a logic but i am not sure what? Thanks alott guys..
Delete
Response.Redirect(//to same page);
Your Save button click should post back to the same page it is on.
I am trying to add a condition for a hyperlink that I have in my page.
Instead of just using a particular link like: Tutorial I want to display different pages for different users. For example, if the user is logged in as Admin, they will be presented with different link than regular users.
I have modified my hyperlink as: <a onclick="displayTutorial_Click">Tutorial</a>
and added this code:
protected void displayTutorial_Click(object sender, EventArgs e)
{
// figure out user information
userinfo = (UserInfo)Session["UserInfo"];
if (userinfo.user == "Admin")
System.Diagnostics.Process.Start("help/AdminTutorial.html");
else
System.Diagnostics.Process.Start("help/UserTutorial.html");
}
But this didn't work. Can anyone please help me to figure out how I can make the Tutorial link work properly? Thank you a lot in advance!!!
The onclick attribute on your anchor tag is going to call a client-side function. (This is what you would use if you wanted to call a javascript function when the link is clicked.)
What you want is a server-side control, like the LinkButton:
<asp:LinkButton ID="lnkTutorial" runat="server" Text="Tutorial" OnClick="displayTutorial_Click"/>
This has an OnClick attribute that will call the method in your code behind.
Looking further into your code, it looks like you're just trying to open a different tutorial based on access level of the user. You don't need an event handler for this at all. A far better approach would be to just set the end point of your LinkButton control in the code behind.
protected void Page_Load(object sender, EventArgs e)
{
userinfo = (UserInfo)Session["UserInfo"];
if (userinfo.user == "Admin")
{
lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
}
else
{
lnkTutorial.PostBackUrl = "help/UserTutorial.html";
}
}
Really, it would be best to check that you actually have a user first.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserInfo"] != null && ((UserInfo)Session["UserInfo"]).user == "Admin")
{
lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
}
else
{
lnkTutorial.PostBackUrl = "help/UserTutorial.html";
}
}
Wow, you have a huge misunderstanding how asp.net works.
This line of code
System.Diagnostics.Process.Start("help/AdminTutorial.html");
Will not redirect a admin user to a new site, but start a new process on the server (usually a browser, IE) and load the site. That is for sure not what you want.
A very easy solution would be to change the href attribute of the link in you page_load method.
Your aspx code:
Tutorial
Your codebehind / cs code of page_load:
...
if (userinfo.user == "Admin")
{
myLink.Attributes["href"] = "help/AdminTutorial.html";
}
else
{
myLink.Attributes["href"] = "help/otherSite.html";
}
...
Don't forget to check the Admin rights again on "AdminTutorial.html" to "prevent" hacking.
this may help you.
In .cs page,
//Declare a string
public string usertypeurl = "";
//check who is the user
//place your code to check who is the user
//if it is admin
usertypeurl = "help/AdminTutorial.html";
//if it is other
usertypeurl = "help/UserTutorial.html";
In .aspx age pass this variabe
<a href='<%=usertypeurl%>'>Tutorial</a>