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.

ASPDEVAsked:
Who is Participating?
 
rajapandian_81Connect With a Mentor Commented:
Try below one.

aspx.cs page code
------------------------------

public partial class Default5 : System.Web.UI.Page
{
    public string strTxtValue;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TextBox1.Text = "Something";
            strTxtValue = TextBox1.Text;
        }
        

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }

}

aspx page code
------------------
    function GetConfirmation()
    {
        if ("<%=strTxtValue%>" != document.getElementById('<%= TextBox1.ClientID %>').value)
            if (!confirm('Are you sure to change ID?'))
                return false;
       
        return true;
    }


<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return GetConfirmation()" onclick="Button1_Click" />

Open in new window

0
 
HainKurtSr. System AnalystCommented:
try this

function check() {
  var ac;
  ac = confirm("Warning. Changing  ID .");
  document.form.Hidtxt.value=ac;
}
0
 
the_billCommented:
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;                  
            }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ASPDEVAuthor Commented:
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.

0
 
the_billCommented:
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.
0
 
ASPDEVAuthor Commented:
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.

0
 
the_billCommented:
You'll need to return something in the check function,
in the click event, return check(), then in function check(), return ac.
0
 
rajapandian_81Commented:
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

0
 
ASPDEVAuthor Commented:
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.
0
 
ASPDEVAuthor Commented:
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.

0
 
rajapandian_81Commented:
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.
0
 
ASPDEVAuthor Commented:
The code you mentioned above works good, but I cannot updated records since I'm calling SaveRecords() in the Button click event.

0
 
rajapandian_81Commented:
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

0
 
ASPDEVAuthor Commented:
Thanks,

I followed your logic.
0
All Courses

From novice to tech pro — start learning today.