Answered by:
Adding values to controls programmatically

Question
-
User-1791872305 posted
I have a form with several checkboxlists controls. The end game is: I want to be able to retrieve values from a table and populate each control. I created a simple form to test accessing controls and assigning values which works without a problem.
When I take that same exact code and place it in a form that uses a site.master form, I get a "Object reference not set to an instance of an object." "System.NullReferenceException: Object reference not set to an instance of an object."So best I can figure, my Site.Master form lacks something that a simple aspx web form added to the project contains. Does anything come to mind? I figure some reference to something but I haven't been able to crack this one. I'm about to the point where I'll just create a separate form and leave the site.master behind but I'd sure like to understand the difference.
The VB test code that works in one form but not the other is below.
Dim mName As String For x = 1 To 4 mName = "CheckBoxList" & x Dim mycontrol2 As CheckBoxList = FindControl(mName) mycontrol2.SelectedValue = x Next x
Wednesday, July 20, 2016 6:48 PM
Answers
-
User36583972 posted
Hi John Hill,
You can refer the following code.
HTML:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList2" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList3" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList4" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> </asp:Content>
ASPX.VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim mName As String For x = 1 To 4 mName = "CheckBoxList" & x Dim MainContent As ContentPlaceHolder = TryCast(Page.Master.FindControl("MainContent"), ContentPlaceHolder) Dim mycontrol2 As CheckBoxList = TryCast(MainContent.FindControl(mName), CheckBoxList) mycontrol2.SelectedValue = "Item " + x.ToString() Next x End If End Sub
Control ID Naming in Content Pages (C#):
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2016 6:46 AM
All replies
-
User1242245693 posted
Hi,
Can you please let me know whether control "CheckboxList" is in the same form where u r accessing it??
In case you want to set the value of control which is in other form then u have to pass the reference of the same , otherwise you wont get it.
Thursday, July 21, 2016 4:14 AM -
User36583972 posted
Hi John Hill,
You can refer the following code.
HTML:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList2" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList3" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> <asp:CheckBoxList ID="CheckBoxList4" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> </asp:CheckBoxList> </asp:Content>
ASPX.VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim mName As String For x = 1 To 4 mName = "CheckBoxList" & x Dim MainContent As ContentPlaceHolder = TryCast(Page.Master.FindControl("MainContent"), ContentPlaceHolder) Dim mycontrol2 As CheckBoxList = TryCast(MainContent.FindControl(mName), CheckBoxList) mycontrol2.SelectedValue = "Item " + x.ToString() Next x End If End Sub
Control ID Naming in Content Pages (C#):
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2016 6:46 AM