473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to prevent a GridView's ButtonField from doing a postback when clicked?

Is there any way to prevent a ButtonField in a GridView from doing a
postback when clicked? I want to use my own onclick handler to call
window.open('SomePage.aspx') to display a page in a new browser window when
the button is clicked.

I tried doing this in the RowCommand postback event by doing this in the
event handler:

Response.Write("<script>window.open('SomePage.aspx ');</script>");

This works, but has an undesirable side effect: It adds the page to the
history list every time one of the button fields is clicked. So for example
if the user clicked three button fields to show three popup windows, and
then wants to return back to the previous page, he must click the Back
button four times instead of just once.

I'm using the RowDataBound event to add my own onclick event handler to the
button's attribute collection, but I can't figure out how to get rid of the
"javascript:__doPostBack(...)" that asp.net automatically adds. I tried
using button.Attributes.Remove("onclick") before adding my own onclick event
handler, but the "javascript:__doPostBack(...)" ends up still being there.

Any ideas on how to stop the postback?

Thanks in advance,
--Rob Roberts
Apr 11 '06 #1
2 20590
Hi Rob,
add an attribute OnClick, with the value of window.open, and add a "return
false;" as well, return false will cause the post back to be canceled,

example,

button.Attributes.Add("OnClick", "window.open('SomePage.aspx'); return
false;");

* for OnClick, you can use the property OnClientClick (Framework 2.0), so it
would be:

button.OnClientClick = "window.open('SomePage.aspx'); return false;";

** if you use response.write with the javascrip code to show the popup
window, most anti-popup will block it...

Bruno
"window.open('SomePage.aspx'); return false;"

"Rob Roberts" <ro************@AndThisToo.pcisys.net> wrote in message
news:et*************@TK2MSFTNGP02.phx.gbl...
Is there any way to prevent a ButtonField in a GridView from doing a
postback when clicked? I want to use my own onclick handler to call
window.open('SomePage.aspx') to display a page in a new browser window
when the button is clicked.

I tried doing this in the RowCommand postback event by doing this in the
event handler:

Response.Write("<script>window.open('SomePage.aspx ');</script>");

This works, but has an undesirable side effect: It adds the page to the
history list every time one of the button fields is clicked. So for
example if the user clicked three button fields to show three popup
windows, and then wants to return back to the previous page, he must click
the Back button four times instead of just once.

I'm using the RowDataBound event to add my own onclick event handler to
the button's attribute collection, but I can't figure out how to get rid
of the "javascript:__doPostBack(...)" that asp.net automatically adds. I
tried using button.Attributes.Remove("onclick") before adding my own
onclick event handler, but the "javascript:__doPostBack(...)" ends up
still being there.

Any ideas on how to stop the postback?

Thanks in advance,
--Rob Roberts

Apr 11 '06 #2
Bruno,
add an attribute OnClick, with the value of window.open, and add a "return
false;" as well, return false will cause the post back to be canceled,

example,

button.Attributes.Add("OnClick", "window.open('SomePage.aspx'); return
false;");


Thanks for the tip of adding "return false;"! I solved the problem by
changing the ButtonField to a TemplateField and then setting the cell's Text
property to a string containing the necessary html to create a button with
an appropriate onclick event handler. But it looks like it would have been
easier to stick with the ButtonField and add "return false;" to the end of
the onclick event handler, as you showed. I'll try that next time.

Thanks again!
--Rob Roberts
Apr 11 '06 #3

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

Similar topics

7
by: José Joye | last post by:
I have a windows service where I create another appdomains. In the newly created AppDomain, I make use of a C library. If I issue an Abort(1) within this library, it simply hard stop my main...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
5
by: Diane Truyens | last post by:
Hi, I have a form with textboxes, a search button and a gridview. The textboxes are filled with default values so that the gridview should return all rows but not before the user has had the...
2
by: pbd22 | last post by:
Hi. Can somebody tell me how to prevent a postback when I click on the little "plus" sign next to the treenode's root folder? It postbacks and I have to restart a video stream coming to the...
5
by: Eric | last post by:
Hi, there are several gridviews all with a linkbutton which all trigger the same procedure. I need to know from which gridview the linkbutton has been clicked. Thanks Eric Here the code:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.