Solved

cancel postback in a imagebutton control

Posted on 2004-09-09
22
1,613 Views
Last Modified: 2012-06-21
Hi

How i can cancel the PostBack in one ImageButton Click Event?
0
Comment
Question by:Elvio Lujan
  • 9
  • 4
  • 4
  • +3
22 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 12019761
Why do you want that.. do you want to disable postback on some condition.

If not then why not use an Image control instead??
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12019887
Adding on..

If you need to use an ImageButton control and want to cancel postback..
Use the "Enabled" property.. it will disable postback.

myImageButton.Enabled = false;

This property is not listed in the Property pages, when you use Visual Studio .NET.. but it is an inherited property of ImageButton control(inherited from Image).. see the MSDN documentation here..
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsimagebuttonmemberstopic.asp
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12019952
ok. but i need to use the control... if i set the property enabled=false... then i'll can't use the control in the next time
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12020002
You could enable / disable it conditionally ..

As I asked.. >>Why do you want to do that (disable postback) ?? On what condition do you want it to work and on what condition do you want it to not work?
0
 
LVL 33

Expert Comment

by:raterus
ID: 12020226
well I suppose you could do this..

myImageButton.attributes.add("onClick", "javascript:return false")
0
 
LVL 33

Expert Comment

by:raterus
ID: 12020240
rather, best to put a semi-colon at then end of that, or you might run into some javascript errors..
myImageButton.attributes.add("onClick", "javascript:return false;")
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020283
but the link don't execute
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020297
i have frames in my site and i need press the imagebutton then send the url link to an other frame
0
 
LVL 33

Expert Comment

by:raterus
ID: 12020323
well "execute" in an imagebutton is a postback!, unless you've written some javascript otherwise that you aren't telling us about.
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020364
ok start again...

I need when the event click succed from a imagebutton... goto an url but in other frame!
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020372
and i need don't reload the frame where is the imagebutton
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:raterus
ID: 12020413
Do you need to link to postback?  ImageButtons are actually form elements (if you ever look at the source), if you don't need the page to postback then you shouldn't be using them.  If you just want to do client-side operations you should just define a regular image "<img src="blah.jpg" /> and add an onClick handler that in client-script will change the src attribute of the <iframe>.  Is this what you are after?
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020468
ok. i'll try it
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12020693
You can even use HyperLink Control from WebControls.

 - Set ImageUrl property of HyperLink to your image.
 - Set NavigateUrl and Target URL according to your need

-tushar
 
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12020738
ok tusharashah... and how i can change the image when the mouse pointer is over the hyprlink control?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12020943
It doesn't sound like you need server-side control (where you currently have your ImageButton) at all.  Just plain ol' HTML:

    <a href="NewPageUrl.aspx" target="otherFrameName"><img src="yourImage.jpg"></a>

If you wanna check the image onmouseover:

    <a href="NewPageUrl.aspx" target="otherFrameName"
        onmouseover="document.getElementById('imgBtn').src='MouseOverImage.jpg';"
        onmouseout="document.getElementById('imgBtn').src='yourImage.jpg';"
    ><img id="imgBtn" src="yourImage.jpg"></a>
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12021292
I will check out the exact syntact, but for mouseover image rotation you can add an attribute to HyperLink in your page_load event liek following

HyperLink1.attributes.add("onMouseOver", "javascript:this.src='NEW URL';")

-tushar
0
 
LVL 8

Expert Comment

by:boulder_bum
ID: 12022895
I didn't read all the above, I just wanted to add something about the answer in case you need it.

If you'd like to conditionally cancel postback for an image button, you can use a javascript confirm dialog:

//in code
myImageButton.Attributes.Add( "onclick", "return confirm( 'Do you still want to PostBack?' );" );
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12025432
tushar:
your code don't work:

HyperLink1.attributes.add("onMouseOver", "javascript:this.src='NEW URL';")
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 12025788
Hello lem2802,
Add following 2 line in your Page_Load event it will work:
------------------------------------------------------------------------------------------------------------------------------

HyperLink1.Attributes.Add("onMouseOver", " this.firstChild.src='[Image1]' ");
HyperLink1.Attributes.Add("onMouseOut", "this.firstChild.src='[Image2]' ");

------------------------------------------------------------------------------------------------------------------------------

-tushar
0
 
LVL 17

Author Comment

by:Elvio Lujan
ID: 12025877
tanks tushar
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12025898
Thanks for the A.
Glad I could help.  :)

-tushar
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now