Greetings,
1) I assume ObjectDataSource automatically binds to data source only on first request, but not on postbacks ( else ObjectDataSource.Selecting event would be fired on postbacks also, but it isn’t):
A) So only way to force ObjectDataSource to also bind on postbacks is by manually calling DataBind()?
2) Assuming DropDownList1 has DataSourceID set to ObjectDataSource1 , then first time page is loaded, ObjectDataSource1 will automatically call DropDownList1.DataBind() ( after Page.PreRender event) and insert retrieved data.
A) But what if we also manually call DropDownList1.DataBind() when page is first loaded:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) DropDownList1.DataBind();
}
Will ObjectDataSource1 somehow notice that DropDownList1.DataBind() has already be called and thus won’t automatically call DropDownList1.DataBind() ?
B) Normally ObjectDataSource1.Selecting event is fired after Page.Prerender event.But what if DropDownList1.DataBind() is called inside Page_Load()?
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
thanx
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
Yes it is called prior to Page.PreRender.
Reason: Each data bound control whose DataSourceID property is set calls its DataBind method in prerender event,
check page life cycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx
http://dotnetshoutout.com/Data-Binding-Events-for-Data-Bound-Controls-in-ASPNet
Since the load event is called before prerender, and when call databind method then in your situation objectdatasource selected event fired before prerender
Related
I have an Asp:ListBox with OnSelectedIndexChange="OnSIC" and AutoPostback="true".
I want to keep track of the selected index after the postback to show some data that depends on the index.
My idea is to save the selected index in ViewState.
protected void OnSIC(object sender, EventArgs e)
{
ViewState["idx"] = listBox.SelectedIndex;
}
However, after the page is refreshed, Page_Load does not yet have the updated ViewState["idx"] value. An additional refresh is required to make Page_Load get the new index value.
I understand that postback events happen after Page_Load, so I tried moving the code that gets ViewState["idx"] in Page_PreRender, but I get the same behavior.
How can I correctly store the listbox's selected index value, after a OnSelectedIndexChanged event is fired, and use it on the very next page refresh (that happens because of the event postback)?
In my Asp.Net Web Form,
Assuming that, I have Main_page and Save_button that it is on Main_page.
When I click Save_button, firstly Page_Load eventexecute and after this btnSave_Click button execute.
I thought that when I click button firstly and only button might execute.
Is it correct or my program doesn't work correctly ?
It is normal that Page_Load is executed first and event handlers afterwards. So your program behaves as designed.
Excerpt from MSDN on the page lifecycle:
Load
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handling
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
If you are interested in details on the lifecycle of an ASP.NET Page, have a look at this link.
Resolution
If you need to perform certain steps in Page_Load (or any other method on your page) only when the page is requested first, you can check the IsPostBack property and thus make your program behave as you describe in your question:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Steps are only run on initial GET, not when request is a PostBack.
}
}
It is correct that load event fires first then button event handler. If you want to execute code only at first, but not when any postback check IsPostBack property.
I am trying to modify a control on a page to reduce session dependence. I currently do not have access to the page itself, only the control which primarily consists of a DataGrid. I am trying to retrieve the DataGrid information on postback so I can manipulate the data and rebind the grid.
The issue is that the page is calling a databind on the control before I can retrieve the data. (actually it is calling the databind on the tab control where my control is located.) This call is happening on the OnLoad event of the page, before the OnLoad of the control is called. I saw that the is a PreLoad event that occurs after the viewstate is loaded but before the OnLoad is called. However I am having issues accessing this event from my control. Is there anyway I can access this event so I can retrieve the data before the page overwrites it?
Add the following code to your control instead of the OnLoad. (from here)
protected override void OnInit(System.EventArgs e)
{
// this assigns Page_PreLoad as the event handler
// for the PreLoad event of the Control's Page property
this.Page.PreLoad += Page_PreLoad;
base.OnInit(e);
}
private void Page_PreLoad(object sender, System.EventArgs e)
{
// do something here
}
This might help: MSDN - ASP.NET Page Life Cycle Overview
The image 1/3 of the way down sums it up.
I want to assign property value on button click in aspx page and want to pass the value to the usercontrol and than bind data according to the Property value.
But the problem is before button click event is fired the page_load of user control is invoved ? is the any way to call the page_load of user control again on button click or is there anyother alternative to do it ?
Thanks
Utilizing Page_Load from a UserControl makes the control very dependant on the page life cycle and thus not very flexible. A better way would be to add a public method to the control that you call from the button OnClick event. That method would then perform the data binding.
Kinda like this:
//MyPage.aspx
void Button_OnClick(object sender, EventArgs e)
{
MyUserControl.DataBind(MyTextBox.Text);
}
//MyUserControl.ascx
public void DataBind(string value)
{
UpdateView(value);
}
This is kludgy, but if you have to you can always call the Page_load event manually after the button fires.
A better approach would be: depending on what code needs to fire after the button_click event, you can move it to another event handler, like the OnPreRender method.
I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.
The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in MyRepeater.Items)
{
MyLabel = (Label)item.FindControl("MyLabel");
}
}
The button is located in the FooterTemplate of the repeater.
<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />
Thanks in advance.
Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)
Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?
THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.
example:
You request the page. On Load Fires. You bind the data to the repeater.
You maniupulate the data in the reapter then click your button
The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
the onclick event is triggered and your code tries to retrieve values that no longer exist.
Make sure your databinding code in your onLoad event is nested within an postback check
if (!Page.IsPostBack)
{
Repeater.DataSource = Datatable;
Repeater.DataBind();
}
I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.
private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
//populate dataTable
if (!IsPostBack)
{
//databind to repeater
}
}
protected void Submit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repeater1.Items)
{
DataRow row = myTable.Rows[item.ItemIndex];
}
}
Ideal? Certainly not but it works.
Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.