Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

Send parameters to method from markup to code behind

Hi Everyone,

I have an image button nested inside a datalist control.  When user clicks the image button, I want to run a method from my code behind.  In my Image Button markup I have this:
OnClick='<%# "ShowDetail(" & Cstr(Eval("ID")) & ")" %>'
This does not work and I am not sure why. This code produces thie error:
Microsoft jscript runtime error: Object Expected

What I do know is that when I call the method without arguments, like this:
OnClick='ShowDetail"
I have no problem.  But, I need to send the argument.  

Any help is greatly appreciated

Thanks

Jay
0
jwoodhams
Asked:
jwoodhams
1 Solution
 
Todd GerbertIT ConsultantCommented:
The OnClick event is defined by the image button and the method this parameter points to must accept two parameters, an Object and an ImageClickEventArgs - you don't have any choice in that matter.
What you probably want to do is have a ImageButton_Click(sender As Object, e As ImageClickEventArgs) method assigned to OnClick, and in that method you can call ShowDetail however you like.  You could assign <%# Eval("ID") %> to the CommandArgument of the ImageButton and then retrieve it in the Click event handler.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

	Protected Sub ImabeButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
		Dim ib As ImageButton = DirectCast(sender, ImageButton)
		ShowDetail(ib.CommandArgument)
	End Sub
	
	Private Sub ShowDetail(ByVal ID As Integer)
		'Do whatever you like here
	End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:ImageButton ID="ImabeButton1" runat="server" CommandArgument="<%# Eval("ID") %>" 
			onclick="ImabeButton1_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
jwoodhamsAuthor Commented:
Thanks so much for the suggestion.  It worked perfectly.  Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now