Solved

Using FindControl in a FormView InsertItemTemplate

Posted on 2007-03-25
4
2,790 Views
Last Modified: 2012-06-27
Hi Experts,

I would like to pass a date selected from a calendar in a FormView InsertItemTemplate into SQL via a parameter.

In my FormView I have:
<InsertItemTemplate>
  <asp:Calendar ID="Calendar1" runat="server" />
</InsertItemTemplate>

And the DataSource I have:
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
           ...
            InsertCommand="INSERT INTO [tblGT] ([GT]) VALUES (@GTDate)">
                 <InsertParameters>
                  <asp:ControlParameter ControlID="Calendar1" Name="GTDate" PropertyName="SelectedDate" Type="DateTime" />
            </InsertParameters>
        </asp:SqlDataSource>

To which I receive the following error: Could not find control 'Calendar1' in ControlParameter 'GTDate'
How do use FindControl() in this scenario to pass the date as the parameter.

I'm a newbie, so any ideas re the best way to approach this?

Thanks-in-advance
Regards
Rob
0
Comment
Question by:rwarcup
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 500 total points
ID: 18789810
Hello rwarcup,

Calendar1 control isn't visible for SqlDataSource because it nested in formview template. Sou you can't reference to it properties in SqlDataSource parameters.
You can solve the problem by using binding mechanism. The "GTDate" declared in Calendar1 will bind to specified parameter in SqlDataSource.

In my FormView you need to change declaratrion:
<InsertItemTemplate>
  <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%#  Bind("GTDate") %>'/>
</InsertItemTemplate>

And in SqlDataSource too:

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
           ...
            InsertCommand="INSERT INTO [tblGT] ([GT]) VALUES (@GTDate)">
                 <InsertParameters>
                  <asp:Parameter Name="GTDate" Type="DateTime" />
            </InsertParameters>
        </asp:SqlDataSource>
0
 
LVL 9

Expert Comment

by:noulouk
ID: 18789813
Hi rwarcup,
Try this:
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        Calendar Calendar1 = (Calendar)FormView1.Row.FindControl("Calendar1");
        // Validate your control

       e.Values["GTDate"] = Calendar1.SelectedDate;
    }
Regards.
0
 

Author Comment

by:rwarcup
ID: 18791251
Thanks igor_alpha & noulouk ... both these solutions work.
Regards Rob
0
 
LVL 9

Expert Comment

by:noulouk
ID: 18791738
You can split points if you want to grade me a little.
;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What .NET website keeps me current? 9 52
Achieve json result 2 64
ASP.net Javascript use multiple sliders 2 16
Where can I ask about my ASP.NET MVC project? 6 16
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now