tags:

views:

340

answers:

6

I have a web application that has a page that loads the content from the database. I want to be able to put a form in the dynamic content, but .net doesn't let the inside form perform it's action. Is there a way to allow this or some other way I can get a form on a dynamic content page?

Thanks.

--EDIT--

I think I need to clarify something. This is an aspx page that loads content from the database. As far as I know, the text I pull from the db and stick in the Label is never compiled or processed by the .net wp, thus I can't use the code behind to fix this issue.

+1  A: 

Not with webforms, no. You have to work within the one, full page form by using an event handler connected to a Button to LinkButton. Fortunately, it's pretty easy to do:

foo.aspx:

...
<asp:TextBox id="txtFoo" runat="server" />
<asp:Button id="btnFoo" runat="server" onclick="btnFoo_Click />
...

foo.aspx.cs:

...
protected void btnFoo_Click(object sender, EventArgs e)
{
    string s = txtFoo.Text;
    // do something with s
}
...
tghw
+2  A: 

There can only be one form on the page (the asp form); you have to use that form somehow.

To clarify, there can only be one form processed.

Joe Philllips
stupid, stupid, webforms
annakata
A: 

you either have to deal with the postback by adding a server side click event handler to what you want to be the "sub forms" submit button (this is how web formas deals with multiple submit type buutons on the same page) or do soemthing clever with AJAX if you dont want a full post back

Pharabus
A: 

I've run across this issue before. One workaround that I have done is to place my code that I want my action to be done upon inside of an asp:Panel. With the panel you can set the attribute of "DefaultButton" to a button inside of the panel, and clicking the button (or pressing "enter") will fire that button's click event. I've found this quite handy when wanting to submit a "form" by pressing enter when I have a master page that contains the only allowable asp:Form.

Hope this helps.

Aaron
+1  A: 

This is a common problem, when you want to have a non-postback form to a 3rd party site (like a PayPal button, for example).

The problem occurs because HTML doesn't let you have form within a form, and most ASP.NET pages have a <form runat="server" /> "high up" in the HTML (or in the Master page).

My favorite solution is to hide the "high up" form tag, while still showing all of the content. Then you can feel free to dump any tags you want in the body. If you do this dynamically you can choose on a page-by-page basis which pages have custom forms.

I created a class called GhostForm.cs to handle this. You can read all about it here:

http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html

JerSchneid
+1  A: 

Dino Esposito has an article from MSDN magazine that covers handling multiple forms or "simulating" sub forms in ASP.Net that might just answer all your questions.

http://msdn.microsoft.com/en-us/magazine/cc164151.aspx

Josh