OnClick - Method With Databind

J

Jl_G_0

Hey all. hope someone can give me a little advice here.
I have a GridView, and on each line of it there's an
<asp:imagebutton> like this:

<asp:ImageButton ID="hlCancel" runat="server" ImageUrl="/details.png"
Width="10px" Height="10px" OnClick=<%#
showDetails(HttpUtility.HtmlEncode(Convert.ToString(Eval("id"))))
%> /

The thing is, I need the OnClick event on each line to call the
method showDetails with the ID related to it, like showDetails(LINE
ID), but this binding tag, while working normally on normal fields
(like label's TEXT) doesn't work here.
I think the error is CASTING related... but Im not sure. I also
tried
to create a function to return the string "showDetails(id)", but it
gives me an InvalidPostback error...

Anyone have a tutorial or lessons on binding with functions/methods ?
 
C

Cowboy \(Gregory A. Beamer\)

Use the standard events rather than trying to kludge this together. There
are plenty of tutorials on the web on getting the ID from a postback,
without attempting to call a custom server side event.

if you must have a call that is fired onClick, you can do something like the
following;
http://www.codeproject.com/useritems/aspdatagridclick.asp
 
G

Guest

the OnClick is a serverside event for which you cannot change the signature.
the property is expecting the name of a function (with the proper parameter
list),

if you where trying to call client script, see the OnClicntClick property.

-- bruce (sqlwork.com)
 
J

Jl_G_0

thx. I'll have a look at these links. The reason I wanted to do this
its because I tried to do several functions inside the same aspx page,
with UpdatePanels and dynamic stuff. I was avoiding postbacks and pop-
ups to show details...
 
J

Jl_G_0

Working now - just reporting what I did, maybe someone will need:

The method gets the sender AND its CommandArgument, since the
CommandArgument is a string, it can be bound to the ID field. So the
method gets the sender and then the ID of the gridview element.

void showDetails(object sender, EventArgs e)
{
//HERE I CREATE AN INSTANCE FOR THE BUTTON
ImageButton bot = (ImageButton)sender;
//NOW YOU GET THE COMMAND ARGUMENT
string argument1 = bot.CommandArgument.ToString();
//AND FINALLY YOU CAN USE IT THE WAY YOU WANT
string sql = "SELECT * FROM tbl01 WHERE id=" + argument1;
......
}

Here's the code for the button, OnClick calls the method above,
CommandArgument is bound to the ID field of my database:

<asp:ImageButton ID="hlCancelx" runat="server" ImageUrl="~/
details.png" Width="10px" Height="10px" OnClick="showDetails"
CommandArgument='<%#
HttpUtility.HtmlEncode(Convert.ToString(Eval("id"))) %>'/>

The downside: I had to switch EnableEventValidation to "false" on the
page... but I think it can't be helped...

Thanks for the help, I researched based on what you guys told me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top