asp.net dynamically generates checkbox of data sources as DB or memory collections
- 2020-07-21 07:26:13
- OfStack
Before looking at a lot of online, in fact, there are 1 is similar, I in this site 1 under the general solution to abandon Microsoft CheckBoxList
Requirements:
1 Dynamic generation of 1 set of checkbox(data source is DB or memory collection)
2post submission background can be timely access
3. After submitting, refresh the page checkbox to keep the original checked or unchecked state
4 Avoid generating large amounts of viewstate
Scheme: repeater+input(checkbox)+input(hidden)
html code
Note: Try to write the anonymous function code in OnClick in the header.
Js code
Background is obtained when post is submitted
String data will inevitably have empty strings. Pay attention to deletes, otherwise a cast exception will occur when unboxing
Requirements:
1 Dynamic generation of 1 set of checkbox(data source is DB or memory collection)
2post submission background can be timely access
3. After submitting, refresh the page checkbox to keep the original checked or unchecked state
4 Avoid generating large amounts of viewstate
Scheme: repeater+input(checkbox)+input(hidden)
html code
<asp:Repeater runat="server" ID="RPT_ReportType">
<ItemTemplate>
<input type="checkbox" value='<%#Eval("Id") %>' onclick="{var next = $(this).next();if($(this).attr('checked')){next.val($(this).val())}else{next.val('')}}" runat="server"/><%#Eval("TypeName")%>
<input type="hidden" name="reportType"/>
</ItemTemplate>
<AlternatingItemTemplate>
<input type="checkbox" value='<%#Eval("Id") %>' onclick="{var next = $(this).next();if($(this).attr('checked')){next.val($(this).val())}else{next.val('')}}" runat="server"/><%#Eval("TypeName")%>
<input type="hidden" name="reportType"/>
</AlternatingItemTemplate>
</asp:Repeater>
Note: Try to write the anonymous function code in OnClick in the header.
Js code
$(":hidden[name='reportType']").each(function () {
var obj = $(this).prev();
if (obj.attr('checked')){ $(this).val(obj.val()); }
});
Background is obtained when post is submitted
string[] _str = Request["reportType"].Split(',');
String data will inevitably have empty strings. Pay attention to deletes, otherwise a cast exception will occur when unboxing