Link to home
Start Free TrialLog in
Avatar of ASPDEV
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.RegisterStartupScript(Me.GetType(), "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.

Avatar of HainKurt
HainKurt
Flag of Canada image

try this

function check() {
  var ac;
  ac = confirm("Warning. Changing  ID .");
  document.form.Hidtxt.value=ac;
}
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('Hidtxt').value=ac;                  
            }
Avatar of ASPDEV
ASPDEV

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.

move this
 Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "check();", True)

to the page load event from the button click event. should work first time then.
Avatar of ASPDEV

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.

You'll need to return something in the check function,
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" />

Open in new window

Avatar of ASPDEV

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.
ASKER CERTIFIED SOLUTION
Avatar of rajapandian_81
rajapandian_81
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ASPDEV

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.RegisterStartupScript(Me.GetType(), "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.

You are showing confirm message using code "Page.ClientScript.RegisterStartupScript".

Instead of that I am using OnClientClick event of button. No need of Page.ClientScript.RegisterStartupScript.

Are you facing any problem when using OnClientClick event of button instead of Page.ClientScript.RegisterStartupScript.
Avatar of ASPDEV

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();
    }

Open in new window

Avatar of ASPDEV

ASKER

Thanks,

I followed your logic.