问题
Its driving me bit crazy on how to do a PostBack for CheckBoxList.
I have an autocomplete textbox that triggers the Javascript (on an item selected) by which I'm adding items to checkboxlist dynamically using Javascript appendChild element from the resource:
https://stackoverflow.com/a/3184505/1426121
But as told by the user in the answer, the added item disappears the very next moment, for which I have to do a PostBack (suggested by them). Also as in the comment for the answer, I need to have a *static listitem in the CheckBoxList for that Javascript to work. So here I'm summarizing the question.
Am I going in the right way to generate a CheckBoxList item? Or is there any other way?
If no other go,
- how can I do the postback for the above code?
- how can I get rid of the *static listitem? Or is there any workaround for hiding a single static listitem from a set of dynamically generated listitems?
回答1:
I was so dumb for searching on SO for this silly question. Here is the answer.
"Or is there any other way?"
Instead of appending a CheckBoxList Item with javascript,
- create a contentplaceholder where you need to have the checkboxlist
- store the autocomplete textbox value in ontextchanged event.
- invoke a method for generating a checkboxlist and pass the listitem attributes.
Sample code:
private void AddingDynamicCheckBoxList(string listitem_name, string listitem_value)
{ chkBxLst1.Items.Add(new ListItem(listitem_name, listitem_value)); }
来源:https://stackoverflow.com/questions/12408134/do-a-postback-and-add-the-items-server-side-for-dynamically-created-checkboxlist