Sending values from one page to another using sessions - asp.net

I am sending values from send page to receive page using sessions, its working fine but the problem is I remember the URL and i directly run receive page its shows previous information but it's invalid is it....?
If end user directly visit receive page i need to restrict or give some message something like that
How can i overcome this problem..............
This is my code
send.aspx.cs
protected void gv_rowcommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource)).NamingContainer;
Session["c"] = grow.Cells[0].Text;
Session["s"] = grow.Cells[1].Text;
Session["e"] = grow.Cells[2].Text;
Session["t"] = grow.Cells[3].Text;
Session["a"] = grow.Cells[4].Text;
Response.Redirect("confirmation.aspx");
}
}
confirmation.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lblgetcourse.Text = Convert.ToString(Session["c"]);
lblgetstartdate.Text = Convert.ToString(Session["s"]);
lblgetenddate.Text = Convert.ToString(Session["e"]);
lblgettimings.Text = Convert.ToString(Session["t"]);
lblgetamount.Text = Convert.ToString(Session["a"]);
}

Once you have displayed the values on the confirmation page you could remove them from the Session.
Session.Remove("c");
...
This will ensure that the confirmation page will always display fresh values coming from the send.aspx page. You could also check for the presence of those values in the session before displaying them.

On Confirmation Page, Fetch the Values from Session Variables an display them , then Clear Session variables just like: Session.Clear();

You can check the referrer header :
Request.UrlReferrer
and see where the request comes from.
so your page_load will look like :
protected void Page_Load(object sender, EventArgs e)
{
if (!Request.UrlReferrer=='...') Response.Redirect("~/login.aspx");
...

protected void Page_Load(object sender, EventArgs e)
{
if(Request.UrlReferrer.AbsolutePath == "~/send.aspx")
{
lblgetcourse.Text = Convert.ToString(Session["c"]);
lblgetstartdate.Text = Convert.ToString(Session["s"]);
lblgetenddate.Text = Convert.ToString(Session["e"]);
lblgettimings.Text = Convert.ToString(Session["t"]);
lblgetamount.Text = Convert.ToString(Session["a"]);
}
else
{
//do something
}
}

Related

select master page at run time for a specific page

I have an asp page employeeHome.aspx and I have two master pages adminMasterPage.master for Admin login and userMasterPage.master for normal user login as I have 2 types of user login one as Admin and other normal user. And I want to set adminMasterPage.master as a master page for employeeHome.aspx in case of normal user login (just for this page).
How can I do this?
Put your code to change the master page in Page_PreInit event.
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MasterPage.master";
}
Put the code at employeeHome.aspx page
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["empCode"] != null)
{
if (Session["empCode"].ToString() != "0")
{
this.MasterPageFile = Server.MapPath("adminMasterPage.master");
}
}
}
You can change that by having the required master page file specified in the PreInit event, which is a part of the page life cycle..
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "adminMasterPage.master"; //For Admin
//this.MasterPageFile = "userMasterPage.master"; - For Normal User
}
This worked this way and here what I did as suggest by #Iswanto San and made changes in the path.
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["IsEmporAdm"].ToString() == "0")
{
this.MasterPageFile = "~/StyleLibrary\\layout\\AdminMaster.Master";
}
else
{
this.MasterPageFile = "~/StyleLibrary\\layout\\UserMaster.Master";
}
}

Update a label in server side in ASP.Net after Button Click

I want to update my label content when the user submits a form but it doesn't get updated. Although I have put it in if (!IsPostBack) condition in form load It doesn't show the changes. The only solution that I came up with was defining a counter and increase it in button_click event and check it before label update in !IsPostBack condition. Which is working fine with that.
Is there any other way to update the label text?
Here is my solution:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (count > 0)
lblSuccessMsg.Text ="A Message!";
count = 0;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Save();
count = count + 1;
}
please update your label code outside of !IsPostBack evnt and inside a pageload.
Hard to tell but by the sounds of it you have a submit button and the onclick needs to update this label, something like this should work. I'm using viewstate but session would work here, as would a redirect to the same page with a querystring parameter. Not sure if I've understood your question correctly though.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if(Viewstate["updateLabel"] == "true")
{
lblYourLabel.Text = "I'm updated now!";
Viewstate["updateLabel"] = "";
}
}
}
protected void btnYourButton_Click(Object sender, Eventargs e)
{
ViewState["updateLabel"] = "true";
//Do other stuff here if you want
}

When events fire, page starts from defaults

I've a Calendar on my webpage, and during the page_load event I'm setting the webpage to take today's date and load the data for today's date in the Gridview. Paging is allowed in the Gridview.
I also have a Calendar_Selectiondate event and when someone clicks on a date in the calendar, it will show data for that date. The date value is showed in a session variable. In this scenario when I click on the paging hyperlink 2, it will take me to the current day's second page instead of the selected day's second page. I know this is because it's going through the Page_Load event whenever I click on that hyperlink 2 and the date is getting set to Today's date instead of the selected date.
public partial class UKMail_UKMail7Day : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Calendar.SelectedDate = DateTime.Today;
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
//UKMail7DayGridView.DataSourceID = "UKMail7DayAllData";
//UKMail7DayGridView.DataBind();
}
protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
}
}
The events in your Page_Load execute no matter what triggers the postback. If you have code that should ONLY happen the FIRST time a page is loaded, put it within an if(!Page.IsPostback) block.
void Page_Load(object sender, EventArgs e)
{
// code that will execute on every postback, button click, etc.
if(!Page.IsPostback)
{
//code that will only execute the first time the page is loaded.
}
}
Strongly recommended reading: (Every ASP.NET developer should know this.) http://msdn.microsoft.com/en-us/library/ms178472.aspx
Edit using your updated code:
public partial class UKMail_UKMail7Day : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
Calendar.SelectedDate = DateTime.Today;
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
//UKMail7DayGridView.DataSourceID = "UKMail7DayAllData";
//UKMail7DayGridView.DataBind();
}
}
protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
}
}
In the page load event, set this
if(Page.IsPostback)
return;
Do this before any of your other code so it wont be executed on postback. I hope I understood you correctly.

Crystal Report error Missing parameter values when move to next page

My Crystal Report works fine in the first page but when i click on the next page button the report doesn't load and gives Missing parameter values error. Can anybody help me to solve this problem.
My current coding is given below.
protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
{
GenerateReport();
}
protected void GenerateReport()
{
//Create report document
ReportDocument crystalReport = new ReportDocument();
//Load crystal report made in design view
crystalReport.Load(Server.MapPath("Reports/PhotoGallery.rpt"));
//Set DataBase Login Info
crystalReport.SetDatabaseLogon("root", "pwd", #"localhost", "nsis");
//Provide parameter values
crystalReport.SetParameterValue("adno", adNo);
crvReportViewer.ReportSource = crystalReport;
}
I think you need to call GenerateReport(); method in page load as well try following
protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
{
GenerateReport();
ViewState["ReportLoad"] = "Load";
}
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["ReportLoad"] != null)
{
GenerateReport();
}
}

how to display the mark sheet on the next page using ASP.NET?

i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}

Resources