473,503 Members | 3,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programatically setting OnClick handler for asp:Button (how to do?)

Hi! I have an aspx that I've created which has an asp:Button in it. I
would like to be able to change the OnClick handler at runtime
(basically, I want this form to either edit or create a record, and I
want to change the procedure it calls depending on what it should be
doing). However, apparently the .OnClick member is read only?

(Sample:)

<script language="jscript">
function do_edit() {
...
do_action_btn.Text = "Save";
do_action_btn.OnClick = "do_update";
}
....
</script>
<html>
<head>
</head>
<body>
<form id="page_form" runat="server" />
...
<asp:Button id="do_action_btn" runat="server" />
...
</form>
</body>
</html>

----------

Alternately, if the above isn't possible, I can do something with
CommandButtons, but without a repeater/datagrid/datalist, I'm not sure
where to put the "OnItemCommand" property...

Thanks muchly,
Ricky Morse
Nov 18 '05 #1
2 2508

I haven't had much luck re-using buttons over multiple
post backs. There is a solution, however. You can use a
number of asp:Panels to hold sets of buttons (or whatever
you need to change between post backs). Create a Panel
holding the first "Edit" button, and a second Panel
holding the "Save" button with visibility=false. In
Edit_OnClick, set edit_panel.visibility=false and
save_panel.visibility=true. Obviously, you will need
different OnClick handlers for each button.

That's not exactly what you asked for, but it can
sometimes be a little cleaner than re-using buttons and
eventhandlers.

hth,
Matt
-----Original Message-----
Hi! I have an aspx that I've created which has an asp:Button in it. Iwould like to be able to change the OnClick handler at runtime(basically, I want this form to either edit or create a record, and Iwant to change the procedure it calls depending on what it should bedoing). However, apparently the .OnClick member is read only?
(Sample:)

<script language="jscript">
function do_edit() {
...
do_action_btn.Text = "Save";
do_action_btn.OnClick = "do_update";
}
....
</script>
<html>
<head>
</head>
<body>
<form id="page_form" runat="server" />
...
<asp:Button id="do_action_btn" runat="server" />
...
</form>
</body>
</html>

----------

Alternately, if the above isn't possible, I can do something withCommandButtons, but without a repeater/datagrid/datalist, I'm not surewhere to put the "OnItemCommand" property...

Thanks muchly,
Ricky Morse
.

Nov 18 '05 #2
submit/image buttons have a value they post back. just change the value in
client script, and check the value on the server side on the onclick event.
function do_edit() {
var btn = document.getElementById('do_action_btn');
...
btn.value = "Save";
}

-- bruce (sqlwork.com)
"Richard Morse" <re*****@partners.org> wrote in message
news:re***************************@plato.harvard.e du...
Hi! I have an aspx that I've created which has an asp:Button in it. I
would like to be able to change the OnClick handler at runtime
(basically, I want this form to either edit or create a record, and I
want to change the procedure it calls depending on what it should be
doing). However, apparently the .OnClick member is read only?

(Sample:)

<script language="jscript">
function do_edit() {
...
do_action_btn.Text = "Save";
do_action_btn.OnClick = "do_update";
}
...
</script>
<html>
<head>
</head>
<body>
<form id="page_form" runat="server" />
...
<asp:Button id="do_action_btn" runat="server" />
...
</form>
</body>
</html>

----------

Alternately, if the above isn't possible, I can do something with
CommandButtons, but without a repeater/datagrid/datalist, I'm not sure
where to put the "OnItemCommand" property...

Thanks muchly,
Ricky Morse

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2109
by: giant food | last post by:
Hi, I'm writing an asp app. I have a text box with a validator and a submit button. Here is code from my .aspx file.... <asp:TextBox ID="txName" TextMode="SingleLine" Runat="server" />...
4
2274
by: z. f. | last post by:
Hi, I'm having an aspx page with a server form. i have a grid with a delete button and below the grid, another area with inputs for inserting new values and an "add" button for submiting the...
1
1479
by: tshad | last post by:
I want to be able to change the color of the text of a button after it is pressed. The first part of the event is: sub submitQuestion_click(Sender as Object, e as EventArgs)...
3
2448
by: wardy1975 | last post by:
Hi all....looking for a bit of help here....I'm using an asp.net server side button, and trying to add an onclick event on the button to call a javascript function. However, I am trying to enter...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.