views:

407

answers:

1

I need to get textbox that exits in Formview inserttemplate via javascript.

Both return null:

$get('txtTitle');
document.getElementById("txtTitle");

The problem is that formview is not rendered on form load...

A: 

As you stated, the formview contents are rendered on the server upon request rather than on page load. That said, try this code to access controls in the formview. Change the name of 'FormView' to match your unique control ID.

document.getElementById('<%=FormView.FindControl("txtTitle").ClientID%>');

If needed, Here are a few useful events you can use to register the javascript in the code behind if there are lifecycle considerations.

The ItemCreated event is raised after all rows are created in a FormView control. This can occur when the control is first rendered, or when the user navigates to another record. You can use this event to provide an event-handling method that performs a custom routine, such as adding to or modifying the contents of a row, whenever this event occurs.

Note: The ItemCreated event occurs before the FormView control is bound to data. To modify the value of a bound field, use the DataBound event.

PortageMonkey
when I run the application i get an error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
dani
Hmm., this error happens only if my javascript code in header section, if i move it to body it works. why is that?
dani
The difference here is that <%= %> expressions are embedded into the ASP.NET output as part of the generated Parse Tree class. if you really want it in page head, an alternative might be to try <%# %> expression instead, as it is embedded at runtime. I have no way to test this for you, but it could work.
PortageMonkey