469,904 Members | 2,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

dynamic loading of images by linkbutton clicks

sef
i have a server-side script that executes this:

linkbutton1.Attributes.Add("onClick", "linkClicked();");

the client-side code has an html image control
<img id="image1">
the client-side javascript function linkClicked looks like
this
void linkClicked(object sender, System.EventArgs e){
image1.src = "test.jpg";
}

what i would want to happen is when the linkbutton is
clicked, the linkClicked() function gets called to change
the image loaded on image1.
However this does not happen and no images are loaded. Any
insights on this?
Nov 18 '05 #1
4 5664
I assume that you're trying to avoid a postback by using a regular img tag
rather than an ASP.NET image control. In this case, you're really just using
a linkbutton server-side control to generate a client-side hyperlink. Most
everything is client-side script. Don't forget to use return false to
prevent the postback from happening.

How about this?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
LinkButton1.Attributes.Add("onClick", "linkClicked();return false")
sb.Append("<scr")
sb.Append("ipt language='javascript'>")
sb.Append("function
linkClicked(){document.all.item(""cdnimage"").src" )
sb.Append("=""http://www.gc.ca/images/francaisbt.gif"";}</script>")
Page.RegisterClientScriptBlock("imgchg", sb.ToString)
End Sub
<P>
<asp:LinkButton id="LinkButton1"
runat="server">LinkButton</asp:LinkButton></P>
<P><IMG id="cdnimage" alt=""
src="http://www.gc.ca/images/englishbt.gif"></P>
Ken

"sef" <is**************@yahoo.com> wrote in message
news:03****************************@phx.gbl...
i have a server-side script that executes this:

linkbutton1.Attributes.Add("onClick", "linkClicked();");

the client-side code has an html image control
<img id="image1">
the client-side javascript function linkClicked looks like
this
void linkClicked(object sender, System.EventArgs e){
image1.src = "test.jpg";
}

what i would want to happen is when the linkbutton is
clicked, the linkClicked() function gets called to change
the image loaded on image1.
However this does not happen and no images are loaded. Any
insights on this?


Nov 18 '05 #2
Wow, thank you very much Ken! I can't wait to try out that code. Though
I would also like to clear some things a bit:

The function linkClicked() I mentioned was already coded in the HTML as
opposed to your suggestion of dynamically writing the code from
Page_Load (code-behind). Do the two approaches have any difference?

Also, my LinkButtons are also dynamically created from OnInit event so
as to preserve them on post back. I am not so sure that a postback
occurs when I click the linkbutton though, cause nothing happens, the
page is not even reloaded and the image also fails to show. Does this
mean that a postback doesnt occur when I click the linkbutton?
"I assume that you're trying to avoid a postback by using >a regular img tagrather than an ASP.NET image control. In this case, you're
really just using
a linkbutton server-side control to generate a client-side
hyperlink. Most
everything is client-side script. Don't forget to use return false toprevent the postback from happening."


Should I wish to avoid this postback so that the image is loaded on the
onClick event of the linkbutton?
Thank you very much!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Hi Joseph,
The function linkClicked() I mentioned was already coded in the HTML as
opposed to your suggestion of dynamically writing the code from
Page_Load (code-behind). Do the two approaches have any difference?
No real difference. I just find it easier to keep as much code as possible
in the code-behind file. Your approach is perfectly valid.
Also, my LinkButtons are also dynamically created from OnInit event so


When you create controls dynamically, you have to handle the viewstate and
event handling on your own. There's a control that will help you do this:

http://www.denisbauer.com/ASPNETCont...aceholder.aspx

Ken

Nov 18 '05 #4
Your code works perfectly well. Thanks ken.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by BLiTZWiNG | last post: by
3 posts views Thread by NateDawg | last post: by
13 posts views Thread by rn5a | last post: by
9 posts views Thread by pbd22 | last post: by
3 posts views Thread by =?Utf-8?B?UiBSZXllcw==?= | last post: by
5 posts views Thread by xend | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.