Do a PostBack and add the items server-side for dynamically created checkboxlist item

自闭症网瘾萝莉.ら 提交于 2019-12-11 17:49:29

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!