Solved

Using FindControl in a FormView InsertItemTemplate

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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