Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Attributes.Add doesn't work w/ buttons and onclick

Author  Topic 

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-03 : 13:52:04
I created an ASP web control button on my web form and I did the following line of code:

btnSave.Attributes.Add("onclick", "blur();")

The problem is that when ASP.Net creates the HTML control on the client side there is already and onclick event (for validation) and this overrides the functionality of my custom onclick event. Is there a way I can prepend additional attributes to the onclick event in this case?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-03 : 14:54:24
The question is -- do you still want the postback or do you want your event to fire BEFORE (and in addition to) the post back?
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-03 : 15:26:27
Before and in addition to the post back
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-03 : 15:32:25
I did some googling and saw this page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformseventmodel.asp

which makes me think you are doing things correctly .. At what point are you executing that piece of code? On the page_load() event, or at another time? What exactly happens?
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-03 : 15:44:26
It creates a second onclick event in the resulting HTML. It has the onclick event to process the postback and then the onclick to process blur().

The problem is it processes the postback onclick event first and never gets to the blur.

I'll read that article and see if I can find anything helpful
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-03-01 : 15:25:03
jsmith8858: I read that article and I found the answer to my solution. When using and HtmlInputButton you cannot end your onclick statement in a semicolon.

My code was:
onclick="blur();"

That did not work, but the following code does work:
onclick="blur()"

Such minor little details can be so frustrating at times.
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-03-01 : 15:33:56
Just use external Javascript. Theres no need to have it inline.


if(document.getElementById)
window.load = load;

function load()
{
if(document.getElementById("<your button id>") != null)
document.getElementById("<your button id>").onclick = someFunction;
}

function someFunction()
{
//javascript to call when button is clicked.
}

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-01 : 15:57:11
quote:
Originally posted by Billkamm
Such minor little details can be so frustrating at times.



Welcome to the world of computer programming ... it's all about the details. The computer has a nasty habit of doing exactly what we tell it to do, not what we *want* it to do.

Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-03-01 : 16:06:54
DustinMichaels: I'm not seeing the benefit of typing in all that extra code when you can just include inline onclick="somefunction()"

What is the benefit of adding the onclick attribute dynamically on window load?
Go to Top of Page
   

- Advertisement -