User input is overridden by page_load during cross page postback - asp.net

1the source page has a page load method like below:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
}
it will result a textbox1.text to display tomorrow's date when the source page is rendered. I have this source page cross post back to a target page, and in the target page load event i have
if (Page.PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
{
TextBox SourceTextBox1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox1 != null)
{
Label1.Text = SourceTextBox1.Text;
}
}
the problem is if the user changes the content of textbox1, supposely, the label1 on target page should catch the user input and display it, but now it only displays whatever i set in the source page load event. I understand the self page post back life cycle, but this is cross page post back. IMO, the source page load event has nothing to do with this, but why it overrides the user input?? Any idea.

Just surround this with a if(!IsPostBack) check:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
}
}
Otherwise the value will be overwritten on every postback. So when you Server.Transfer it to the other page it is already changed.

Related

Browser back button lost session on asp.net

I'm clicking browser back button and then I try to go to any aspx page, I'm losting session on my Asp.net web project.
How can I solve this?
My master page load code like that;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["admin_id"] != null)
{
AdminName.Text = Session["admin_name"].ToString();
mybadge.Text = Session["my_badge"].ToString();
}
else
{
Response.Redirect("admin.aspx");
}
}
}
I think you are not touching/saving any data before navigating to another page. So that's why session gets blank when you press on back button.

ASP.NET buttonclick called after master page load when trying to create session

I am trying to create a session when a user clicks a button. I want to update controls on the master.aspx page after the session is created from the login button being clicked. However the master.aspx page_load is being called before the button click function. Any ideas on how I could work around this?
Default.aspx.cs
protected void ButtonLoginIn_Click(object sender, EventArgs e)
{
Session["username"] = "Joe";
}
Master.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
LinkLoginInOut.Text = "Sign In";
}
else
{
string sessionId = Session["username"].ToString();
LabelDbUserName.Text = "Joe";
LinkLoginInOut.Text = "Log Out";
}
}
You can simply use FindControl on the Master
LinkButton lb = Master.FindControl("LinkButton1") as LinkButton;
lb.Text = "LinkButton Master";
You can try to move the code in the Page_Load event of the masterpage to an event, that is called after the button_click event. That way the changes to the session occur before the updating of the masterpage. You can try the Page_LoadComplete event. Please check the "asp.net page life cycle" documentation for the sequence, in which the different page events are called.

How to update CheckBoxes on the client side after making changes on the server side?

I have a DropDownList and a CheckBox on my web form. After the DropDownList is clicked and this event is posted back to the server. DropDownList_SelectedIndexChanged event is called on the server side. Inside that event handler, I have CheckBox.Checked = true, But I couldn't make the page on the client side to reflect this change (CheckBox.Checked = true). How do I achieve this? Or am I in the wrong direction to use the DropDownList's event handler to update the CheckBox because the page firstly reloads and then DropDownList_SelectedIndexChanged is called?
Page load method:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.Items.Clear();
AddItemsToDropDownList();
}
}
DropDownList selected index changed event handler:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = this.DropDownList1.SelectedItem.Text;
CheckBox checkBox = GetCheckBoxToBeSetByText(selected);
checkBox.Checked = true;
}
OK. Found the issue. Actually there is nothing wrong with the code in my original post. But to make a smallest sample when I posted, I removed some "extra" code. The below is the "complete" code (OK, fine, I still removed some code). As you can see, I put the CheckBox into a static Dictionary. Each time the SelectedIndexChanged event handler is called, it's modifying the CheckBox in that static Dictionary, which means it's modifying the CheckBox object created from the last session? (still not clear here) Looks like each time when a postback message is received, a new set of CheckBox objects are created. Bear with me if this is known to everybody here already because I only have two days of experience on this web development thing up to today.
private static Dictionary<Environment, CheckBox> EnvironmentsCheckBoxes;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EnvironmentsCheckBoxes = new Dictionary<Environment,CheckBox>();
EnvironmentsCheckBoxes.Add(Environment.Dev1, this.Dev1_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Dev2, this.Dev2_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.QA, this.QA_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.QA2, this.QA2_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Demo, this.Demo_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Prod, this.Prod_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.UAT, this.UAT_CheckBox);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = this.DropDownList1.SelectedItem.Text;
if (selected == "Dev1")
{
EnvironmentsCheckBoxes[Environment.Dev1].Checked = true;
}
else if (selected == "Dev2")
{
...
}
...
}

ASP.net passing data between pages

I have a .aspx web page, with a html form within it, this also has two input boxes.
Whats the best way to take the input box data and pass it to a new .aspx page where it is dealt with by the request method.
Assuming that the data is not sensitive then the best method to pass it to your new page using Response.Redirect and the querystring using:
protected void MyFormSubmitButton_Click(Object sender, EventArgs e)
{
string value1 = txtValue1.Text;
string value2 = txtValue2.Text;
// create a querystring
string queryString = "x=" + value1 + "&y=" + value2;
// redirect to the encoded querystring
Response.Redirect("NewPage.aspx?" + Server.URLEncode(queryString));
}
This web page has a lot of information which you can use for passing the values from page to page.
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx#Y1100
Try Server.Transfer:
Terminates execution of the current
page and starts execution of a new
page by using the specified URL path
of the page. Specifies whether to
clear the QueryString and Form
collections.
If you set the preserveForm parameter
to true, the target page will be able
to access the view state of the
previous page by using the
PreviousPage property.
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// ThreadAbortException occurs here.
// See http://support.microsoft.com/kb/312629 for more details.
Server.Transfer("AnotherPage.aspx", true);
}
}
"AnotherPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
// Accessing previous page's controls
}
}

UserControl Property Changing

I have created a User Control(Popupcontrol) and in that control i have created a property(PageType) and when i am using the Popupcontrol on the page then i set the property(pagetype) according to the page.
but now there is some problem i have to two button on the page and on the second button click i want to change the pagetype property .So is there any solution for the same.
Based on your comment, it seems you bind the data (PageType property in your question) in the Page_Load event, instead of this it should be done in overrided DataBind method which should be called if the page is not in post back request (otherwise your data will be overwritting in the next Page_Load event as you mentioned in your comments):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
}
}
public override void DataBind()
{
PageType = someValue;
}
after this your click handler may looks like:
protected void button2_Clicked(object sender, EventArgs e)
{
PageType = someOtherValue;
}
Are you setting the variable in a page load event? You may need to add:
if (!Page.IsPostback) {
// Code here.
}

Resources