Solved

Send parameters to method from markup to code behind

Posted on 2010-09-23
2
557 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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