Share via


button.attributes.add

Question

Sunday, August 7, 2005 5:35 PM

i need some help in understanding how

button1.attributes.add("onclick","function();") works

my form postback because of this function dont know why :(

All replies (9)

Sunday, August 7, 2005 6:23 PM

The button's postback will be blocked if function returns FALSE, otherwise clicking the button will cause a postback.

HTP
Shane Sukul
|Bsc|Mcsd.Net|Mcsd|Mcad|


Sunday, August 7, 2005 10:55 PM

can u help me ..i am still getting errors :(

function confirmbtn(){
var confirmed = window.confirm("change a record, would you like to continue ?");
 return(confirmed);
 }

and i am calling this function from code behind page ..but its not working correctly at first click ...the message appears at 2nd click ...
public sub btn_click(para1,para2)

btn.Attributes.Add("onclick", "return Confirmbtn();")
help needed urgent

 


Monday, August 8, 2005 12:30 AM

check whether you are placing the btn.Addributes.Add in a proper location
you have to place it within page load event
Also check that if you placed it in a if statement which will check the Page.IsPostBack property and executed only if page is post backed


Wednesday, March 26, 2008 1:10 AM

Hi,

     You can write the button.attributes in pageload event and ensure that it should not be inside the not postback event

 like this:-

page_load event()

{

button.attributes.add("onclick","return validate()");

       if(!page.ispostback())

       {

 

      }

 Regards

Kumaran. M

HCL Technologies

 

 

     

 

 


Wednesday, March 26, 2008 1:41 AM

Hi

can u help me ..i am still getting errors :(

function confirmbtn(){
var confirmed = window.confirm("change a record, would you like to continue ?");
 return(confirmed);
 }

and i am calling this function from code behind page ..but its not working correctly at first click ...the message appears at 2nd click ...
public sub btn_click(para1,para2)

btn.Attributes.Add("onclick", "return Confirmbtn();")
help needed urgent

 

 

Javascript is case sensitive and your declared a function with 

function confirmbtn(){

and you are calling it as

return Confirmbtn();")

.

Add it as shown below:

btn.Attributes.Add("onclick", "return confirmbtn();")

instead of writing a function and adding attributes, there is a easy way

you can call the confirmation in buttons onclientclick event. Ex.

 <asp:Button ID="Button1" runat="server" Style="z-index: 101; left: 216px; ;
          top: 160px" Text="Button" OnClientClick="return confirm('change a record, would you like to continue ?');" />

 


Monday, July 28, 2008 2:07 AM

I have used OnClick event, so the application was showing error. I tried with OnClientClick, it works fine.

 Thanks

P. Rajesh


Tuesday, November 30, 2010 2:04 AM

instead of writing a function and adding attributes, there is a easy way

you can call the confirmation in buttons onclientclick event. Ex.

 <asp:Button ID="Button1" runat="server" Style="z-index: 101; left: 216px; ;
          top: 160px" Text="Button" OnClientClick="return confirm('change a record, would you like to continue ?');" />

Are there attributes in the button properties you need to change to not get post back?

I have a simple page with two text boxes and want the value of textbox 1 to appear in text box 2 when the user clicks the button, it works, but the page reloads when I click the button.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="JavaExample.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer" 
            onclientclick="DefaultValue()" />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    
    </div>
    </form>

    <script type="text/javascript">
        function DefaultValue() {
            var txt1 = document.getElementById("TextBox1");
            var txt2 = document.getElementById("TextBox2");
            txt2.value = txt1.value;
        }
    </script>
</body>
</html>

EDIT:

Well, sometimes you have to ask the question before you can find the answer.... tried a different search and found it.

I used a second HTML button and called the function on click and then added a "return false;" to my script and

Button1.Attributes.Add("onclick", "return DefaultValue();") to the page load sub.

Both methods seem to work fine and don't cause the page to reload... but why do you need to add the "return" before "DefaultValue();"?

*
*


Saturday, November 26, 2011 2:28 AM

hi friend

if you wanna stop post back. so simple at the end return false from your javascript function like this it will work

function yourfunction(){

return false;

}

button1.attributes.add("onclick","return function();") ;


Saturday, November 26, 2011 6:34 AM

Did you have a look at this article:

http://aspalliance.com/829_Disabling_the_Postback_Button