Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using FindControl in a FormView InsertItemTemplate

Posted on 2007-03-25
4
Medium Priority
?
2,872 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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