Usercontrol findcontrol

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
3/9/2011 1:21:59 PM
Gravatar
Total Posts 31

Usercontrol findcontrol

Hi,

I have a problem with finding a control inside a formview in my usercontrol.
I always get a null reference exception.

complete this task?

I have a usercontrol with the following controls

<portal:mojoPanel ID="mp1" runat="server" ArtisteerCssClass="art-Post" RenderArtisteerBlockContentDivs="true">
<asp:Panel ID="pnlWrapper" runat="server" CssClass="art-Post-inner panelwrapper GuestBook">
  <portal:ModuleTitleControl EditText="Edit" EditUrl="~/GuestBook/Backend/GuestbookEdit.aspx"
   runat="server" ID="TitleControl" />
  <portal:mojoPanel ID="MojoPanel1" runat="server" ArtisteerCssClass="art-PostContent">
     <div id="content">
     <asp:FormView ID="fvWerkgever" runat="server" DataKeyNames="Oid"
      DataSourceID="xpWerkgever" DefaultMode="Insert" ondatabinding="fvWerkgever_DataBinding"
                        oninit="fvWerkgever_Init">
      <InsertItemTemplate>
      <asp:DropDownList ID="cbxBranche" ClientIDMode="Static" runat="server"></asp:DropDownList>
     </InsertItemTemplate>
     </asp:FormView>
     <dx:XpoDataSource ID="xpWerkgever" runat="server"
      TypeName="AenV.Benefitsplein.Business.CRM.Werkgever">
     </dx:XpoDataSource>
   </portal:mojoPanel>
</asp:Panel>
</portal:mojoPanel>

I would like to add items on runtime to the cbxBranche (pageload)

        protected void Page_Load(object sender, EventArgs e)
        {
           
            moduleSettings = ModuleSettings.GetModuleSettings(this.ModuleId);
            LoadSettings();
            PopulateLabels();
            PopulateControls();
            //if (moduleSettings["Captcha"].Equals("false"))
            if (!IsPostBack)
            {
                setComboBoxen()
            }
        }

        private void setComboBoxen()
        {
            DropDownList dl = fvWerkgever.FindControl("cbxBranche") as DropDownList;
            ListItem item = new ListItem("Nieuw", "-1");           
            dl.Items.Add(item);
        }

How can i find this control?

Greetings Patrick

3/10/2011 9:24:22 AM
Gravatar
Total Posts 18439

Re: Usercontrol findcontrol

FormView is a databound control. Child controls inside an itemtemplate are repeated controls so they don't have global ids like unrepeated controls. You can find them by id but only inside events that fire on the databound control.

I don't use FormView anywhere in mojoPortal so I have no example for you, but it is similar in concept with gridview and other databound controls.

If you look in source code in Web/Admin/ModuleDefinitionSettings.aspx.cs you can see how I find controls inside the Row_Updating event like this:

TextBox txtSortOrder = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtSortOrder");

Hope it helps,

Joe

 

3/21/2011 2:04:38 AM
Gravatar
Total Posts 31

Re: Usercontrol findcontrol

Yoe, Thanks

 

Works fine!

You must sign in to post in the forums. This thread is closed to new posts.