ASPDEV
asked on
calling Javascript in codebehind using Page.ClientScript.RegisterStartupScript in ASP.NET 2.0
Hello,
I'm callling Javascript from Codebehind in an Button Click Event.
Page.ClientScript.Register StartupScr ipt(Me.Get Type(), "alert", "check();", True)
JS:
function check()
{
var ac=confirm("Warning. Changing ID .");
Hidtxt.value=ac;
}
Here Hidtxt is an hidden variable And I get value of Hidtxt as ""' where, I should either get True or False , so my condition fails.
And it should be in button click event, with the condition I should do my necessary operation.
I'm callling Javascript from Codebehind in an Button Click Event.
Page.ClientScript.Register
JS:
function check()
{
var ac=confirm("Warning. Changing ID .");
Hidtxt.value=ac;
}
Here Hidtxt is an hidden variable And I get value of Hidtxt as ""' where, I should either get True or False , so my condition fails.
And it should be in button click event, with the condition I should do my necessary operation.
You should ensure your hidden field has an ID attribute, to ensure it works the same in all browsers
function check()
{
var ac=confirm("Warning. Changing ID .");
document.getElementById('H idtxt').va lue=ac;
}
function check()
{
var ac=confirm("Warning. Changing ID .");
document.getElementById('H
}
ASKER
the Bill,
It works but what happens is I have to hit the button twice(Reload the page), to do my operation.I get the .Hidtxt value as true or false when I click second time.
It works but what happens is I have to hit the button twice(Reload the page), to do my operation.I get the .Hidtxt value as true or false when I click second time.
move this
Page.ClientScript.Register StartupScr ipt(Me.Get Type(), "alert", "check();", True)
to the page load event from the button click event. should work first time then.
Page.ClientScript.Register
to the page load event from the button click event. should work first time then.
ASKER
Here is the task:
I have a textbox in a page,it holds Id which is being populated on page load.Whenever user try to do change value in the textbox and click on save then it should alert with the Warning.I got it working, when I did in page load. But the page reloads and again I have to click on the save button.
I need to work, like when user click Save button, it has to ask to whether to change or not, if yes then it should update in Database, else nothing.
I have a textbox in a page,it holds Id which is being populated on page load.Whenever user try to do change value in the textbox and click on save then it should alert with the Warning.I got it working, when I did in page load. But the page reloads and again I have to click on the save button.
I need to work, like when user click Save button, it has to ask to whether to change or not, if yes then it should update in Database, else nothing.
You'll need to return something in the check function,
in the click event, return check(), then in function check(), return ac.
in the click event, return check(), then in function check(), return ac.
Use onclientclick event of button. If user click ok, code in Button1_Click event will get executed. If user click cancel, nothing happens.
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirm('Are you sure to change ID?')" onclick="Button1_Click" />
ASKER
Well the code you given is working, but I'm missing Logic.
The Java script should fire only when the value is being changed in the text box,if its different then it should fire , if not it should save without any Alert message.Right now its doing every click on the button.
The Java script should fire only when the value is being changed in the text box,if its different then it should fire , if not it should save without any Alert message.Right now its doing every click on the button.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rajapandian 81,
I appreciate for your help, as I tried your code still it page reloads:
I want the JS to run in CodeBehind of the ButtonClick event.
Page.ClientScript.Register StartupScr ipt(Me.Get Type(), "alert", "check();", True)
function check()
{
var ac=confirm("Warning. Changing Vehicle ID may result in incorrect vehicle reporting.");
document.getElementById('< % =Hidtxt.ClientId %>').value = ac;
}
Where Check() function should execute and get me the value of Hidtxt i, so that I check the condition again in the same event before its completed.
I tried couple of different ways above mention, the page reloads and when I click again, then I get the value into the Hidtxt.
I appreciate for your help, as I tried your code still it page reloads:
I want the JS to run in CodeBehind of the ButtonClick event.
Page.ClientScript.Register
function check()
{
var ac=confirm("Warning. Changing Vehicle ID may result in incorrect vehicle reporting.");
document.getElementById('<
}
Where Check() function should execute and get me the value of Hidtxt i, so that I check the condition again in the same event before its completed.
I tried couple of different ways above mention, the page reloads and when I click again, then I get the value into the Hidtxt.
You are showing confirm message using code "Page.ClientScript.Registe rStartupSc ript".
Instead of that I am using OnClientClick event of button. No need of Page.ClientScript.Register StartupScr ipt.
Are you facing any problem when using OnClientClick event of button instead of Page.ClientScript.Register StartupScr ipt.
Instead of that I am using OnClientClick event of button. No need of Page.ClientScript.Register
Are you facing any problem when using OnClientClick event of button instead of Page.ClientScript.Register
ASKER
The code you mentioned above works good, but I cannot updated records since I'm calling SaveRecords() in the Button click event.
You can call SaveRecords() inside Button1_Click like below. Whats the problem with this.
protected void Button1_Click(object sender, EventArgs e)
{
SaveRecords();
}
ASKER
Thanks,
I followed your logic.
I followed your logic.
function check() {
var ac;
ac = confirm("Warning. Changing ID .");
document.form.Hidtxt.value
}