0

I have a masterpage in which i defined a function for getElementById .

function Clear() {
    document.getElementById('<%= NameTextBox.ClientID  %>').value = "";
    document.getElementById('<%= LastNameTextBox.ClientID  %>').value = "";
}

when i use this function in other page it is clearing the element value.

but when i use this code in masterpage, i get error :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
 System.Web.UI.ControlCollection.Add(Control child) +9601391
AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) +691
AjaxControlToolkit.ScriptControlBase.OnLoad(EventArgs e) +44
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

if i change my code to :

document.getElementById('<%# NameTextBox.ClientID  %>').value = "";

i don't get error. but don't clear myelement.

Niloo
  • 1,205
  • 5
  • 29
  • 53

1 Answers1

0

Looks like you have added mentioned js function in <Head> tag of master page. Instead try adding your js function in <form> tag in master page at end.

Amit Thakkar
  • 356
  • 2
  • 6