Reputation: 187
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>
Upvotes: 0
Views: 6477
Reputation: 460108
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.
Upvotes: 4
Reputation: 218827
This sounds indicative of populating the checkboxes in Page_Load. Is that the case? If you're populating the controls in Page_Load then you'll want to wrap it in a conditional:
if (!IsPostBack)
{
// populate your controls from data
}
Otherwise, they'll get re-populated with each postback. When you have an autopostback or click a button or perform some other action on the page which initiates a postback, Page_Load is called before the event handler. So in effect, this is happening:
(On a side note... Please look into using AJAX for dynamic client-server interaction like this. Autopostback makes for a poor user experience, and as you're discovering also makes for a difficult development experience.)
Upvotes: 1