Solved

Using FindControl in a FormView InsertItemTemplate

Posted on 2007-03-25
4
2,781 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

22 Experts available now in Live!

Get 1:1 Help Now