On Page_Init I create a table of dynamically created controls based on a couple of database tables. One of the controls is an ImageButton for moving an list item up the list. What this event handler does is to update the SortOrder column in the database for the affected items.
Now the problem is that since the controls are created in the Page_Init event and the SortOrder is updated later on when the ImageButton command event is fired. What's the best procedure for updating the table with the correct SortOrder. If I recreate the table after the event has fired the ImageButton command event does not work any more.
Should I implement a method for updating the data in the table without recreating it?
Should I reload the page in code after the event has fired?
What's your preferred way for solving this problem?
Page events such as Init and Load will always fire before the event handler that raised the postback. This is the basis of the Page lifecycle (For a visual representation by Peter Bromberg, see here). Most developers new to ASP.NET have a major problem understanding and appropriately handling this "quandary".
The ideal way to do this is:
a. Your Page_Init should call a procedure (let's call it BindData() for illustration) that handles the creation of the table based on database data. This method would be similar to a binding method that binds to the database data and renders UI elements on the basis of that binding. IOW, you should remove the table creation code from the Page_Init method and put it in a separate method so that it can be called when needed.
Important note: This BindData() method also handles the attaching of the eventhandler for the dynamically created ImageButton control to the control. We'll call this ImageButton_Click. This is crucial for the control to the event to fire on subsequent postback.
b. When your ImageButton_Click method executes, it calls the BindData() method to recreate the table and it's bindings but with new sort order rules.
So, the order of execution on first load is:
Page_Init
BindData()
The order of execution on subsequent loads (on postback) is:
Page_Init
BindData() - Eventhandler for ImageButton attached.
ImageButton_Click
BindData()
You'll need something like this...
OnInit (IsPostBack = false)
Dynamically create ImageButton
Wireup ImageButton Event Handler
Load Table - Check for a sort-order in Session/Variable. If none; use the default
Click the button
OnInit (IsPostBack = true / 1st Postback)
Dynamically re-create ImageButton
Wireup ImageButton Event Handler
Load Table - with default sort order
ImageButton_OnClick (Still the same 1st postback)
Reload Table - with specific sort order
Save this sort-order variable in Viewstate/Session variable
Cause some other Postback
OnInit (IsPostBack = true / 2nd & Subsequent Postbacks)
Dynamically create ImageButton
Wireup ImageButton Event Handler
Load Table - Check for a sort-order in Session/Variable. If FOUND, use that.
Firstly, you seem to be binding your data manually to UI controls. In Asp.Net there and many ways to avoid this using built-in data binding techniques. Many controls like the GridView allow automatic creation of Html tables from a given data source. There are many other options including Repeaters.
However you do choose to bind your data, the technique is to rebind at some point every time through the page lifecycle.
You need to...
Bind you data on first page load with the default sort order
Rebind the data in the image button’s event handler after the sort order has been changed.
The code would look something like this...
private void Page_Load (...)
{
if (!IsPostBack)
//On First Load
BindData(defaultSoortOrder);
else
BindData(currentSortOrder);
}
private void ImageButton_Click (...)
{
currentSortOrder = newSortOrder;
BindData(currentSortOrder);
}
If the Image button is clicked, you will end up calling BindData twice. But this is necessary since a page postback could be initiated from any control, you need to always ensure you bind the data when the page loads.
Related
I have the control dropdownlist which has been loaded inside the template column of RadGrid.
While loading I have set AutoPostBack='True' for dropdownlist and also created the event SelectedIndexChanged.
DropDownList ddlConditions = new DropDownList();
ddlConditions.ID = "ddl" + name;
ddlConditions.AutoPostBack = true;
ddlConditions.SelectedIndexChanged += new EventHandler(ddlConditions_SelectedIndexChanged);
My question is while i change the selected index of dropdownlist the event SelectedIndexChanged is not getting triggered.
Can anyone help me to solve this problem?
Thanks in advance.
Usually this caused by a page life cycle problem. When your index changed event of Dropdownlist fires the control doesn't exist to bind it on the postback.
Example:
-> MyEvent fires.
-> Drop-down created.
-> Event Handler specified.
-> Index Changed event triggered. Page reloads. Drop-down not found, cannot fire.
So you have to ensure that the drop-down is created before .NET attempts to handle the event.
Please refer this answer for more information regarding this type of issue and life cycle.
I can suggest you to check the place where you have created DropDownList. Dynamic controls should be added on OnInit or at least on OnLoad. After OnLoad finishes executing ASP.NET starts processing control's events and values.
My question is while i change the selected index of dropdownlist the
event SelectedIndexChanged is not getting triggered.
Answer: because you have created DropDownList after the events have been processed.
Is the page posting back? If so, you'll need to make sure that the control is recreated on the page on every postback.
If it's inside the usual if(!IsPostBack) block, then put it outside - Usually, it's prudent to create controls in page_init as well, but that can depend on your specific setup.
I have a GridView on a page with a search button. The GridView is not visible to start with so that the user must click search before results are retrieved. The DataSourceID is set to the ID of an ObjectDataSource. When click is called, the following method is called from the click handler:
private void PopulateGrid()
{
gv.Visible = true;
gv.DataBind();
}
A problem occurs when the same method is called from the Page_Load handler. We store a user's search terms in their session and retrieve them the first time the page is accessed, something like this:
if(!PostBack && Session["search"] != null)
{
SetSearchFromSession();
PopulateGrid();
}
The problem in this case is that the ObjectDataSource's Selecting event is fired twice. Once when the GridView is made Visible, and again when DataBind() is called. I fixed this by substituting gv.Visible = true; for PopulateGrid(); in Page_Load.
But I'd like to understand what is going on. Why does setting GridView visible from page load result in DataBinding when a call in a button click event doesn't?
If you declaratively set the datasourceid then it is going to get called after PreRender and if you call DataBind it will be called again. (twice)
DataBinding
Raised after the control's PreRender event, which occurs after the
page's PreRender event. (This applies to controls whose DataSourceID
property is set declaratively. Otherwise the event happens when you
call the control's DataBind method.)
This event marks the beginning of the process that binds the control
to the data. Use this event to manually open database connections, if
required, and to set parameter values dynamically before a query is
run.
source
I have been reading about the Page LifeCycle. I understand the LifeCycle, however, it's not clear on what to do, and when to do it. The problem is I use Page_Load to get database values, and set form fields. I use a button's onClick method to update the database. However, the form fields text properties were set during Page_Load, so it's really only updating the database with the OLD values.
Page_Load: I gather data, and set control text properties to reflect data.
Button_onClick: I update the database from the form
Problem: It's updating values gathered from Page_Load and not the actual form.
Certainly, I am not supposed to perform everything in the Page_Load. So where am I going wrong during this process?
Page_Load
If you are loading your database data in the Page_Load event, the very first thing to do is to wrap it within a if (!IsPostBack) statement.
IsPostBack
Gets a value that indicates whether the page is being rendered for the
first time or is being loaded in response to a postback.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
So IsPostBack = true when the page cycle is the result of postback.
In your Page_Load, you should only gather your data when IsPostBack = false, not on every page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// gather your data here
}
}
Setting fields
I personnaly prefer to set the fields content on the PreRender event handler (but honnestly i don't know it should/must be done there, it just seems more logic to me).
PreRender is executed after your postback events (click on a button, drop-down selection change...) so it ensures that your updates and more generally data modifications are done before rendering the page.
I have an ASP.NET web form which I am adding a variable number User Controls to. I have two problems:
The User Controls are added to a PlaceHolder on the form in the first PageLoad event (I only add them when "(!this.IsPostback)", but then when the form is posted back, the controls are gone. Is this normal? Since other controls on the form keep their state, I would expect these dynamically added ones to stay on the form as well. Do I have to add them for every postback?
I also have a button and an event handler for the button click event, but this event handler is never called when I click on the button. Is there something special I have to do to catch events on dynamically added controls?
Yes, you need to add them in every postback.
Yes... the control needs to be in the control hierarchy before asp.net dispatches the event (i.e. create the dynamic controls as early in the page lifecycle as possible).
1) You should add the controls on the Pre-init (Page life cycle)
2) You have to attach the event handler to the event of the created button.(events might occur much later in the page life cycle than the same events for controls created declaratively)
To achieve this, add your controls at page init instead of page load. (re-add at postback)
You'll need to know the id of the buttons added to bind them to the event.
I ran into a similar problem. I had a page that displayed a collection of custom web controls. My solution was to add an additional invisible web control so that when I clicked a button to add another control that I would just use the invisible one. Then on post back my load function would add another invisible control to the collection.
I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I ran into the exact same problem and struggled through like 5-6 hours.
I'm posting this maybe someone like me could get help.
1) You should initialize your controls at Page.PreInit event. (In my case I had to add my controls to a place holder so I extended PreInit to load those controls before but you don't need to do that. It depends on your scenario.)
2) You should bind those exact methods to your controls after you initialize them in your Page.PreInit event.
Here is my sample code:
protected override void OnPreInit(EventArgs e)
{
// Loading controls...
this.PrepareChildControlsDuringPreInit();
// Getting ddl container from session and creating them...
if (GetDDLSession().Count != 0)
{
foreach (DropDownList ddl in GetDDLSession())
{
ddl.SelectedIndexChanged += SelectedIndexChanged;
phDropDowns.Controls.Add(ddl);
}
}
base.OnPreInit(e);
}
public static void PrepareChildControlsDuringPreInit(this Page page)
{
// Walk up the master page chain and tickle the getter on each one
MasterPage master = page.Master;
while (master != null) master = master.Master;
}
I am adding some user controls dynamically to a PlaceHolder server control. My user control consists of some labels and some textbox controls.
When I submit the form and try to view the contents of the textboxes (within each user control) on the server, they are empty.
When the postback completes, the textboxes have the data that I entered prior to postback. This tells me that the text in the boxes are being retained through ViewState. I just don't know why I can't find them when I'm debugging.
Can someone please tell me why I would not be seeing the data the user entered on the server?
Thanks for any help.
This is based on .NET v1 event sequence, but it should give you the idea:
Initialize (Init event)
Begin Tracking View State (checks if postback)
Load View State (if postback)
Load Postback Data (if postback)
Load (Load event)
Raise Changed Events (if postback)
Raise Postback Events (if postback)
PreRender (PreRender event)
Save View State
Render
Unload (Unload event)
Dispose
As you can see, the loading of ViewState data back to the controls happen before the Load event. So in order for your dynamically-added controls to "retain" those values, they have to be present for the ASP.NET page to reload the values in the first place. You would have to re-create those controls at the Init stage, before Load View State occurs.
I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I believe you'll need to add the UserControl to the PlaceHolder during the Init phase of the page life cycle, in order to get the ViewState to be filled in by the Load phase to read those values. Is this the order in which you're loading those?
Ensure you are defining your dynamic controls at the class level and adding them to the ASP container:
Private dynControl As ASP.MyNamespace_MyControl_ascx
And when you instantiate the control, ensure you call LoadControl so the object is added properly:
dynControl = CType(LoadControl("~/MyNamespace/MyControl/MyControl.ascx"), ASP.MyNamespace_MyControl_ascx)
You have to create your controls in the Page_PreInit event handler. The ASP.NET server control model is tricky; you have to fully understand the page lifecycle to do it right.
As others have said, any form of control manipulation must be done before viewstate is created.
Here is a good link on the page lifecycle to help you out:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
We have experienced the same thing and have handled it by using ghost controls on page_load that have the exact same .ID and then the post back picks up the events and the data. As others said it's the dynamic adding of the control after the init stages that the state is built already and controls added after aren't stored.
Hope this helps a bit.
I also want to add that I've seen user controls work the way that you'd expect them to just by setting the Control.ID property at run time. If you do not set the ID, items may get built in a different order and work oddly.