Do a PostBack and add the items server-side for dy

2019-09-07 17:22发布

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.

  1. Am I going in the right way to generate a CheckBoxList item? Or is there any other way?

  2. 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条回答
对你真心纯属浪费
2楼-- · 2019-09-07 18:05

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)); }
查看更多
登录 后发表回答