Keep page contents while post back - c#

When ever i do postback the page get blanked, i have seen some pages, where page is not being blank while post back
can some give me idea how it can be done
Thank You

private void Page_Load()
{
if (IsPostBack)
{
// It is a postback
}
else
{
// It is not a postback
}
}
refer this link
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

Did you bind data on post back?
example:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.Text = "";
}
try adding if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
textBox1.Text = "";
}
}

Try using update panels. This way you only have to post back the areas that require post back. You will not feel the flicker.

Related

Session variable has old value, needs to update with page redirect

I have session variable that dont update with new value. I have two pages, one were you enter the values and klick on the button and you get redirected to page 2 and there you can check your input, if this is wrong you click back-button and you go back to the first page where you can change the input but now when i click the button to validate again the new value does not show up in the session variable but only the old value. I have been readingabout session for the last day but i just cant find the problem, the behind code is below:
Page1
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
public string info { get { return lbl1.Text; } }
protected void inputButton_onclick(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
Session["value1"] = info;
Response.Redirect("~/validpage.aspx");
}
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value1"] != null)
{
lbl2.Text = (String)Session["value1"].ToString();
}
}
protected void BackButton_Click(object sender, EventArgs e)
{
Session["value1"] = lbl2.Text;
Response.Redirect("~/Default.aspx");
}
Maybe i have staired my self blind on this code as to me this should not have this problem it is presenting. Any idea and help will be appreciated.
Every time Page1 loads, lbl1 is set to the contents of the session, unless it's never been set. So when you click the button, the lbl1 is first set back to the content of the session as the page is loaded. You then read this value back & but it back in the session.
try this instead:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
}
This says only set the value if we're not postback, ie NOT clicking a button on the page.
Also in page2, there's no need to call ToString AND cast to a string. Do either, not both.

Passing session variable from page to page

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");
}

Validate data in Radgrid before changing pages

I want to validate the users changes on a page before allowing them to go to another page. If the validation fails I want to stop the pager from changing the page.
For example:
protected void rgOrderItem_PageIndexChanged(object source, GridPageChangedEventArgs e)
{
if (Mapvalues(false))
{
rgOrderItem.CurrentPageIndex = LastPageIndex;
rgOrderItem.DataBind();
}
}
This does not work. The pager changes regardless. Anyone know how to stop a page change event?
Thanks, Tony
Please try with the below code snippet.
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
if (Mapvalues(false))
{
e.Canceled = true; //Prevent to execute pagging functionality
}
}
protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
{
if (Mapvalues(false))
{
e.Canceled = true; //Prevent to execute pagging functionality
}
}

Redirect as a fresh request after using Response.Redirect

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.

How can I load the Same ASP Grid View with different conditions?

I have a DDL and a ASP .net Grid view in my aspx page. I have two methods getALLProgram and getProgramBy name, both are working fine. My problem is: when the page is loaded for the first time, I want to call the getAllprogram method, after that if a User selects a program from DDL I want my getprogramByname method to be called.
How here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindProgramDDL();
BindGrid();
}
//BindProgramDDL();
}
protected void BindGrid()
{
string strProgramCode = DDLProgram.SelectedIndex.ToString();
List<FormGridEntity> gridEntities = new List<FormGridEntity>();
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
//throw new NotImplementedException();
}
protected void BindProgramDDL()
{
List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
The Code is incomplete. i am still working on it. But I not getting the logic How will I make this happen that I have told you here. I hope I made my question clearly, if it confusing, please let me know what else I should provide here.
You should check in your BindGrid if any program has been selected or not and route the call as per that. For example,
protected void BindGrid()
{
...
if (DDLProgram.SelectedIndex >= 0)
{
// program selected
var programCode = DDLProgram.SelectedValue;
data = GetProgramByName(programCode);
}
else
{
// get all programs
data = GetAllPrograms();
}
// bind data with grid
}
You can either call BindGrid in page_load unconditionally (i.e. in post-back scenarios also) or invoke it on your DDL change.
how about writing getProgramByname on a selected index changed event of a drop down list and getALLProgram on page load event ?
I hope, I was clear on what your doubt and the above mentioned suggestion did helped.
Just change these 2 things
protected void BindGrid()
{
List<FormGridEntity> gridEntities = (DDLProgram.SelectedIndex==-1)
?FormSaleSubmit_BAO.GetAllPrograms()
:FormSaleSubmit_BAO.GetProgramByName(DDLProgram.SelectedValue);
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
}
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
BindGrid();
}

Categories

Resources