Solved

Finding Textbox in form view

Posted on 2009-03-31
13
435 Views
Last Modified: 2013-11-05
Hi,
    Am trying to find a Textbox in javascript named "TextBox1" in a formview "FormView1" so I can custom validate it, am using the following code but to no avail, I just recieve.

BC30451: Name 'TextBox1' is not declared.

Can anyone help on this, also if there's any good info on this subect out there somewhere, it's triped me up a few times now.

Thanks


<script type="text/javascript">

    function TestVal(source, args)

        {

        if (document.getElementById('<%=TextBox1.ClientID%>').value == '')

            {

            args.IsValid = false

            }

        }

                </script>

Open in new window

0
Comment
Question by:Higgster
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24029585
do this in your function:
alert('<%=TextBox1.ClientID%>');
see what you get, then have a look in your page source where the textbpx should be, and this may give a better idea as to what is going on
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24029612
You can also "View source" to see the exact name given to your control.
0
 

Author Comment

by:Higgster
ID: 24029706
Thanks,

    The source code gives me a id="FormView1_TextBox1"

    I put that in place of 'TextBox1' but recieved an obect expected error
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24029832
please send how you defined the textbox.. :)
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24029920
I am asking this since it may be that the declaration of the textbox itseld is malformed
0
 

Author Comment

by:Higgster
ID: 24029923
Here's all of it!

Thanks.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript">

                    function TestVal(source, args)

                        

                        {

                        if (document.getElementById('TextBox1.ClientID').value == '')

                            {

                            args.IsValid = false

                            }

                        }

                </script>
 

</head>

<body>

    <form id="form1" runat="server">

    <div style="height: 356px">

    

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

            SelectCommand="SELECT * FROM [tblComments]"></asp:SqlDataSource>

        <br />

    

        <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 

            DataSourceID="SqlDataSource1" Height="211px" Width="385px">

            <EditItemTemplate>

                ID:

                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />

                <br />

                JobNo:

                <asp:TextBox ID="JobNoTextBox" runat="server" Text='<%# Bind("JobNo") %>' />

                <br />

                UserName:

                <asp:TextBox ID="UserNameTextBox" runat="server" 

                    Text='<%# Bind("UserName") %>' />

                <br />

                DateTimeStamp:

                <asp:TextBox ID="DateTimeStampTextBox" runat="server" 

                    Text='<%# Bind("DateTimeStamp") %>' />

                <br />

                Comment:

                <asp:TextBox ID="CommentTextBox" runat="server" Text='<%# Bind("Comment") %>' />

                <br />

                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 

                    CommandName="Update" Text="Update" />

                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 

                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />

            </EditItemTemplate>

            <InsertItemTemplate>

                JobNo:

                <asp:TextBox ID="JobNoTextBox" runat="server" Text='<%# Bind("JobNo") %>' />

                <br />

                UserName:

                <asp:TextBox ID="UserNameTextBox" runat="server" 

                    Text='<%# Bind("UserName") %>' />

                <br />

                DateTimeStamp:

                <asp:TextBox ID="DateTimeStampTextBox" runat="server" 

                    Text='<%# Bind("DateTimeStamp") %>' />

                <br />

                Comment:

                <asp:TextBox ID="CommentTextBox" runat="server" Text='<%# Bind("Comment") %>' />

                <br />

                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 

                    CommandName="Insert" Text="Insert" />

                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 

                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />

            </InsertItemTemplate>

            <ItemTemplate>

                ID:

                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />

                <br />

                JobNo:

                <asp:Label ID="JobNoLabel" runat="server" Text='<%# Bind("JobNo") %>' />

                <br />

                UserName:

                <asp:Label ID="UserNameLabel" runat="server" Text='<%# Bind("UserName") %>' />

                <br />

                DateTimeStamp:

                <asp:Label ID="DateTimeStampLabel" runat="server" 

                    Text='<%# Bind("DateTimeStamp") %>' />

                <br />

                <br />

                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Comment") %>'></asp:TextBox>

                <asp:Button ID="Button1" runat="server" Text="Button" />

                <asp:CustomValidator ID="CustomValidator1" runat="server" 

                    ClientValidationFunction="TestVal" ControlToValidate="TextBox1" 

                    ErrorMessage="CustomValidator" ValidateEmptyText="True">CustomValidator</asp:CustomValidator>

            </ItemTemplate>

        </asp:FormView>

    

    </div>

    </form>

</body>

</html>

Open in new window

0
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.

 
LVL 23

Expert Comment

by:apresto
ID: 24030211
Try and reference it like this:
((TextBox)FormView1.FindControl("Textbox1")).ClientId
0
 

Author Comment

by:Higgster
ID: 24030487
Thanks,

    where exactly do I put that, sorry Java's not that great.

0
 
LVL 23

Expert Comment

by:apresto
ID: 24030714
No need to apologise, i was once a beginner too.
Also, try and put that line in an alert() too and see what it comes out with. I havent tested this yet, but this is the syntax to cast as textbox and retrieve a control from a formview. The reason you cant access it, is because the control is nested within the formview

<script type="text/javascript">

    function TestVal(source, args)

        {

        if (document.getElementById('<%=((TextBox)FormView1.FindControl("Textbox1")).ClientId%>').value == '')

            {

            args.IsValid = false

            }

        }

</script>

Open in new window

0
 

Author Comment

by:Higgster
ID: 24030801
OK, tried that. This time I wind up with:

    Compilation Error
    BC30108: 'TextBox' is a type and cannot be used as an expression.

cheers.
0
 
LVL 4

Accepted Solution

by:
amazingwolf1 earned 400 total points
ID: 24036912
Try this:

function TestVal(source, args)
        {
        if (document.getElementById('<%=FormView1.FindControl("Textbox1").ClientId%>').value == '')
            {
            args.IsValid = false
            }
        }
0
 

Author Closing Comment

by:Higgster
ID: 31564856
Thanks amazingwolf1, that works a treat.

    All I have to do now is workout how to find a control thats in a formview that's in a contentplaceholder!!

Thanks All
0
 

Expert Comment

by:mbertuol
ID: 34770808
I have tried that too.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <script type="text/javascript">
        document.getElementById("<%=divMain.ClientID%>").style.height = 400 + 'px';
    </script>

I still get the same error.
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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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