Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / November 2007

Tip: Looking for answers? Try searching our database.

OnClick - Method With Databind

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jl_G_0 - 05 Nov 2007 14:41 GMT
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 ?
Cowboy (Gregory A. Beamer) - 05 Nov 2007 15:51 GMT
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

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
> Hey all. hope someone can give me a little advice here.
> I have a GridView, and on each line of it there's an
[quoted text clipped - 15 lines]
>
> Anyone have a tutorial or lessons on binding with functions/methods ?
bruce barker (sqlwork.com) - 05 Nov 2007 16:52 GMT
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)

> Hey all. hope someone can give me a little advice here.
>  I have a GridView, and on each line of it there's an
[quoted text clipped - 15 lines]
>
>  Anyone have a tutorial or lessons on binding with functions/methods ?
Jl_G_0 - 05 Nov 2007 17:00 GMT
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...
Jl_G_0 - 05 Nov 2007 20:39 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.