Solved

Using FindControl in a FormView InsertItemTemplate

Posted on 2007-03-25
4
2,824 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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