I have a ASP:TextBox control in my page. The event OnTextChanged by default is used to call a server side event. I want to define a javascript function on the client site and then the event will call the client side function to set another TextBox control's
text instead.
Here are some codes in Page_Load() event to register client side javascript:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" +
"document.getElementById('MonitorChangeControl').Text = 'Some values may have been changed.'; }</script>";
// Add the JavaScript code to the page.
if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript);
}
Then I tried to add the following codes in my aspx page:
This server-side setting will bind your javascript method to the OnKeyUp event, which fires on the client any time a key is pressed while the textbox has focus.
But dswersky is correct, the correct way to do this is in the Page_Load handler of the CodeBehind file:
TextBox1.Attributes.Add("onchange", "alert('TextBox1 onchange fired');");
Thanks for your solution. That works with some changes in my codes:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" +
"document.getElementById('ctl00_ContentPlaceHolder_MonitorChangeControl').value = 'Some values may have been changed.'; }</script>";
// Add the JavaScript code to the page.
if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript);
}
The first one is the property name for element: it was Text, actually, value should be used. The textbox control is converted to an input element in the final asp page (I checked it by viewing the page source). The second one is the ID of the textbox control in my script. In my asp page or source codes, it is "MonitorChangeControl", however, when I check it by viewing the source, the ID is prefixed with "ctl00_ContentPlaceHolder_". I am not sure if this prefix will be different depending on the browser and time. How can I reference to the textbox by a correct ID in my script or source codes?
string changeScript = "<script language='javascript'> function SomeValueChanged() {" +
"document.getElementById('" + MonitorChangeControl.ClientID +
"').value = 'Some values may have been changed.'; }</script>";
// Add the JavaScript code to the page.
if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript);
}
ClientID will return the actual ID used by the control in asp page. It works fine.
Marked as answer by chudq on Aug 05, 2008 09:11 PM
Actually, if you cannot use parameter format in this case since there is "..{docu...}" in the string. If you prefer to use the similar format, you have to use replace like this:
string changeScript = (
"<script type=text/javascript>function SomeValueChanged() {" +
"document.getElementById('{0}').value = " +
"'Some values may have been changed.'; }</script>").Replace("{0}",
MonitorChangeControl.ClientID);
Could you help me in this please? I just need to make a panel visible and enable two validators the moment happens a client-side onchange event. The odd thing that I can figure it out is that the following just accepts javascript:
chudq
Member
7 Points
46 Posts
Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 04:33 PM|LINK
I have a ASP:TextBox control in my page. The event OnTextChanged by default is used to call a server side event. I want to define a javascript function on the client site and then the event will call the client side function to set another TextBox control's text instead.
Here are some codes in Page_Load() event to register client side javascript:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" + "document.getElementById('MonitorChangeControl').Text = 'Some values may have been changed.'; }</script>"; // Add the JavaScript code to the page. if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged")) { ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript); }Then I tried to add the following codes in my aspx page:
However, I got compiling error. How can I call my client side function for an ASP:TextBox control?
dswersky
Participant
1790 Points
295 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 05:59 PM|LINK
The standard ASP.NET TextBox does not have markup-settable client-side handlers. You have to add the handlers to the textbox programatically:
dailyValue.Attributes.Add("OnKeyUp", "return SomeValueChanged()");
This server-side setting will bind your javascript method to the OnKeyUp event, which fires on the client any time a key is pressed while the textbox has focus.
www.daveswersky.com
Follow me on Twitter
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 06:51 PM|LINK
The correct name for the client-side event is onchange and not OnTextChanged
Try:
<asp:TextBox ID="TextBox1" onchange="alert('TextBox1 onchange fired');" ...
But dswersky is correct, the correct way to do this is in the Page_Load handler of the CodeBehind file:
TextBox1.Attributes.Add("onchange", "alert('TextBox1 onchange fired');");
NC...
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 06:55 PM|LINK
<
asp:TextBox runat="server" ID='txt1' onchange='return myjs();'></asp:TextBox>don't worry about VS intellisense not picking it up.
Edit:....Sorry...above posts answer the question....damn my slow connection.
blog: www.heartysoft.com
twitter: @ashic
chudq
Member
7 Points
46 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 06:56 PM|LINK
Thanks for your solution. That works with some changes in my codes:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" + "document.getElementById('ctl00_ContentPlaceHolder_MonitorChangeControl').value = 'Some values may have been changed.'; }</script>"; // Add the JavaScript code to the page. if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged")) { ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript); }chudq
Member
7 Points
46 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 05, 2008 08:59 PM|LINK
I think I found a solution. Here is my codes:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" + "document.getElementById('" + MonitorChangeControl.ClientID + "').value = 'Some values may have been changed.'; }</script>"; // Add the JavaScript code to the page. if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged")) { ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript); }ClientID will return the actual ID used by the control in asp page. It works fine.
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 06, 2008 01:45 PM|LINK
Or
string changeScript = string.Format(
"<script type=text/javascript>function SomeValueChanged() {" +
"document.getElementById('{0}').value = " +
"'Some values may have been changed.'; }</script>",
MonitorChangeControl.ClientID);
NC...
chudq
Member
7 Points
46 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Aug 12, 2008 04:58 AM|LINK
string changeScript = ( "<script type=text/javascript>function SomeValueChanged() {" + "document.getElementById('{0}').value = " + "'Some values may have been changed.'; }</script>").Replace("{0}", MonitorChangeControl.ClientID);M_Sadeq
Member
2 Points
2 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Mar 01, 2010 01:00 PM|LINK
function SetFocus()
{
document.getElementById('<%= TextBox1.ClientID %>').focus();
}
<asp:TextBox ID="TextBox1" runat="server onchange="javascript:return SetFocus();"/ >
Acaspita
Member
5 Points
5 Posts
Re: Call client side javascript function for TextBox's OnTextChanged event
Mar 23, 2010 06:49 PM|LINK
Hi:
Could you help me in this please? I just need to make a panel visible and enable two validators the moment happens a client-side onchange event. The odd thing that I can figure it out is that the following just accepts javascript:
Pregunta.Attributes.Add("onchange", "return SomeValueChanged()");
But I'd like it to take cs since I need to enable these asp server controls...
How should I do this? To access these control properties from a client-side javascript event?
Thanks!:D
client-side onchange event