[RESOLVED] [2005] invalid client id
i am trying to associate "onfocusin" of a textbox ( txt_day ) to a function ( javascript code ) .
the code works fine if the text box is used directly on a page.
but if textbox is used in a template column of gridview , javascript error is generated.
reason for that is the html id of control returned by txt_day.clientid is not same as main tag itself.
main tage generated is :
ctl00_ContentPlaceHolder1_DocAttributeValues_GvDocAttributes_ctl04_DC_AttributeValue_txt_day
id returned by txt_day.clientid is :
ctl00_ContentPlaceHolder1_DocAttributeValues_GvDocAttributes_ctl02_DC_AttributeValue_txt_day
below is the complete code :
Code:
txt_day.Attributes.Add("onfocusin", "gotfocus()")
Dim StrScript As String
StrScript = "<script language=javascript>"
StrScript = StrScript & "function gotfocus()"
StrScript = StrScript & "{"
StrScript = StrScript & "if (document.getElementById('" & txt_day.ClientID & "').value=='day')"
StrScript = StrScript & "{"
StrScript = StrScript & "document.getElementById('" & txt_day.ClientID & "').value='';"
StrScript = StrScript & "}"
StrScript = StrScript & "}"
StrScript = StrScript & "</script>"
Page.RegisterStartupScript("gotfocus", StrScript)
Re: [2005] invalid client id
Hi,
I´m not 100% certain about this but I have the feeling that you make have to modify your javascript for when you use it on the GridView.
Make your javascript function accept a parameter, which will correspond to the obj, your textbox, which will be firing the event.
Something like:
Pass the object raising the event as a parameter.
Code:
txt_day.Attributes.Add("onfocusin", "gotfocus(this)")
Get the id of the object which raised the event.
Code:
Dim StrScript As String
StrScript = "<script language=javascript>"
StrScript = StrScript & "function gotfocus(obj)"
StrScript = StrScript & "{"
StrScript = StrScript & "if (document.getElementById('" & obj.ClientID & "').value=='day')"
StrScript = StrScript & "{"
StrScript = StrScript & "document.getElementById('" & obj.ClientID & "').value='';"
StrScript = StrScript & "}"
StrScript = StrScript & "}"
StrScript = StrScript & "</script>"
Not sure whether this part of the code may need to be changed as well.
Code:
Page.RegisterStartupScript("gotfocus", StrScript)
I haven´t tried this out my self!
Re: [2005] invalid client id
thanks Rauland .
for the parameter idea. it worked.
obj will be passed by the client script , not by the server
so instead of :
Code:
StrScript = StrScript & "if (document.getElementById('" & obj.ClientID & "').value=='day')"
we have to use
Code:
StrScript = StrScript & "if (document.getElementById(obj.id).value=='day')"
complete code :
Code:
txt_day.Attributes.Add("onfocusin", "gotfocus(this)")
Dim StrScript As String
StrScript = "<script language=javascript>"
StrScript = StrScript & "function gotfocus(obj)"
StrScript = StrScript & "{"
StrScript = StrScript & "if (document.getElementById(obj.id).value=='day')"
StrScript = StrScript & "{"
StrScript = StrScript & "document.getElementById(obj.id).value='';"
StrScript = StrScript & "}"
StrScript = StrScript & "}"
StrScript = StrScript & "</script>"
Page.RegisterStartupScript("gotfocus", StrScript)
Re: [RESOLVED] [2005] invalid client id
You're using document.getElementById, passing the object instance's ID, to get the object's ID. Does that sound ... funny?
Code:
function gotfocus(obj) {
if (obj.value == "day") {
obj.value = "";
}
}
Re: [RESOLVED] [2005] invalid client id
Quote:
Originally Posted by axion_sa
You're using document.getElementById, passing the object instance's ID, to get the object's ID. Does that sound ... funny?
Code:
function gotfocus(obj) {
if (obj.value == "day") {
obj.value = "";
}
}
Your right!
Re: [RESOLVED] [2005] invalid client id
Quote:
Originally Posted by axion_sa
You're using document.getElementById, passing the object instance's ID, to get the object's ID. Does that sound ... funny?
Code:
function gotfocus(obj) {
if (obj.value == "day") {
obj.value = "";
}
}
ya it is .
i just realised that.:)
Code:
txt_day.Attributes.Add("onfocusin", "gotfocus(this)")
Dim StrScript As String
StrScript = "<script language=javascript>"
StrScript = StrScript & "function gotfocus(obj)"
StrScript = StrScript & "{"
StrScript = StrScript & "if (obj.value=='day')"
StrScript = StrScript & "{"
StrScript = StrScript & "obj.value='';"
StrScript = StrScript & "}"
StrScript = StrScript & "}"
StrScript = StrScript & "</script>"
Page.RegisterStartupScript("gotfocus", StrScript)