开发者

ASP.NET dropdownlist callback doesn't work inside div

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" r开发者_高级运维unat="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!


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.


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".


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜