views:

122

answers:

3

This seems super weird to me. I have a callback handler done in VB which works fine with this code:

<!-- Div Outside Form -->
<div class="container">
<form id="querydata" runat="server">
<asp:DropDownList runat="server" ID="myddl" AutoPostBack="true" OnSelectedIndexChanged="myddlhandler">
<asp:ListItem>Hello</asp:ListItem>
<asp:ListItem>Goodbye</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" ID="label1"></asp:Label>
</form>
</div>
<!-- Yep, they're matching -->

I can change the value and everything is A-OK, but if I change the code to this (div inside form):

<form id="querydata" runat="server">
<!-- Div inside form doesn't work :( --> 
<div class="container">
<asp:DropDownList runat="server" ID="myddl" AutoPostBack="true" OnSelectedIndexChanged="myddlhandler">
<asp:ListItem>Hello</asp:ListItem>
<asp:ListItem>Goodbye</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" ID="label1"></asp:Label>
</div>
</form>

It the postback no longer works. Is how asp is supposed to work? Or is it some magic error that only works for me? And most importantly, if asp is not supposed to work this way, how should I be doing this?

Thanks!

A: 

Well it turns out I was wrong - it wasn't the div that was giving me problems it was other non-ASP form elements.

Bad:

<div>
    <form runat="server">
       <asp:DropDownList runat="server" autopostback="true" onselectedindexchanged="myhandlername">
         <asp:ListItem>One</asp:ListItem>
         <asp:ListItem>Two</asp:ListItem>
      </asp:DropDownList>
      <div>
         <input type="text" id="mytext" />
      </div>
    </form>
</div>

Good:

<div>
    <form runat="server">
       <asp:DropDownList runat="server" autopostback="true" onselectedindexchanged="myhandlername">
         <asp:ListItem>One</asp:ListItem>
         <asp:ListItem>Two</asp:ListItem>
      </asp:DropDownList>
      <div>
         <asp:TextBox runat="server" ID="mytext></asp:TextBox>
      </div>
    </form>
</div>

So apparently ASP form components when used with autopostback are incompatible with normal HTML form components.

Wayne Werner
A: 

The form should still postback when changing the dropdown selections, when mixing element types, but the values in the html elements won't be preserved, since they aren't 'covered' by viewstate.

Ed B
A: 

I don't know if it will make any difference, but I noticed you're not closing your <form> tag in your answer (which should really be an edit to your question if you still want an answer). Also, the "ASP form components" get rendered into "normal HTML form components" (with some JavaScript, etc.), so there is no reason they should be "incompatible".

Nelson
Ah, thanks - it really doesn't (usually) make a difference because HTML has really liberal acceptance of "correct" code. But that's bad form <pun intended>. I know that they do turn into "regular" code (view source, woo), which is why the thought never occurred to me. It wasn't until I was trying to create a little test example for Robin that I discovered that it was, indeed, having "normal" components that broke the postback. I have no clue why, and it doesn't make a whole lot of sense. As for the answer - I figure since I discovered the answer, I may as well post it as an answer.
Wayne Werner
@Wayne: I thought it was still a problem, but I see your point now. I'm surprised it behaves that way. I would have imagined that non-ASP form tags would not cause any problems unless you had Id conflicts. I suppose I had never tried it. You can have a regular <form> tag without runat="server" and you can place any HTML elements there. Of course you have to work with it separately, no ViewState, etc.
Nelson