Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate checkbox dynamically by asp.net

Shulou Source: shulou.com Published: 2022-06-03 07:38:05 09月11日 Update

This article introduces the relevant knowledge of "how asp.net can dynamically generate checkbox". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Demand:

1 dynamically generate a set of checkbox (data source is DB or memory collection)

When the 2post is submitted, the backend can get it in time.

3 refresh page after submission checkbox remains previously selected or unchecked

4 avoid producing a large amount of viewstate

Solution: use repeater+input (checkbox) + input (hidden)

Html code

The copy code is as follows:

Note: try to write the anonymous function code in OnClick on the top of the page.

Js code

The copy code is as follows:

$(": Hidden [name = 'reportType']") .each (function () {

Var obj = $(this) .prev ()

If (obj.attr ('checked')) {$(this) .val (obj.val ());}

});

Get when the post is submitted by the backend

The copy code is as follows:

String [] _ str = Request ["reportType"] .split (',')

String data will inevitably have empty strings. Be careful to delete them, otherwise there will be a type conversion exception when unpacking.

This is the end of the content of "how to dynamically generate checkbox in asp.net". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Code dynamic content background characters strings data more knowledge practical inevitable learned next memory functions dilemmas actual situation data sources articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Shulou Information Shulou Tech Info Xiaomi