Solved

Send parameters to method from markup to code behind

Posted on 2010-09-23
2
558 Views
Last Modified: 2012-05-10
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
Comment
Question by:jwoodhams
2 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 33744956
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
 
LVL 1

Author Closing Comment

by:jwoodhams
ID: 33746025
Thanks so much for the suggestion.  It worked perfectly.  Thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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