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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.