Solved

Send parameters to method from markup to code behind

Posted on 2010-09-23
2
554 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
Comment Utility
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
Comment Utility
Thanks so much for the suggestion.  It worked perfectly.  Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

9 Experts available now in Live!

Get 1:1 Help Now