1

I want to set some of my controls in a FormView manually and some from a SQL Data Source.

Here is the snippet of my code:

 <asp:FormView runat="server" DataSourceID="ds_Sco_Post_Reference_Data" ID="frmview1">
        <ItemTemplate>
                <div class="customer-details-inception-date">
                        <label class="applicationfont">Inception Date:</label>
                        <asp:TextBox CssClass="smallinputbox" runat="server" ID="txt_customer_details_inception_date" Text='<%# Bind("InceptionDate") %>' />
                </div>
                <div class="customer-details-renewal-date">
                        <label class="applicationfont">Renewal Date:</label>
                        <input class="smallinputbox" runat="server" id="txt_customer_details_renewal_date" readonly="readonly" />
                </div>
                <div class="customer-details-date-complient-logged">
                        <label class="applicationfont">Date Complient Logged:</label>
                        <asp:TextBox CssClass="smallinputbox" runat="server" id="txt_customer_details_date_complient_logged" />
                </div>
        </ItemTemplate>
</asp:FormView>

I want to be able to find the control txt_customer_details_date_complient_logged and have tried alot of different C# code.

Here is an example that does not work:

FormView cph = (FormView)this.FindControl("frmview1");
TextBox t = ((TextBox)cph.FindControl("txt_customer_details_date_complient_logged"));
t.Text = DateTime.Now.ToString("d");

Does anyone know why I cant get this to work?

Jonathan
  • 5,495
  • 4
  • 38
  • 53
Ben Clarke
  • 256
  • 1
  • 6
  • 23
  • asp.net has a tendency of prefixing the controls. To see if the control is still same name when the app is running, you must view source in the browser and search for the control name. – Bayeni Jul 16 '14 at 09:54
  • Have you tried: TextBox t = (TextBox)frmview1.FindControl("txt_customer_details_date_complient_logged"); – Kinyanjui Kamau Jul 16 '14 at 09:56
  • @Bayeni 'ctl04$txt_customer_details_renewal_date' is the name that is appearing in source. – Ben Clarke Jul 16 '14 at 09:58
  • @KinyanjuiKamau Yes t is returning NuLL. – Ben Clarke Jul 16 '14 at 10:00
  • @user3129331 did you make sure that your databinding is working/your datasource is not null? If there are no results/if the control is not bound yet, this textbox won't be rendered. To test it you can do an emptyItemTemplate and bind the source - FormView.findControl should work fine. – therak Jul 16 '14 at 10:30
  • @therak I am not databinding to this control i want to set it manually. – Ben Clarke Jul 16 '14 at 10:32
  • @user3129331 ok - but even though you don't wanna databind, a form list view won't render its ItemTemplate/EmptyItemTemplate if there are no items/ no databinding. you will never find the Control because it won't be rendered. you can still call FormView.DataBind even without a datasource. Just test it with EmptyItemTemplate, you will see. – therak Jul 16 '14 at 10:51
  • Can you check this link http://stackoverflow.com/questions/2171427/formview-findcontrol-returns-null-until-databind – Lakpa Sherpa Jul 16 '14 at 19:37

0 Answers0