Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

ASP.Net forms: general philosophy

I have a rather basic question about ASP.Net. Let's say you
have a book search form that allows the user to specify
the subject, year published, keywords, etc., then
click the button and see the books that were found.
Since an ASPX form can only post to itself, what's the
conventional and recommended approach to this ?
Should I:

1. In the button click event on the server, do a redirect
to another page (with the datagrid and everything) passing
along the user input in a query string ? What if there
are dozens of fields ?

2. Forgo the aspx form and use regular html form with action set to the aspx file with the datagrid ?

3. Have one aspx form that will have "two faces": if it's
not a postback, display the input controls; otherwise,
show the books in the grid.

Thanks,

- Mike.

Mike
Tuesday, February 11, 2003

Well. Looking at the MS examples I tend to see floating around, number 3 usually wins. Not sure I like that. The jury is still out where my opinion is concerned. Sorry that's not much help is it? :(

Andrew Cherry
Tuesday, February 11, 2003

That's exactly why I decided to ask around - to see what
people in the field are doing. All examples I've seen in the
books are basic data forms that don't expect any submission, or maybe just a couple of params in the query string. 

#3 looks like an overkill to me;
#2 would be kind of against the spirit of ASPX; besides, what if I want to use ASP validators ?
#1 would make the most sense, but it seems somewhat
stupid to be copying and re-submitting already submitted values.

Mike
Tuesday, February 11, 2003

This article looks interesting. I haven't tried it yet, but the ability to pass data between forms in an object oriented manner seems like a good pratice.

  http://aspnet.4guysfromrolla.com/articles/050802-1.aspx 

Bill K
Tuesday, February 11, 2003

3 is really easy, clean, and even understandable to HTML people.  Just use panels. Panels are your friends!  They make life so clean.  Try it once and you'll be hooked!

Vincent Marquez
Tuesday, February 11, 2003

Thanks for that Vincent, using panels in that way is something I would of never considered.

Got anymore tips?

Stephen Moody
Wednesday, February 12, 2003

To Bill K. - the link doesn't seem to work.

To Vincent: I am really ashamed of myself, but what are panels ?

Mike
Wednesday, February 12, 2003

<asp:Panel runat=server id=myPanel>
  ... some html, some controls, whatever
  <asp:Button id=myButton runat=server .... />
</asp:Panel>

In code behind:
void myButton_Click(....) {
  // do search
  ...
  // Hide panel
  myPanel.Visible=false;
}

Duncan Smart
Wednesday, February 12, 2003

or even:

<asp:Panel Visible=<%# !IsPostBack %> runat=server id=myPanel>
  ...
</asp:Panel>

In code behind:
void Page_Load(....) {
  ...
  DataBind();
}

Duncan Smart
Wednesday, February 12, 2003

Hmmm, don't know why the link doesn't work, but the article still there, Transferring the Datagrid Data Between Web Forms  on that site. It basically shows how to expose properties and controls on a page so they can be read by another page. Again, I haven't really tried it, but it seems like it might be better then using query strings. I suspect it might be the same as using session variables, but in an OO way.

Bill K
Wednesday, February 12, 2003

I usually go with #3 (using asp:panels, of course -- a minor aside here is because of some Visual Studio weirdness, all my panels are visible by default; I only hide/show from code) unless I've got two very distinct pages and only a small amount of data to pass (one or two variables). In that case, I tend to just pass things along a query string.

Dave Rothgery
Wednesday, February 12, 2003

I use Panels as well, but I ran into a little problem with them recently.

I wanted to display/hide certain columns of a table header depending on the values I had in the database. It worked in IE, but was broken in Netscape.

After a big of investigating I found that Panels actually output a bit of HTML where the server tags are, the HTML they produce varies for different browsers. In IE, they will put some DIV tags around the code you want in your Panel. But in Netscape, they wrap a table around it, and since a cell was inside my Panel, the table broke the code.

Is there any reason why they have to produce these extra HTML tags when using DIV's?

Ben
Wednesday, February 12, 2003

If you don't want Netscape/Mozilla to be a "special case" when it comes to WebControl rendering then put this in your web.config:
  <browserCaps>
      tagwriter=System.Web.UI.HtmlTextWriter
  </browserCaps>

This makes sure HTML 4 gets rendered for the controls.
Yes a Panel is effectively a server-side <DIV>. So you will get a <DIV> rendered to the page (or a table of the Html32TextWriter tagwriter is selected by the browser capabilities). If this is a problem use <asp:PlaceHolder> instead

Duncan Smart
Thursday, February 13, 2003

Thanks Duncan, I'll have a look at that.

Ben
Thursday, February 13, 2003

3 is definitely the way to go.

The search is one panel, the datagrid is in another (I like the look of the FIELDSET LEGEND combo personally, but some don't).  When the search is clicked, the search gets hidden and the datagrid is shown.  This also makes it easier to have a "Revise Search" button, since you just set the Search panel visible and the search terms are still there exactly how the user entered.  If the Search box is compact, then you don't even need to hide it.  Just show the datagrid below it.

Things to keep in mind:

1) At least one of your users will always ask for the ability to sort the results by clicking on the headers.  Learn how to do this (see the quickstart tutorials at www.asp.net for examples).

2) If there are no results and you call DataBind on the grid anyways, the user will only see the headers.  I like to put a hidden label "No Results" message.  In the DataGrid's DataBinding event, I check to see if there are any results.  If there are none, the grid is hidden and the "No Results" message is shown.  If there are many, the grid is visible and the "No Results" message is hidden.  If there is exactly 1 results, you may want to redirect to a page for that specific result, depending on the particular application.

Richard Ponton
Wednesday, February 19, 2003

Don't do Visible=<%# !IsPostBack %>!!

It's a shortcut now, but it'll bite you later.  PostBacks do not necessarily mean the user clicked the button you thought they did.

Richard Ponton
Wednesday, February 19, 2003

Thanks guys, your replies were really helpful.

Mike
Friday, February 21, 2003

*  Recent Topics

*  Fog Creek Home