asp.net - how to check selected item in Check Box List (generated from code) -


i have have asp.net / vb application need check on selected item in check box list checkbox dynamically code behind

<asp:panel id="panel1" runat="server">     <asp:button id="button1" runat="server" text="save" />     <asp:label id="lbl_selected_items" runat="server" text=""></asp:label> </asp:panel> 

code behind page load

dim checkboxlist new checkboxlist             pagerview.controls.add(checkboxlist)             ...est 

button1 click event

protected sub button1_click(byval sender object, byval e system.eventargs) handles button1.click dim checkboxlist1 checkboxlist = ctype(me.findcontrol("checkboxlist"), checkboxlist) dim scheckedvalue string = "" each oitem listitem in checkboxlist1.items     if oitem.selected         if scheckedvalue = ""             scheckedvalue = ("selected value : " + oitem.value & " selected text: ") + oitem.text         else             scheckedvalue += ("<br/>selected value : " + oitem.value & " selected text: ") + oitem.text         end if     end if next lbl_selected_items.text = scheckedvalue 

error text : null reference exception object reference not set instance of object – error

so happening? error? no result?

at point thing can recommend loading dynamic controls in page init instead of page load.

edit

you find when debug checkbox on first line of button click nothing. there 2 mistakes in code.

  1. when add control form, not provide id value. , not lookup value

  2. when lookup control in button click findcontrol looks in single container. not recursive. should calling pagerview.findcontrol

the page load (i still recommend init):

protected sub page_load(byval sender object, byval e eventargs) handles me.load     dim checkboxlist new checkboxlist     checkboxlist.id = "cbl1"     pagerview.controls.add(checkboxlist) end sub 

and button click:

protected sub button1_click(byval sender object, byval e system.eventargs) handles button1.click     dim checkboxlist1 checkboxlist = ctype(pagerview.findcontrol("cbl1"), checkboxlist)     dim scheckedvalue string = ""     each oitem listitem in checkboxlist1.items         if oitem.selected             if scheckedvalue = ""                 scheckedvalue = ("selected value : " + oitem.value & " selected text: ") + oitem.text             else                 scheckedvalue += ("<br/>selected value : " + oitem.value & " selected text: ") + oitem.text             end if         end if     next     lbl_selected_items.text = scheckedvalue end sub 

ms reference


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -