Solved

javascript to get formview control

Posted on 2009-07-13
21
3,835 Views
Last Modified: 2012-05-07
How do i use javascript to get the value of a formview control? Below is my code and the error I get back is that the object is null.
<script language="javascript" type="text/javascript">
function ValidateForm()
{
	//var form = document.getElementById('form1.FormView5');
	
var formServiceDate = document.getElementById('<%= FormView5.FindControl("txtbxServiceDate").ClientID %>');

Open in new window

0
Comment
Question by:fwstealer
[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
  • 10
  • 6
  • 4
21 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24840833
assuming FormView5 is your formview's name,

var form = document.getElementById('<%=FormView5.ClientId%>');

Note: ID in ClientId should NOT be in all CAPS
0
 

Author Comment

by:fwstealer
ID: 24840896
System.Web.UI.WebControls.FormView' does not contain a definition for 'ClientId' and no extension method 'ClientId' accepting a first argument of type 'System.Web.UI.WebControls.FormView' could be found (are you missing a using directive or an assembly reference?)

formview5 is inside the parent form

<form id="form1" name="form1" runat="server">
<asp:FormView ID="FormView5"

0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24840974
Is this not working? - document.getElementById("<%=txtbxServiceDate.ClientId%>");
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:fwstealer
ID: 24840983
changed it to;

      var form = document.getElementById('<%=FormView5.ClientID%>');

it allowed me to load the page.

now when i test for the javascript with an empty textbox value i get:
Microsoft JScript runtime error: 'txtbxServiceDate.value' is null or not an object

       if(form.txtbxServiceDate.value != "") //blows up here and this textbox is in formview5
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24841023
As I indicated in my earlier post, document.getElementById("<%=txtbxServiceDate.ClientId%>").value will get you what you want
0
 

Author Comment

by:fwstealer
ID: 24841191
yes, it is not working inside a formview.

<script language="javascript" type="text/javascript">
function ValidateForm()
{
    var form = document.getElementById('<%=FormView5.ClientID%>');

    //validate txtbxServiceDate
       if(document.getElementById("<%=txtbxServiceDate.ClientID%>").value != "")//errors here

error:  The name 'txtbxServiceDate' does not exist in the current context

if i use this
if(form.txtbxServiceDate.value != "") //errors here

my error is: Microsoft JScript runtime error: 'txtbxServiceDate.value' is null or not an object
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24841431
Do you use any templates inside the formview?
0
 

Author Comment

by:fwstealer
ID: 24841590
dang, yes the -- EditItemTemplate

here is what the 1st textbox looks like:

        <asp:TextBox ID="ServiceDateTextBox" runat="server" Text='<%# Bind("ServiceDate") %>' />
0
 

Author Comment

by:fwstealer
ID: 24841901
i tried this:

       if(document.getElementById('<%=FormView5.FindControl( "txtbxServiceDate").ClientID%>'))

and got the error: The name 'txtbxServiceDate' does not exist in the current context

Ahhh

0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24841981
0
 

Author Comment

by:fwstealer
ID: 24842087
Instead, you should be retrieving your controls from PreRender event handler.  Be sure to check for your FormView's CurrentMode to make sure it's in FormViewMode.Edit mode before retrieving any controls within your EditItemTemplate.


<asp:FormView ID="FormView5" runat="server" DataKeyNames="ReqId"
            DataSourceID="SqlDataSource6" DefaultMode="Edit"
            ondatabound="FormView5_DataBound" name="FormView5" >

this is getting confusing!!
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24842417
First off: "ClientId" is WRONG! when you are using the Response.Write() short-hand "<%= %>" the code inside is .Net code. It is NOT javascript code. Therefore "ClientID" is correct.

Secondly, there are several things to remember about a FormView. These are specially designed .Net controls. They allow things to exist that don't follow the norm. For example, there are three templates (insert, edit, item) and they can all have a control with the same ID.

<asp:FormView>
<EditTemplate>
   <asp:TextBox ID="MyText"/>
</EditTemplate>

<ItemTemplate>
   <asp:Label ID="MyText"/>
</ItemTemplate>
</asp:FormView>

In this example, the Edit and Item templates both have a control with ID of "MyText" but the Update template does not. This means when you do a FindControl when it is in Update mode, you will not pull back a control, rather it will be NULL.

Because of this, you will need to MAKE SURE you are in the template you *think* you are in when you are executing the javascript. I would recommend adding <b>TemplateType</b> (where TemplateType is "Edit" or "Item" respectively). That will help distinguish.

Since it appears you are writing the javascript in the code-in-front you don't need to worry about when it is added to the page compared to when the control is added.

Next I would try adding the following to the page and see what you get.

<%= FormView5.ToString() %> <!-- should be System.Web...FormView -->
<%= FormView5.FindControl("txtbxServiceDate").ToString() %> <!-- Should be System.Web...TextBox -->
<%= FormView5.FindControl("txtbxServiceDate").ClientID.ToString() %> <!-- Should be the client id something like xxxx_xxxx_txtbxServiceDate" -->

If none of these return anything like mentioned in the comments, I would recommend that you check the spelling and CASE of the ID. For example "TxtBxServiceDate" is different from "txtbxServiceDate".

If none of these ideas pan out, let me know.
0
 

Author Comment

by:fwstealer
ID: 24842562
here is what I got

<body >
<%= FormView5.ToString() %> <!-- should be System.Web...FormView -->
System.Web.UI.WebControls.FormView

nullreferenceexception
<%= 'FormView5.FindControl("txtbxServiceDate").ToString() %>
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24842709
The null reference exception means that the control with ID="txtbxServiceDate" does NOT exist in the currently active template. That is why the javascript you have is not working. Until you can get the right template to be active with the correct ID for the control, it will not work.

Add this to your page to be able to tell what template the FormView is using:

<%= FormView5.CurrentMode.ToString() %>

That will tell you what template you are using. At which point, add the control you need to to it and your javascript will work.
0
 

Author Comment

by:fwstealer
ID: 24843379
its Edit

Any idea how to get into it?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24848329
can you post the EditTempalte?
0
 

Author Comment

by:fwstealer
ID: 24849149
all the code for it?
0
 

Author Comment

by:fwstealer
ID: 24849174
<asp:FormView ID="FormView5" runat="server" DataKeyNames="ReqId"
            DataSourceID="SqlDataSource6" DefaultMode="Edit"
            ondatabound="FormView5_DataBound" name="FormView5" >   
<EditItemTemplate>
<table cellpadding="0" cellspacing="0" class="style1"
    style="background-color:#EFF3FB;border-color:#B5C7DE;border-width:1px;border-style:Solid;border-collapse:collapse;">
            <tr>
            <td align="center" style="color:White;background-color:#507CD1;font-size:0.9em;font-weight:bold;" colspan="2">
            Requisition Core Data</td>
            </tr>
<tr>
  <td width="152" >
                <asp:Label ID="ReqIdLabel1" runat="server" Text='<%# Eval("ReqId") %>' Visible="false"  />
          * Service Date:
          </td>
<td width="546">
        <asp:TextBox ID="ServiceDateTextBox" runat="server" Text='<%# Bind("ServiceDate") %>' />
            </td>
            </tr>


other stuff

<tr>
            <td colspan="2">
            <asp:Button ID="btnAdd" runat="server" Text="Update Requisition" onclick="btnAdd_Click" OnClientClick="return ValidateForm()" />
            </td>
            </tr>
           </table>
    </EditItemTemplate>
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 24850061
I'm sorry if I sound a little rude here, but I really wish you would have read my first two comments, it would have saved a LOT of time.

From Comment 1:
If none of these return anything like mentioned in the comments, I would recommend that you check the spelling and CASE of the ID. For example "TxtBxServiceDate" is different from "txtbxServiceDate".

From Comment 2:
The null reference exception means that the control with ID="txtbxServiceDate" does NOT exist in the currently active template.

Putting it together:
Your EditTemplate does NOT contain a textbox (or any control for that matter) with the ID of "txtbxServiceDate". The ID of the text box (I assume) that you want to reference in your javascript is "ServiceDateTextBox". Remember this IS case sensitive and must be spelled correctly. Also keep in mind that this javascript will be dependent on you being in the correct template.
0

Featured Post

Industry Leaders: 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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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