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;
}
}
}
Related
I have a customized authentication on the system, what I would like is for certain pages (ex. EditingProfile.aspx) to not be accessible through URL navigation.
if a user is authorized: buttons (ex. btnEditingProfile) is enabled and would redirect to EditingProfile.aspx.
is a user isn't authorized: buttons (btnEditingProfile) would be disable. I'm looking for a way to prevent users from accessing EditingProfile.aspx through URL navigation.
The system I'm working on is old so I need a safe way to secure it that wouldn't interfere with other aspects of the system.
MainMenu.aspx
protected void Page_Load(object sender, EventArgs e)
{
//CODE
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
//MORE CODE
}
protected void btnEditProfile_Click(object sender, EventArgs e)
{
queryStrings=SOMEVALUE();
string QueryString = QueryStringEncrypter.GetEncryptedQueryString(queryStrings);
Response.Redirect("ProfileDetails.aspx?" + QueryString, false);
}
In my opinion, each user should has an login session to know their permission.
https://www.codeproject.com/Articles/21474/Easy-way-to-create-secure-ASP-NET-login-using-Sess
Following Tim Nguyen's suggestion I did the following:
in MainMenue.aspx I added
protected void Page_Load(object sender, EventArgs e)
{
//code
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
Session["url"] = Request.UrlReferrer.AbsoluteUri.ToString();
//more code
}
in EditingProfile.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["url"] != null)
{
Response.Redirect(Session["url"].ToString());
}
//code
}
nothing else was changed. The code is running the way I want it to so I thought id update this question in case someone need it.
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 am trying to make a web browser with multiple tabs. But now, I have a problem with the DocumentTitle for the name of the tabs.
The problem here is that the code to name the tab is performed before it loads the page. I tried to find a way to perform it after, but it doesn't work.
For example:
private void stackoverflowToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser) tabControl1.SelectedTab.Controls[0]) .Navigate("Http://www.stackoverflow.com/");
Browser_Navigated(null, null);
}
void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tabControl1.SelectedTab.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentTitle;
}
The WebBrowser class exposes a DocumentTitleChanged event that you can use to update the tab title.
I have a table with all the objects I have in my db. I load them in my Page_Load function. I have a text field and a button that when clicking the button, I want the handler of that click to put a new object with the name written in the text field in the db.
Now, I want that what happens after the click is that the page loads again with the new item in the table. The problem is that the button event handler is run after the Page_Load function.
A solution to this would be to use IsPostBack in the Page_Load or use the pre load function. A problem is that if I would have 3 different buttons, I would have to differ between them there instead of having 3 different convenient functions.
Any solutions that don't have this problem?
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userId"] == null)
Response.Redirect("Login.aspx");
// LOAD DATA FROM DB
}
protected void CreateObject(object sender, EventArgs e)
{
// SAVE THE NEW OBJECT
}
Maybe you should try loading your data during PreRender instead of Load
protected void Page_Load(object sender, EventArgs e)
{
this.PreRender += Page_PreRender
if (Session["userId"] == null)
Response.Redirect("Login.aspx");
}
protected bool reloadNeeded {get; set;}
protected void CreateObject(object sender, EventArgs e)
{
// SAVE THE NEW OBJECT
reloadNeeded = true;
}
protected void Page_PreRender(object sender, EventArgs e)
{
if(reloadNeeded || !IsPostBack)
// LOAD DATA FROM DB
}
You can check the event target and do what you need then:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string eventTarget = Page.Request.Params["__EVENTTARGET"];
if(whatever)
{
//do your logic here
}
}
}
Get control name in Page_Load event which make the post back
Use the Page_PreRenderComplete event to retrieve your table. That way your page will always have the most recent data available after all user events have fired.
Why don't you move what you have in the click event into a new method. Then call that method as the first line in your page load?
An old question but I faced the same problem in my C#/ASP.NET Website with master/content pages: a click on a link on the master page should change a query parameter for a gridview on the content page. As you stated the button click event is handled after Page_Load. But it is handled before Page_LoadComplete (see the information about ASP.NET Page Life Cycle), so you can change the page content there.
In my case I solved the problem by setting a session variable in the click event in the master page, getting this variable in the Page_LoadComplete event in the content page and doing databind based on that.
Master page:
<asp:LinkButton ID="Btn1" runat="server" OnCommand="LinkCommand" CommandArgument="1" >Action 1</asp:LinkButton>
<asp:LinkButton ID="Btn2" runat="server" OnCommand="LinkCommand" CommandArgument="2" >Action 2</asp:LinkButton>
protected void LinkCommand(object sender, CommandEventArgs e)
{
HttpContext.Current.Session["BindInfo", e.CommandArgument.ToString());
}
Content page:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string BindInfo = HttpContext.Current.Session["BindInfo"].ToString();
YourBindDataFunction(BindInfo);
}
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