i tried to dynamicaly load GridView on the existing page from server. However, when I get the html from the server and insert it into the element, grid appears, but some actions are not available on it (for example drag and drop on columns, filters and so on). Moreover, i have a callback on init event and controls init event, but they dont invoke. Recently, i have loaded gridview with full page and all was good. On support center i found that i need to use ASPx.Evt.DispatchEvent(window, "load"); after insert gridview in my page, but there isnt any explanation about this. Currently, after i have added this piece of code and most of functionality works well and client side events fired, but some are still broken, for example GridView.PerformCallback();. What else i need to do to init controls on my gridview?
Origin code about 'load' event in devexpress support center
https://www.devexpress.com/Support/Center/Question/Details/T489045/how-to-determine-if-scripts-are-loaded
The aforementioned ticket refers to the MVC Extensions product, not the WebForms.
In the meantime, why do you need to render the HTML and add it to the DOM and initialize manually? It is not a good practice for the server-side controls (i.e., when the client-side counterpart should be properly processed).
If you need to add the DevExpress ASP.NET WebForms control dynamically, consider using, for example, ASPxCallbackPanel.
Related
So in a WinForm app, the Properties tab has an event button. It shows me all the possible events I can wire up to a delegate for the control that has focus. Then, all I have to do is double-click my event of choice and VS will auto-stub the delegate for me with the appropriate EventArgs type and everything. It'll then automagically take me to that delegate in the class.
How can I do the same thing in ASP.NET? For example, where can I click or double-click to have VS auto-stub in lifecycle Page events such as OnInit or OnUnload and then take me to the newly stubbed delegate? Surely this isn't always a manual process?
The Event tab in the property grid requires that Design View be up to date (synchronized) to work. For this to happen, you either need to be in design view, or have just left it without making changes to the document (or be in split view, so you can refresh the designer easily at will).
See also: Events tab randomly appears and disappears in VS 2008
Adding information from the comments:
Currently (VS2010), the only stubbing support for event handler generation using WebForms is for server control events. There isn't any tooling out-of-the-box to create event handlers for the Page itself (e.g. OnInit, OnUnload) if you're using code-behind files. However, if you are using single file webforms (i.e. no code behind) you can generate these via the Navigation Bar (disabled by default in VS2010, but enabled in previous versions).
Just select the element, go to event tab in property grid. Then double click the event you want.
Also make sure you are using "runat=server" and ASP.NET WebControls. In other words, not HTML controls.
As an addendum to the question: Please see Jimmy's comments above. Looks like everything else works fine but there's no method of auto-wiring up Page-type-specific events.
I'm working on a project where the page load certain controls depending on the index available. The loading occurs in the page load where the method PopulateSearchField is called.
Within this method, all the UserControl are added on the page using : Page.LoadControl("path");
The page load and all the required controls are on the page. My problem is when the user click on the Search button the event is triggered and a query is built based on the user input int those controls. Unfortunately, the method isn't able to produce a proper query as it is unable to find any of the controls on the page.
With a temporary ControlCollection variable, I've been able to see that the number of controls on my page is 3 when it should be something from 4 to 10. Those 3 controls in the collection are the static label and buttons on the page.
I don't know if something is wrong with the code or if it's a page cycle problem as this solution used to work on framework 1.1. Yeah, I know this isn't the best thing to do so, but they did it this way and I gotta make it work.
I'm not sure if it is the migration that has caused the problem or not.
Thanks a lot, David!
When you click the button, the controls are no longer available server side when your click handler is being processed. The page, server side, has no knowledge of the controls you created dynamically since there are no server side controls for the posted values to map to. If you want to find the values, you need to inspect the posted control data and not rely on the server side asp.net control heirarchy.
You could also write all the data you require to a hidden field via javascript and then read the hidden data server side since it will will be posted.
The following is occuring:
Creating controls dynamically
Posting controls data on click
ASP.NET maps the data to the existing controls it knows about.
Your controls are not found so the data is no mapped to anything.
You need to add your controls before the mapping occurs (in PreInit). Check out the Page Lifecycle and you will see how it ties all the controls and data together.
Are you re-adding the controls to the new page when the user clicks search?
Remember... every time the user hits your server for that page... a new Page object is created. If you're dynamically adding controls, you have to do it every time the page loads.
Additionally, since you seem to want to get values out of the controls, you're going to have to make sure that the controls are created with the exact same ID property every time, and created before viewstate is loaded, if you want them to retain their values.
I want to add more controls to page based on some specific conditions. Those controls don't need any ViewState or binding data, it is as simple as a static link.
I wonder where I could write the code: inside OnLoad or OnInit method? and why? If I place it inside OnLoad, should I add following line: if (IsPostBack) return; before any initialization code?
You can add controls in either the OnInit method or OnLoad, whether they need view state or not. (The reason why is because as soon as you add a control to the Page the control loads its view state, even if you add it after the LoadViewState stage...)
should I add following line: if (IsPostBack) return; before any initialization code?
No. It is imperative that your dynamically added controls are added to the control hierarchy on every page load, not just the initial one.
If you are going to work with dynamically-added Web controls, I strongly suggest you read these two articles:
Dynamic Controls in ASP.NET (This is actually three articles, this being the first in a series.)
Creating Dynamic Data Entry User Interfaces
For a working, end-to-end example of dynamically loading controls based on some external conditions (such as configuration in a database), see Creating a Dynamic Data-Driven User Interface.
Happy Programming!
I would suggest just adding the controls to the page statically and toggling their visibility to "True" when the conditions are met. They won't render anything to the page when they're invisible, and this will save you a lot of headaches, especially since it sounds like you're fairly new to dynamic controls.
I'm not sure I fully understand, but I'd personally put an asp:Literal on the page (or several if you need them in different places) and then create the HTML you need in the OnLoad event.
If you do that, then the html you put into that literal will be saved in viewstate, and therefor you won't have redo it on postback.
http://chetanwarade.wordpress.com/2010/08/21/asp-net-add-dynamic-control-and-retrieve-dynamic-control-value-2/
Here is code that demonstrate how to add dynamic control and retrieve dynamic control value.
I have an aspx page with a set of controls.
A small JS script written on top of jQuery allows the user to drag "li" elements from one list to another.
What I would like now is for my C# code to be able to figure out which items the user has placed in which list after the page is posted back to the server.
Does anyone know how I can do this?
Thanks.
Without some specifics, its hard to get into details. But for ASP.NET server-side controls, the trick is properly maintaining viewstate (or just bypassing it). So strategically you've got two fundamental options:
1) Track the changes server-side by using AJAX to push updates to the server.
2) Track the data client-side by updating some element ASP.NET could understand. My personal favorite is to use a hidden form field that I add a user's "moves" to in a format that can be replayed in my web form.
The easiest way to do it would be to add a HiddenField to your page. Whenever the lists change, populate the HiddenField's value accordingly. When a postback occurs, the HiddenField's value will be available on the server side.
What i've done in the past with drag & drop functionality in ASP.NET, is read the DOM elements via jQuery, and do an AJAX postback with the values in those DOM elements as a parameter.
I've built a ascx control and I would like to be able to keep adding new instances of it using JavaScript instead of having to do a AJAX callback. Is this possible? I am basically building a web form for a query control and should clause X be filled in, I want to generate a control for the next clause below. I would like to learn how to do this without doing a callback.
Thanks
ASCX are server side user controls and, to my knowledge, can only be loaded by a server event. This can be accomplished through a full page postback or using UpdatePanels and ASP.net AJAX.
If you don't want to use these options and stick with a full JavaScript solution, you're looking at probably doing DOM manipulation and dynamically adding straight HTML.
If the ASCX controls don't change their appearance and all you're doing is showing and hiding them, one last alternative could be to load all of them into DIV tags that have their display style set to none. Then when the user clicks on a checkbox or whatever, you can use JavaScript to show that DIV tag containing the next control. This is how many JavaScript tab setups work.