We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,521 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Calling Javascript from ImageButton

Hi guys,

how would I translate the functionality of this anchor:

<a style="font-size: small;" href="javascript:void(0)" onclick="document.getElementById('info').style.display='block';document.getElementById('fade').style.display='block';return false;">Blahblah</a>

To an asp.net imagebutton control?

This is what I have come up with:

<asp:ImageButton ID="ImgBtn" runat="server" ImageUrl="~Images/Go.gif" 
                OnClientClick="<script type='text/javascript'>document.getElementById('info').style.display='block';document.getElementById('fade').style.display='block';return false;</script>" />

But its not coming out the same way. Can anyone help me out?

3
Contributors
5
Replies
2 Days
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

But its not coming out the same way.

What is different?

pritaeas
Posting Prodigy
Moderator
9,541 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

Looks like the asp.net engine doesnt care for the JavaScript syntax written in that manner. If you look at the HTML produced for the ImageButton it is rendered as such:

<input type="image" name="ImgBtn" id="ImgBtn" src="Images/Go.gif" onclick="&lt;script type=&#39;text/javascript&#39;>document.getElementById(&#39;info&#39;).style.display=&#39;block&#39;;document.getElementById(&#39;fade&#39;).style.display=&#39;block&#39;;return false;&lt;/script>;" />

Why dont you add a JavaScript block and just call the function?

    <a style="font-size: small;" href="javascript:void(0)" onclick="myscript()">Blahblah</a>
    <asp:ImageButton ID="ImgBtn" runat="server" ImageUrl="~Images/Go.gif" OnClientClick="myscript()" />
    <script>
        function myscript() {
            document.getElementById('info').style.display = 'block';
            document.getElementById('fade').style.display = 'block';
            return false;
        }
     </script>
JorgeM
Industrious Poster
4,164 posts since Dec 2011
Reputation Points: 297
Solved Threads: 567
Skill Endorsements: 119

Hi Jorge,

I tried it as suggested and it works, but to a certain extent. When I click the ImageButton the blocks only appear for a split second. How should I fix this issue? When the blocks are displayed, there is a button where the user can close the window, so the blocks should not disappear on their own. Is this because I am calling it from an 'OnClientClick' property?

Thanks again

while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Nevermind, I actually got it working as desired. I just used the <input> tag and used the image property to make it appear as an image button. I also called my javascript function with the 'onclick' event. The syntax was as follows:

onclick='return myscript();

thanks again guys for your help. It is very much appreciated.

while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by pritaeas and JorgeM

ok not a problem. with regard to

When I click the ImageButton the blocks only appear for a split second..

I suspect that the issue you had is that the image button, since rendered as a input button will post back when clicked. You would have to modify that behavior.

Just wanted to provide this info for the benefit of anyone else that may come accross this thread.

JorgeM
Industrious Poster
4,164 posts since Dec 2011
Reputation Points: 297
Solved Threads: 567
Skill Endorsements: 119

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0712 seconds using 2.68MB