Dynamically create <form runat="server"> - c#

How can I create a <form runat="server" /> with code (c#)? My problem is that I want to have multiple forms on my page (and none initially). I'm loading all components from db.
I've looked everywhere and can't find anything about it.

From painful memory, working with multiple forms in ASP.NET-webforms is a real pain. Maybe consider looking at ASP.NET-MVC which is much closer to the HTML; consequetly you can do anything you like re forms; just use a foreach in the view, writing a form for each item.

ASP.NET page can have only one "server side" form.. that's one of the "core" rules.
So you'll have to find other way to achieve what you need.
You can work with UpdatePanels and "submitting" specific panel should send only its contents, ignoring the other data in the form.

ASP.NET only allows for one serverside form but allows multiple regular forms in one page. I suggest you read this article: ASP.NET Forms.

Thanks for the replies. I solved it using this inside my Page:
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
And then render my form with plain text.. Can't see any errors yet, but will update if any turns up.
Also, as I said in my question, I'm loading all control dynamically and I don't use any code behind with the controls (if I did, I guess some errors would turn up). All my post are handled by checking against HttpContext.Current.Request.Form.

Related

RadWindowManager, RegisterScriptControl, and multiple controls with same ID issues

We just upgraded our Telerik controls from 2012.3.1308.45 to 2014.1.1403.45. As a result, a page that previously worked, doesn't now.
The initial error is as follows:
Script control 'RadWindowManager1' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
There are a couple of postings on the Telerik site about this. The preferred solution seems to be to set the RegisterWithScriptManager attribute on the control to false.
Doing this, then, makes another error pop up. So, I'm not sure if I'm moving forward or not. The new error is:
Multiple controls with the same ID 'RadWindowManager1_alerttemplate' were found. FindControl requires that controls have unique IDs.
The page has a bunch of Response.Write(...) ASP .NET expressions in the form of: <%=MyControl.ControlID%>. Every one of these expressions causes this error.
At this point, I'm stumped.
There was another posting on the Telerik site that suggested adding a Location element to the web.config for the Telerik.Web.UI.WebResource.axd would solve this. I tried that and it didn't do any good.
I also tried to use the Microsoft ScriptManager rather than the Telerik RadScriptManager, and that doesn't do any good.
I checked my web.config for duplicate entries, and there are none. I don't know what else to do. I'd appreciate any assistance on this issue.
For what it's worth, I've made some progress or dug myself deeper into a hole. I don't know:
Working with the second error, the multiple control issue, I discovered that we had on our page, a couple of controls that I got rid of.
First, I removed:
<telerik:RadCodeBlock runat="server"></telerik:RadCodeBlock>
primarily, this block appeared empty as there was nothing inside of it. Don't know if it's needed or not, but I removed it.
Second, I removed this:
<telerik:RadAjaxPanel ID="gridPanel" runat="server" LoadingPanelID="gridLoadingPanel">
<telerik:RadAjaxLoadingPanel ID="gridLoadingPanel" runat="server" Transparency="5">
<asp:Image ID="Image1" runat="server" AlternateText="Loading..." BorderWidth="0px" ImageUrl="~/App_Themes/Default/Images/Administration/Loading.gif"></asp:Image>
</telerik:RadAjaxLoadingPanel>
which existed, but wasn't being used at all.
When I did that, I got another new and exciting error:
RegisterForEventValidation can only be called during Render();
Using Google, I found out that one solution is to do this:
Add EnableEventValidation="false" to the Page directive.
Override the VerifyRenderingInServerForm in the code behind with nothing in it.
Did that, and now an even more newer and exciting error occurs.
Script control 'nameOfControl' is not a registered script control.
Script controls must be registered using RegisterScriptControl()
before calling RegisterScriptDescriptors().
The control in question is a RadNumericTextBox displayed within a table.
So, either I'm going deeper and deeper into a rabbit hole here, or I'm slowly working through multiple errors. I have no idea which it is.
Searching for this last error isn't giving me anything concrete to work with, though. Any additional help would be appreciated.
Make sure you do not have ClientIDMode set to Static for Telerik controls.
Then, if you use AJAX, wrap server code blocks in a RadCodeBlock tag (some server code blocks)
take away AjaxControlToolkit if you have it, it outputs changed MS AJAX scripts.
Don't use Response.Write() and the like if you use partial postbacks
If neither helps, strip down the page piece by piece to find the problem (e.g., some customization you have in place).
For me it helped to set the RegisterWithScriptManager="true".

Web Application Modular Design Practices

I have an existing web application built using ASP.NET 4.0 and C#. The application has an Administration page, that contains many different sections in which you can change different settings. I've attached screenshots for clarity.
Currently everything is contained to one page. Which means I have a massive amount of controls on just one page. I would like to have a navigation panel where you could select the "section" you want and it would load a central content area with that "section". I'm wondering what would be the best way to do this. Would it be best to make each section it's own control, or would I separate each section into a separate page?
I want to make sure I go about it the right way so that it will be easy to maintain and scale.
I appreciate any help.
I would definitely recommend using controls for each piece. The nice thing about controls is that if you ever need to use them in a completely different part of your application you are good to go!
As for loading in separate controls you can simply use query string variables. For instance let's say you have an anchor tag like this:
Business Hours
Now we can build a single page: /admin/index.aspx and on the page load we can check the query string. Now depending on that query string we can then load the appropriate control.
Reading from a query string: http://triaslama.wordpress.com/2008/04/12/retrieving-query-string-values-in-aspnet-and-javascript/
Loading User control via code behind: http://www.codeproject.com/Articles/1939/Programatically-include-an-ASP-NET-UserControl-in

Dynamically creating asp.net with c# pages

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.

Form POST in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5. The page has several forms on it for several different things.
In ASP.NET, there's a server form control wrapping the entire page, and form controls don't work within a server form control, and you can't have more than one server form control on a page.
So in order to keep this functionality, I can either:
Remove the server form control that's wrapping the page, and leaving the html forms on the page.
Create button click events for every form and perform the POST in the code-behind.
What's the preferred method here?
I wonder if converting to vanilla asp.net (aka webforms) is a bad idea here. Personally I'd go to MVC instead - allows multiple forms etc, and the views are much closer to he HTML, a lot like ASP.
I guess I'm saying there are some glitches vanilla asp.net introduces that you don't have to suffer.
I would go with the second option, any button click is going to post the whole page back anyway so you're not saving any bandwidth. Simply handle each button appropriately.
Check the answer I provided to a similar question here :-)
How to get past embedding a html form for paypal buttons asp.net
If you're going to use different button clicks, you still need to use this override to disable the non-related buttons in each handler, otherwise it won't work. You can only have one form tag at a time - this way you can toggle/disable the ones you're not using as appropriate.
Better still, refactor your application to use a single form. While MVC would be a closer match to the model you're using right now, it wouldn't make sense to go that route unless you were experienced enough with it; Web Forms is an easier jump.

Asp.Net Wizard in ASPxCallBackPanel control

Im using ASPxPopUpControll in which I have ASPxCallBack panel. THis CallbackPanel was embeded there because I wanted to have solution based on callbacks despite of reloading page each time.
In this CallBackPanel I've embeded asp:Wizard control.
What I want to achive is get rid of postbacks after clicking next previous etc buttons in this wizzard.
Any hints?
Maybe there is other way to create nice wizard without any postbacks ?
thanks for help
The ASPxCallbackPanel cannot intercept postbacks and "convert" them to callbacks as the MS UpdatePanel does this. So, a possible solution is to replace the ASPxCallbackPanel with the MS UpdatePanel and use the Wizard inside it. One more solution is to use the ASPxPageControl, position all required controls in its Pages and manage them manually.
I would recommend looking into ajax and jquery for asynchronous postbacks, that way you don't get a page refresh and you would only need to update a smaller part of the ui.

Categories