Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Add javascript to a button

  Asked By: Sienna    Date: Oct 08    Category: Asp.net    Views: 743
  

I have a form having a dropdown list containing a list of products
and 2 buttons - add and submit. When the user clicks on the add
button, it will add a row in my datagrid which is located right below
the buttons (similar to shopping cart). Of course, the datagrid
contains a delete and edit buttons. When the user is finished adding
the products into the datagrid (or done shopping), the user will
click on the submit button to finish the process. This will insert
all data in the datagrid into the table located in my database. What
I would like to do is to add a confirm message to the submit button.
For example, 'Are you sure you want to save the data?' with Yes/No
button. Can you please show me how achieve this task?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Myrna Brown     Answered On: Oct 08

This is very easy to accomplish
myButton.Attributes["onclick"] = "return confirm('Are you sure you want to save  the data?')";

This will cancel the click event if you press no. Pressing yes will potback the page.

 
Answer #2    Answered By: Reginald Thomas     Answered On: Oct 08

I am encountering a peculiar problem. On a button  click, I have added the following code.


btnNew.Attributes["onclick"] = "return alert('Great to meet you!')";

But the issue is when I click the nuttom the 1st time, I don't get any message  but on further clicks  I do get this message. Does anybody have any idea as to why this is happening?

 
Answer #3    Answered By: Seth Anderson     Answered On: Oct 08

If I get this right it's plain and simple answer. The first time the page gets loaded the button  doesn't have the onclick attribute because U add  it in the server on click. After that you have it because of EnabledViewState... So. Put your lone of code into Page_Load but only the fisrt time it loads (!isPostBack).

 
Answer #4    Answered By: Jeanne Lawson     Answered On: Oct 08

Ok. But check for subsequent postbacks. Maybe the attribute doesn't get stored into ViewState and you'' have to add  it eachtime you load the page. If so, don't check for !isPostBack and load it every time...

 
Didn't find what you were looking for? Find more on Add javascript to a button Or get search suggestion and latest updates.




Tagged: