I have a button called btnFailureReport. Once a javascript function was called, I want the text of the btnFailureReport can be changed. Please see the code below.
The problem is the "text" of the btnFailureReport was changed only for look. If I want to save the text to the database, I found the text is still the old text. So I put the two alert box (commented out) in the function to test. The result for the first
one (ctrl.value) is the "hello" which is right. But the result for the second one (ctrl.text) is "undefined", which means the "text" was not changed.
Does anybody knows how to fix this problem?
My javascript function is like this:
function GetFileName()
{
var ctrl =document.getElementById("ctl00_ContentPlaceHolder1_tcRMACAR_TabPanelCAR_tabPanelInsideCar_tpContainment_btnFailureReport");
//ctrl.value =document.getElementById("ctl00_ContentPlaceHolder1_tcRMACAR_TabPanelCAR_tabPanelInsideCar_tpContainment_FileUpload1").value;
ctrl.value = "hello";
ctrl.text = ctrl.value;
//alert(ctrl.value);
//alert(ctrl.Text);
}
Javscript runs only on the client side. To save the data to a database or whatever, you need a postback which does this on the server side. The setData is server side code, but you still need a postback.
As far as I know, In Javascript, document.getElementById("btnFailure").value returns the text which is displayed on the Button.
So If u want to change the text of the button in javascript upon clicking of the button, Just use document.getElementById("btnFailure").value to set the text u want to display and using __EVENTTARGET and __EVENTARGUMENT, change the text of button while
saving into DB
After the text on the button was changed by the javascript function, I viewed the source. And found on the source view, the value of the button is same as before, it did not change! How's that?[:'(]
mabellez
Member
169 Points
313 Posts
how to change a button's text in javascript
Sep 04, 2008 04:50 PM|LINK
I have a button called btnFailureReport. Once a javascript function was called, I want the text of the btnFailureReport can be changed. Please see the code below.
The problem is the "text" of the btnFailureReport was changed only for look. If I want to save the text to the database, I found the text is still the old text. So I put the two alert box (commented out) in the function to test. The result for the first one (ctrl.value) is the "hello" which is right. But the result for the second one (ctrl.text) is "undefined", which means the "text" was not changed.
Does anybody knows how to fix this problem?
My javascript function is like this:
function GetFileName() { var ctrl =document.getElementById("ctl00_ContentPlaceHolder1_tcRMACAR_TabPanelCAR_tabPanelInsideCar_tpContainment_btnFailureReport"); //ctrl.value =document.getElementById("ctl00_ContentPlaceHolder1_tcRMACAR_TabPanelCAR_tabPanelInsideCar_tpContainment_FileUpload1").value; ctrl.value = "hello"; ctrl.text = ctrl.value; //alert(ctrl.value); //alert(ctrl.Text); }mabellez
Member
169 Points
313 Posts
Re: how to change a button's text in javascript
Sep 04, 2008 07:03 PM|LINK
I tried to put this code inside the GetFileName() function:
<%SetData("Hello"); }%>;
and add a function in C# code:
public void SetData(string txt)
{
btnFailureReport.Text = txt;
lbFailure.Visible = true;
lbFailure.Text = "txt: " + btnFailureReport.Text;
}
It does not run until I click on the button. Also, after I click on the button, lbFailure shows the right content.
So strange.
Damien12345
Member
76 Points
28 Posts
Re: how to change a button's text in javascript
Sep 04, 2008 07:22 PM|LINK
tchaitanya
Member
82 Points
25 Posts
Re: how to change a button's text in javascript
Sep 04, 2008 07:37 PM|LINK
Hi Friend,
As far as I know, In Javascript, document.getElementById("btnFailure").value returns the text which is displayed on the Button.
So If u want to change the text of the button in javascript upon clicking of the button, Just use document.getElementById("btnFailure").value to set the text u want to display and using __EVENTTARGET and __EVENTARGUMENT, change the text of button while saving into DB
mabellez
Member
169 Points
313 Posts
Re: how to change a button's text in javascript
Sep 04, 2008 07:52 PM|LINK
Not sure how to do that: using __EVENTTARGET and __EVENTARGUMENT, change the text of button while saving into DB
vjay
Contributor
2220 Points
500 Posts
Re: how to change a button's text in javascript
Sep 05, 2008 05:14 AM|LINK
just look over this article
http://msdn.microsoft.com/en-us/library/aa479011.aspx
TonyDong
Contributor
4777 Points
939 Posts
Re: how to change a button's text in javascript
Sep 05, 2008 05:37 AM|LINK
mabellez
Member
169 Points
313 Posts
Re: how to change a button's text in javascript
Sep 05, 2008 02:35 PM|LINK
Thank you! I'm not sure what do you mean by using Request.Form["__EVENTTARGET "] to get button name.
I know the button ID. it's btnFailureReport.
mabellez
Member
169 Points
313 Posts
Re: how to change a button's text in javascript
Sep 05, 2008 03:05 PM|LINK
After the text on the button was changed by the javascript function, I viewed the source. And found on the source view, the value of the button is same as before, it did not change! How's that?[:'(]
TonyDong
Contributor
4777 Points
939 Posts
Re: how to change a button's text in javascript
Sep 05, 2008 04:22 PM|LINK
If you want to save the text changes to the database, you could use ajax, it will get the new value you changed.
This video will show you how to use ajax
http://www.asp.net/learn/ajax-videos/video-79.aspx