Search code examples
asp.netautopostback

Asp.NET dropdownlist autopostback


I have a dropdownlist with autopostback enabled. and have a repeater where i populate checkboxes from database.

If i set the autopostback to true then when selecting a value checkboxes lose its value...

Any workarounds on this?

Here is the code :

<asp:DropDownList ID="dropdown" runat="server"  class="pop" AutoPostBack="true" >
</asp:DropDownList>

<asp:Repeater ID="rptD" runat="server" >
        <ItemTemplate>
        <td valign="top" >
        <input type="checkbox" class="al" />
        </ItemTemplate>
        </asp:Repeater>

Solution

  • I assume this is because you are DataBinding the Repeater not only if(!IsPostBack) but also on postbacks. Therefore the checked state will be overriden.

    So do this in Page_Load(assuming C#):

    if(!IsPostBack){
       DataBindRepeater();
    }
    

    Whereas DataBindRepeater is a method that sets the DataSource property and DataBind the Repeater.

    You might also want to use an ASP.NET Checkbox control instead of the html input type="checkbox". The checked state is reloaded only if it's a server WebControl that implements IPostBackDataHandler.