Xerratus
Happily stressed out, since 1974
Tuesday, November 21, 2006
<< AH HAH... Found it!
Setting a cell to NULL when editing data in SQL within a tableview >>
How to get ASP.NET 2.0 Button WebControl to render as input type="button"
While working an ASP.NET 2.0 project today I ran across a problem with the OnClientClick event within the Button WebControl. The problem is that .NET renders this button:
<
asp
:
Button
ID
="btnAddChoice"
runat
="server"
Text
="Add"
OnClientClick
="AddToListBox(lbxChoices, txtChoiceName);"
/>
as
<
input
type
="submit"
name
="btnAddChoice"
value
="Add"
onclick
="AddToListBox(lbxChoices, txtChoiceName);"
/>
So when I click on the button, my JavaScript fires correctly but the page then submits -not what I want it to do. Thinking fast, I added a snippet to return false after my client-script ran, like so:
<
input
type
="submit"
name
="btnAddChoice"
value
="Add"
onclick
="AddToListBox(lbxChoices, txtChoiceName);return false;"
/>
This however, does not work; the page still submits. After looking at the rendered HTML I can see the problem right away -the button type is submit. What I need is for it to be a type of button.
An input control of type button, will not automatically submit the form. But how do I get .NET to render one?
What I do not want to to is capture it just before it renders and change it there. And I also do not want to try to manipulate the type attribute after it's already been rendered. The solution I found; .NET has a added an attribute (boolean) to the Button WebControl called UseSubmitBehavior. By setting it to false in the WebControl, like so:
<
asp
:
Button
ID
="btnAddChoice"
runat
="server"
Text
="Add"
OnClientClick
="AddToListBox(lbxChoices, txtChoiceName); return false;"
UseSubmitBehavior
="false"
/>
Renders the input control like I want it to, as a button:
<
input
type
="button"
name
="btnAddChoice"
value
="Add"
onclick
="AddToListBox(lbxChoices, txtChoiceName); return false;__doPostBack('btnAddChoice','')"
/>
Notice that I still need to stop the execution of the JavaScript by returning false because .NET added __doPostBack('btnAddChoice','') to the onclick event.
But the main difference is that by returning false the page
DOES NOT
submit. Now, I can go back to writing my client-script to manipulate the page and capture those changes when the real submit button is clicked and the server code takes over.
Tuesday, November 21, 2006 @ 06:06 PM (-08:00) Pacific Standard Time
Comments (7)
tags:
Programming
admin: Edit | Remove
Thursday, November 23, 2006 11:06:04 AM (Pacific Standard Time, UTC-08:00)
I think there're 2 ways to go here.
1. If your functionality makes sense ONLY when JavaScript is enabled, then you should create that button on the client side, and yes, make it a button.
2. If you depend on that functionality (and I think this is your case here), you should not expect JavaScript. Make it work even when it's not enabled. So, the button here has to be a submit button. There will be no __doPostBack here, and your first try would almost be the way to go, except that, instead of returning false, which doesn't prevent the click event to propagate, you have to set the returnValue of the intrinsic event to false, something like:
event.returnValue = false; // IE
// and/or
event.preventDefault(); // FF
This means more work, every Add/Remove will be made server side without JS and client side with JS, and you have to code all of that, so, as always, it depends on your project.
Adi
Sunday, November 26, 2006 5:31:37 PM (Pacific Standard Time, UTC-08:00)
Actually, I DO NOT want the form to submit AT ALL when this button is clicked. The action I want the button to do it solely client-side (add the contents of a textbox to a listbox). There is another button below that submits the form to the server. I could have easily created an input control without .NET and wired up my JavaScript that way. But I wanted to use the .NET control to keep things consistent.
This is also part of a company intranet application so we can safely assume browser and JavaScript usage.
So doing this all server-side is actually not a route we'd like to go with this piece of the application because posting back too many times is slow and clunky.
John McGuinness
Tuesday, November 28, 2006 6:25:26 AM (Pacific Standard Time, UTC-08:00)
Nice hidden property, but is there any reason the button needs to exist server-side at all? I would have just used a standard html button there.
Wyatt Barnett
Tuesday, November 28, 2006 5:18:09 PM (Pacific Standard Time, UTC-08:00)
You're absolutely right Wyatt but for consistencies sake, I wanted to use a server control.
John McGuinness
Thursday, July 26, 2007 12:49:16 PM (Pacific Standard Time, UTC-08:00)
There is a "UseSubmitBehavour" property of asp:Button, just set it to false and it will render as <input type="button">
Andrey
Thursday, July 26, 2007 5:02:47 PM (Pacific Standard Time, UTC-08:00)
Andrey, not sure what to tell you but the post basically describes just that; how to use the "UseSubmitBehavior" to get the input type equal to button -you just have to read the entire thing first.
At first glance, it looks like I just switched and used the input tag but that was just a test to see why my Javascript wasn't working. Once I got that figured out, I searched and found the UseSubmitBehavior functionality.
John McGuinness
Friday, February 08, 2008 6:24:19 AM (Pacific Standard Time, UTC-08:00)
Ah...very nice. I am doing exactly what you are...and have been beating my head for a couple of days. return false; saved the day :)
As a side note: the reason why I want it a .NET button is I want to make it visible/hidden...which I control from the DLL.
Cheers.
v
Name
E-mail
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
Site Navigation
About Me
Calendar
Disclaimer
Site Search
Sponsored Links
Calendar
<
November 2008
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
Monthly Archives
August, 2008 (1)
July, 2008 (1)
March, 2008 (2)
January, 2008 (3)
December, 2007 (3)
October, 2007 (1)
September, 2007 (1)
August, 2007 (5)
July, 2007 (5)
June, 2007 (3)
April, 2007 (5)
February, 2007 (2)
January, 2007 (2)
December, 2006 (9)
November, 2006 (15)
October, 2006 (19)
September, 2006 (3)
August, 2006 (5)
July, 2006 (4)
June, 2006 (6)
May, 2006 (12)
April, 2006 (20)
March, 2006 (11)
February, 2006 (14)
January, 2006 (14)
December, 2005 (23)
November, 2005 (23)
October, 2005 (42)
September, 2005 (4)
Categories
Alert (14)
Community Server (2)
Daily Quote (1)
Did you know (7)
Dumb Searches (1)
Dumbass (15)
General (133)
Haiku (6)
Holiday (5)
Movie quote (8)
Paladin (4)
Paris (1)
Parody (1)
Photo (24)
Political (2)
Priceless (7)
Programming (35)
Pytheus (2)
Rant (23)
Screen capture (15)
SQL (3)
Technical (39)
Video (4)
Vista (8)
Visual Studio 2005 (2)
Wifey (33)
XP (1)
Feeds
RSS
Atom
Good Reading
Code Better
Coding Horror
Computer Zen
Daily WTF
Days Bush Has Left
Dooce
Fargg
Me-Nikk
Post Secret
Rory Blythe
TechCrunch
Sign In