How is this explained with ASP.NET page life cycle? - asp.net

I have the following code, which simply inserts a record into the database, based on some text fields and a dropdown. The dropdown gets bound in the Page Load Event.
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
Personeel p = new Personeel();
p.achternaam = txtNaam.Text;
p.naam = txtVoornaam.Text;
p.fk_afdeling_id = Convert.ToInt16(cmbAfdeling.SelectedValue);
BLLpersoneel BLLp = new BLLpersoneel();
BLLp.insert(p);
lblFeedback.Text = "Done and done!";
rptPersoneel.DataBind();
}
catch (Exception err)
{
lblFeedback.Text = err.Message;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack == false)
{
BLLafdeling BLLa = new BLLafdeling();
cmbAfdeling.DataSource = BLLa.selectAll();
cmbAfdeling.DataTextField = "naam";
cmbAfdeling.DataValueField = "afdeling_id";
cmbAfdeling.DataBind();
}
}
My question is about IsPostBack.
On first load, the page has no PostBack, so it will bind the data to the dropdown "cmbAfdeling".
Then, when submitting the form, there IS a postback, so we don't reach the code inside the if statement. To me, that would mean that ASP.NET would NOT bind the data to the combo box.
However, the data is still there after submitting (and thus having a postback).
How is this explained?

This is due to the ViewState. The data in the ComboBox is stored in the ViewState and is sent back & forth during postback.
This might be worth reading to understand what is happening: http://msdn.microsoft.com/en-us/library/ms972976.aspx

It's explained by a concept called viewstate:
If you examine the code produced by your asp, you will find some hidden fields, one of which is the "viewstate". The viewstate saves the important values of your asp in order to be able to populate the elements every time the pages gets loaded, even if it's after a postback.

The data is maintained during postback, as you don't clear the data during postback or on load it will persist.

Related

Click event not detected in UserControl

I've some UserControls which are created dynamically, with parameters :
foreach (DataRow dr in drc)
{
question = (from ques in bdd.QUESTION where ques.ID_QUESTION == idQuestion select ques).FirstOrDefault();
List<Object> listParams = new List<Object>();
listParams.Add(question);
AdminQuestion qa = (AdminQuestion)(Controller.LoadControl(Page, "~/UserControls/AdminQuestion.ascx", listParams.ToArray()));
pnl_question_list.Controls.Add(qa);
}
This is a method I found on SoF and it works great.
This method is called when I click on a button, and all my usercontrols are correctly created.
In this usercontrol, I have a button :
<asp:Button ID="btn_validation_modify_question" runat="server" Text="Modifier"
CssClass="clear_left_float_left myBouton myBoutonEnvoyer"
OnClick="btn_validation_modify_question_Click"/>
And my method :
protected void btn_validation_modify_question_Click(object sender, EventArgs e)
{
QUESTION q = (from m in bdd.QUESTION where m.ID_QUESTION == question.ID_QUESTION select m).FirstOrDefault();
q.MESSAGE = txt_modify_question_message.InnerText;
q.ID_THEME = new Guid(ddl_modify_question_theme.SelectedValue);
bdd.ApplyCurrentValues<QUESTION>("QUESTION", q);
bdd.SaveChanges();
}
But when I click on the button, it don't apply the method, and reload the page, like if I didn't have any declared method.
Is there something I don't know? I already used this technique and everything worked...
If you add controls dynamically, you'll normally need to do it on Init or PreInit page events. Otherwise event handlers will never work.
So, dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the Page class's Init event in your code-behind class, and add your dynamic controls there.
Note You may be able to get away with loading your controls in the
Page_Load
event handler and maintaining the view state properly. It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you're doing it relative to the
Controls.Add(dynamicControl)
line. A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the
Controls
property's
Add()
method recursively loads the parent's view state into its children, even though the load view state stage has passed.
Here you have more information.
EDIT
Just to be sure we're talking about the same, here you have how your code should look like
protected void Page_Init(object sender, EventArgs e)
{
...
foreach (DataRow dr in drc)
{
question = (from ques in bdd.QUESTION where ques.ID_QUESTION == idQuestion select ques).FirstOrDefault();
List<Object> listParams = new List<Object>();
listParams.Add(question);
AdminQuestion qa = (AdminQuestion)(Controller.LoadControl(Page, "~/UserControls/AdminQuestion.ascx", listParams.ToArray()));
pnl_question_list.Controls.Add(qa);
}
...
}
PageLoad(){
If(!Page.IsPostBack())
foreach (DataRow dr in drc)
{
question = (from ques in bdd.QUESTION where ques.ID_QUESTION == idQuestion select ques).FirstOrDefault();
List<Object> listParams = new List<Object>();
listParams.Add(question);
AdminQuestion qa = (AdminQuestion)(Controller.LoadControl(Page, "~/UserControls/AdminQuestion.ascx", listParams.ToArray()));
pnl_question_list.Controls.Add(qa);
}
}
Did you try with this solution? And not use Page_Init()?

Maintaining GridView current page index after navigating away from Gridview page

I have a GridView on ASP.NET web form which I have bound to a data source and set it to have 10 records per page.
I also have a hyper link column on the GridView, such that a user can navigate to another page (details page) from the list. On the details page, they have "Back" button to return to the GridView page
Edit
Just to clarify the query
I am looking for sample code snippet on the Server Side on how to specify the page index to set the GridView after data binding. The idea is to ensure the user navigates to the same page index they were on.
The three basic options at your disposal: query string, session, cookie. They each have their drawbacks and pluses:
Using the query string will require you to format all links leading to the page with the gridview to have the proper information in the query string (which could end up being more than just a page number).
Using a session would work if you're sure that each browser instance will want to go to the same gridview, otherwise you'll have to tag your session variable with some id key that is uniquely identifiable to each gridview page in question. This could result in the session management of a lot of variables that may be completely undesirable as most of them will have to expire by timeout only.
Using a cookie would require something similar where cookie data is stored in a key/data matrix (optimized hash table might work for this). It would not be recommended to have a separate cookie name for each gridview page you're tracking, but rather have a cookie with a common name that holds the data for all gridview pages being tracked and inside that have the key/value structure.
Edit: A small code snippet on setting the page index.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
try
{
if(HttpContext.Current.Request["myGVPageId"] != null])
{
myGridview.PageIndex = Convert.ToInt32(HttpContext.Current.Request["myGVPageId"]);
}
}
catch(Exception ex)
{
// log it
}
}
}
I'm more of a fan of the Session approach, personally. Simply save your page index as a session variable, and, if this Session variable isn't null on page load, use it to fire your "OnPageIndexChanging" method, like so:
Set your current page number whenever the page number changes:
protected void GridViewIndexChanging(object sender, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
Session["pageNumber"] = e.NewPageIndex;
//whatever your page index changing does...
}
Then, on Page_Load do something like:
if (!IsPostBack)
{
if (Session["pageNumber"] != null)
{
GridViewIndexChanged(myGridView, new GridViewPageEventArgs((int)Session["pageNumber"]));
}
}
you can ues the Page Index Change Event of Gridview and Find out the Current Page Index for e:g
yourGridId.PageIndex=e.NewPageIndex;
ViewState["GridPageIndex"]=e.NewPageIndex;
on PageLoad Get the Viewstate Value
string pIndex=string.Empty;
pIndex=Convert.toInt32(ViewState["GridPageIndex"]);
if(!string.Empty(pIndex))
{
yourGridId.PageIndex =pIndex;
}
you must use query string and is recommended, otherwise you can use session object but don't use that for this as you may have grid view opening in different pages so use query string .
gridView1.CurrentPageIndex = (Request["pageNo"] != null) ? Request["pageNo"] as int : 0;
gridView1.DataSource = myDataSet;
gridView1.DataBind();
you can update your link on GridView_DataBound event

how to get value of hidden field value?

I have an asp.net application in which i want to get the value of hidden field value in to my code behind page.
Actually its executing code first and after that its executing javascript.
That's why i am not getting the value. Please tell me how can i get the value.
Javascript
function changeCSS(colr,wd)
{
chooseStyle(colr, wd)
var CssTitle = document.getElementById('<%= hdCssTitle.ClientID %>');
CssTitle.value = colr;
}
and code behind page is
protected void Page_Load(object sender, EventArgs e)
{
string CssTitle = hdCssTitle.Value;
}
Values changed in Javascript won't be reflected in your server side script. You would have to make an Ajax post to your code-behind to obtain it.
Get hidden field and access that value like this string s = hidden.Text

Linq 2 SQL - Manual Databinding not working

I'm using linq2sql in my asp.net app. When using it with linq2sqldatasource object everything works, i mean i bind it without no code to a detailsview control.
My idea is when i click a row in the detailscontrol e will load/add to the same page a customwebcontrol that will permit edit the data.
For that i need to load some items to fill the dropdowns in that customcontrol, and in its load event i have the follwoing code that doesn't work and i can't see why. It raises a object null reference exception.
example:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//loads combobox with organizations
using (MyDataContext cdc = new MyDataContext())
{
var queryOrgs = from p in cdc.orgUnits
select p;
//Organizations
dropDownOrgs.DataSource = queryOrgs.ToList();
dropDownOrgs.DataValueField = "orgUnitID";
dropDownOrgs.DataTextField = "orgUnitName";
dropDownOrgs.DataBind();
}
}
}
Anyone know what is happenning? Looks like when i want to bind all by myself manually something do not work :(
Hope you can help me.
Thanks.
Teixeira
#Chalkey is correct. I have run into this error myself, where due to the fact that LINQ to SQL does "lazy" querying, it waits till the last minute to actually perform the query.
This means that it may wait until after the page_load function to do the query (and therefore outside of the using statement).
Therefore, return the data as a list with .ToList() to force it to run the query immediately.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//loads combobox with organizations
using (MyDataContext cdc = new MyDataContext())
{
List<orgUnit> queryOrgs = (
from p in cdc.orgUnits
select p
).ToList();
//Organizations
dropDownOrgs.DataSource = queryOrgs.ToList();
dropDownOrgs.DataValueField = "orgUnitID";
dropDownOrgs.DataTextField = "orgUnitName";
dropDownOrgs.DataBind();
}
}
}

javascript timer

I am developing an online exam application using asp.net. In the start exam page I have created a javascript countdown timer.
How can I move to the next page automatically after the timer reaches 00?
Here is my code:
long timerStartValue = 1000 ;
private int TimerInterval
{
get
{
int o =(int) ViewState["timerInterval"];
if(o==0)
{
return (o);
}
return 50 ;
}
set
{
ViewState["timerInterval"] = value;
}
}
protected void Page_PreInit(object sender,EventArgs e)
{
string timerVal = Request.Form["timerData"];
if(! String.IsNullOrEmpty(timerVal))
{
timerVal = timerVal.Replace(",", String.Empty) ;
this.timerStartValue = long.Parse(timerVal);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)
{
this.timerStartValue = 10000; //3599000;//14400000;
this.TimerInterval = 500;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.timerStartValue = 3599000;
}
protected void Page_PreRender(object sender, EventArgs e)
{
System.Text.StringBuilder bldr=new System.Text.StringBuilder();
bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID);
bldr.Append("Timer.go()");
ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}
Thanks in advance,
sangita
It sounds like when you click the "Next" button, you are loading an entirely new page. This of course changes all the content and resets all the javascript. You can't maintain state across pages without a bit of work.
The solution to this could be to save the timer state when the next button is pressed, and pass it to the next stage. You could do this by saving the timer state to a hidden form input and submitting it along with the Next button.
The other option would be to load your questions via AJAX. Instead of moving to a new page every time the next button is clicked, you could simply replace the question portion of the page with a new question, and leave the timer intact. This is probably the solution I would use.
Are u reloading the entire page when clicking on the next button ? That may leads to realod the java script file also.So the variable values will reset.May be you can think about showing the questions /answers via Ajax.You need not reload the entire page when showing the next question.the part when you show the quiz will only be updated.so you can maintain the global variables in your java script too. Check the below link to know about partial page updating using jQuery.
http://www.west-wind.com/presentations/jquery/jquerypart2.aspx
Hope this helps.
You can put the timer in an iframe if you can't get rid of the postback.
You need a way to persist information between pages, and there's really only one possibility: To make it part of the next page request.
Now, this could be subdivided into 2 categories:
1) As part of the url: http://www.example.com/page?timer=123;
2) As part of the headers;
And number 2 opens new possibilities:
a) As part of POST data;
b) As a client-side cookie only;
c) As a cookie tied to information on the server;
Number 1, 2a and 2b can be manipulated by the user. So what you can do is store some value in a cookie, a hash for example or a database row ID, that you'll use to fetch information on the server.
tl;dr? Use a asp "Session object". It lets you keep things on the server-side and users will have no idea what they are.

Resources