locked
find control RRS feed

  • Question

  • User-125499312 posted

    i am having an issue to find controls from my code behind

    here is my code

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID1" runat="server" CssClass="BodyText2" style="width:180px;" />

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID2" runat="server" CssClass="BodyText2" style="width:180px;" />

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID3" runat="server" CssClass="BodyText2" style="width:180px;" />

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID4" runat="server" CssClass="BodyText2" style="width:180px;" />

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID5" runat="server" CssClass="BodyText2" style="width:180px;" />

    <asp:DropDownList ID="ddlGiftRegistryVendorAssociatedItemID6" runat="server" CssClass="BodyText2" style="width:180px;" />

    dim strGiftRegistryVendorAssociatedItemIDS  as string

    For i = 1 To 6
    strGiftRegistryVendorAssociatedItemIDS += CType(Me.Page.FindControl("ddlGiftRegistryVendorAssociatedItemID" + i.ToString), DropDownList).SelectedValue
    Next

    i am getting object refernce not set to an instane of an object

    thx for ur help

    Tuesday, April 16, 2019 2:23 PM

All replies

  • User288213138 posted

    Hi  yzidell,
     
      According to your description, I tried to reproduce your problem and found that I could find control according to the following method.


    The code:

    For i = 1 To 4
        Dim list As DropDownList = CType(Me.Page.FindControl("DropDownList" & i.ToString()), DropDownList)
        Response.Write(list.SelectedValue)
    Next
    

    The Result:
     
    Best Regard,
    Sam

    Wednesday, April 17, 2019 6:48 AM
  • User3690988 posted

    If you are using a Master Page, then you have to find your controls in the ContentPlaceHolder.  For example, if your DDLs are enclosed in:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    Then find your DDLs within the ContentPlaceHolder:

    Dim strGiftRegistryVendorAssociatedItemIDS As String = String.Empty
    Dim contnt As ContentPlaceHolder = DirectCast(Master.FindControl("MainContent"), ContentPlaceHolder)
    For i = 1 To 6
      strGiftRegistryVendorAssociatedItemIDS += CType(contnt.FindControl("ddlGiftRegistryVendorAssociatedItemID" + i.ToString), DropDownList).SelectedValue & " "
    Next

    Monday, April 22, 2019 1:48 PM