Ok, I've solved my problem but I don't know why one type of control works and another doesn't.
The scenario: I'm working on a custom wizard style page design that was originally done completely with UserControls in C# ASP.Net 4.0. The step that you are viewing is controlled on server side via control visibilty.
My problem: I dislike the UserControls and want to use WebControls as much as possible, though it's not worth my effort to redo the entire thing with WebControls, and new controls are needed to expand the functionality.
My original solution: I initially started creating WebControls as needed. This worked fine until I got 2 steps away from a given WebControl, where it lost it's ViewState. At least I believe that is what happened. It was a CheckBoxList and as soon as I was 2 steps away, the ListItems disappeared.
The final solution: Recreating the same control as a UserControl I always have access to the CheckBoxList Items.
This all seems fairly logical. If a control or one of it's parent's is not visible, it won't be included in the ViewState data. I guess the real question is, why does UserControl work?
Related
I apologize if this is not a proper question, but I am new to StackOverflow, so here goes...
I am programming in C# for the web in VS2005 (at the company I work for; I use VS2012 at home), and have written a somewhat lengthy program for work.
My problem is that I can add certain controls on the master/home page, such as a nested gridview or even a simple dropdownlist, and they work better than beautifully. When I add the exact same control, with the same SQL data source, to a lower-level webpage, they refuse to work. The SQL data loads just fine, but the SelectedIndexChanged or SelectedIndexChanging methods don't work. Yes, I have checked everything from AutoPostBack=true to the ViewState=true.
Oddly enough, almost every page (20+) in the program has at least one regular gridview, and they work like a charm on every page. I have not yet coded a regular gridview that has NOT worked.
I'm new to ASP.net, the ViewState, and C# in general.
I have some code that adds and deletes boolean constraints that will later be submitted as an SQL query. I dynamically create these constraints with unique IDs. When I delete a constraint earlier that was added before some other constraint, the viewstate tries to map the contents of the DropDownList that was deleted to the DropDownList immediately below it.
This is turn triggers an index changed event and causes undesirable behavior in the application. I'm fairly certain that this behavior is caused because the ViewState loads the postback data into dynamically created controls via index.
I've been searching for a solution came across [ViewStateModeById] as a possible solution. Unfortunately, this seems to only to custom controls. I would like to avoid creating new custom controls for everything just to index the controls by ID.
Is there a way to gain this functionality without having creating custom controls?
I should mention that each dynamically generated constraint (which is a set of about 5 controls) is inside a dynamically generated panel. I could just keep creating empty panels as a sort of placeholder to get my desired behavior, but that is just a hack. I'll end up with many, many panels unless I implement another hack to occasionally delete some of my panels.
I struggled with the viewstate until I got it. This is not a real answer but when working with viewstate it is very important that the dynamic controls created gets the exact same id after each post back. It means that you'll have to re-create/re-bind everything on each post pack.
If it does I'd be surprised if you get strange behaviors and you should be able to code almost like if it was a desktop app.
Dynamically adding web controls to the page's control tree and later deleting some of them seems a bit strange to me.
But anyway, if you want to get rid of already existing controls in the control tree, you could just set their Visible property to false. Controls having Visible==false are not rendered to the client and thus behave almost like they wouldn't exist at all.
Or, what about creating them dynamically without immedialetly adding them to an actual control tree?
You could e.g. add and remove references to and from the Context.Items collection at any time during the page request and finally add just the ones you need.
I am struggling with finding clear answers to dynamically creating the same page over and over. The questions and samples I have found seem to be all over the board on this topic. I have studied the life cycle and still seem to not have a clear answer as to where code should go.
I have a master page and a content page. All the content in the content area needs to be dynamically created (text boxes, ddl's, page tabs, buttons/onclick etc.).
After a user fills in data and clicks a submit button, I need to read the values off the form and rebuild the page completely again (not add/remove controls to current content).
My question is then.
Where do I put my code to build the page?
Will this area allow me to use IsPostBack so I can rebuild content with Request.Form values?
Will my buttons _Click events work?
Are there any working samples out there you could direct me to?
Thank you very much for the feedback...
I don't know all the answers to your questions, but I hope this may get you started. When dynamically generating the UI through code, this happens in Init. Controls dynamically loaded on Init is key because between init and load, on postback, viewstate is loaded for these controls.
This means you need, on every postback, recreate the page as is to match the previous control tree, then deconstruct it after init and recreate the new UI, if something is supposed to change UI wise. This is because it validates the tree structure to determine its the same UI. Now, if you don't need viewstate, this may not be as much of an issue. I haven't verified this without viewstate to see if it behaves different.
It depends how dynamic you need it, whether you need viewstate (is a big factor).
HTH.
Try creating the controls in the page's PreInit method. "IsPostBack" should work and the click event handlers should work as well.
What you need is a web user control, see ASP.NET User Controls
Brian's advices are good and you should follow them.
This might not really answer your question but still I add it as an advice. I'm professionally creating ASP.net web applications at quite a large scale and from my experience I can say that too much "dynamics" is usually bad and should be avoided because it just introduces complexity. Normally you might want to expose UI parts into ASP.net UserControls or if you want to make them even more reusable (if that's a factor) then into ASP.net Server controls. Then you replace different of them dynamically rather than creating everything from scratch.
I'm developing a Silverlight application for the first time. I've gone through some tutorials, but I can't seem to find anything that helps me with this particular problem. I would like a set of buttons to be present on all of my pages (like a template). When a button is pressed, I would like the ContentGrid to slide out and a new ContentGrid slide in (with the relevant .xaml file being loaded).
Are there any tutorials showing the best way to do this? From samples I've seen, they only seem to transition between two pages, so copy-pasting the group of buttons on each xaml page isn't too much of a problem. However, with more pages, it would be inefficient to copy-paste the base layout each time.
Thanks for any suggestions
I used to create a master view and create each page content as a user control; that way, I can swap out/in the user control for the appropriate page, and I would have a consistent header that way... similar to a master page in ASP.NET. I'm sure there are other templates too; I'm not aware of everything available for Silverlight...
The one answer I cannot provide is the transition; the only thing I can offer is Telerik has a control for that purpose, the transition control: http://demos.telerik.com/silverlight/#Controls I understand if you can't drop a couple of hundred bucks to get it though :-)
HTH.
Have you tried the Silverlight Business Application Visual Studio Project template?
I downloaded the source code from this tutorial after a failed attempt to make my own Wizard. The control works great and is integrating nicely into my program. I have one minor problem, though. I have a page that has both a databound ListBox and a databound TreeView. When changing to the next or previous page, the TreeView maintains its selections but the ListBox does not. Instead, it appears to update itself (judging by the flash). How can I prevent this from happening and maintain the user's selections?
Thanks,
Joe
Ugh! Serves me right for re-using code that I haven't looked at in a couple of months. Turns out the TreeView is not data bound but populated from a Value Object. I modified the ListBox to use its own VO and now it works perfectly. Kind of a complicated way of doing things but whatever works, right?