Click to See Complete Forum and Search --> : .Net newbie having fun with multiple forms...


afranklin
02-19-2008, 12:10 PM
Hi,

I am trying to make an aspx page with a form to display a datagrid and also have a small usercontrol dropdownlist in the header to change to different pages.

I scratched my head for a while over not being able to have multiple forms on a page but finally decided a standard html form in the usercontrol would be fine.

I have a simple html dropdownlist in the usercontrol which when submitted runs a javascript function which changes the page... or is supposed to, but I can't get it to work!

I've tried changing the action location of the html form and tried using location.href and window.location to change the page but it just isn't shifting!

I am also getting a whole heap of viewstate info in the final source which makes me think I am being a doughnut for missing something obviously...

Any suggestions much appreciated...
Especially if they relate to the problem above!

Thanks,
a.

Cstick
02-25-2008, 07:39 PM
Even though it may feel odd, do everything in one form. I felt the same way when I first started using ASP.Net and you will find out eventually that there really doesn't need to be multiple forms at all.

In case you hadn't figured it out already, dropdownlists have an attribute "AutoPostBack" which when set to True will automatically submit the form using Javascript. From your codebehind, you simply have to handle the DropDownList.SelectedIndexChanged event and redirect the response.

afranklin
02-26-2008, 07:24 AM
Hi,

Thanks very much for your help!
I am still a bit confused... I want to use UserControls as I would vbscript includes to be on every page and handle thinks like header areas with search forms and the like. I don't see how I can have a single .net form on a page where the main part of the page needs a form that also takes care of the functionality required for the user UserControls?

I've read a few forum posts saying use a single form but that doesn't make sense if you want to use UserControls with forms?!

The book I've bought avoids this issue and I haven't found much on the web?

I will have another look at your suggestion but any further light you can shed on this would be gratefully received...
Thanks again.
a.

Cstick
02-27-2008, 07:35 PM
UserControls and MasterPages would both be of interest to you if you have some functionality that you want to have on every page of a website.

Basically, the <body> tag will be directly followed by a <form runat="server"> tag for every single page. Your UserControls will be inside of the form tag. It is even possible to have UserControls with no form elements in their HTML placed outside of the <body> tag, form elements would raise an exception.

Here's an example:

<html>
<head>
<!-- Head stuff -->
</head>
<body>
<form id="MyForm" runat="server">
<div id="Intro">
<cc:searchcontrol runat="server" />
</div>
<div id="Content">

</div>
<div id="Outro">
</div>
</form>
</body>

Here is an example of the possible html for the search UserControl.

<asp:textbox id="QueryTextbox" runat="server"></asp:textbox>
<asp:button id="SearchButton" runat="server" text="Search" />

The reason you don't need multiple forms is because you can handle the events of each control individually. There is really no need for additional forms. UserControls are very convenient since they allow you to split bits of functionality away from the rest of the page and they raise most of the same events that the Page class does.

afranklin
03-05-2008, 11:21 AM
Thank you very much for your help!!
I get it!