Solved

Send parameters to method from markup to code behind

Posted on 2010-09-23
2
560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 58
Build a string of emails from a gridview 2 31
c#, datatable, aspx 4 52
dropdownlist in asp.net vb. 3 23
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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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