473,503 Members | 12,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OnClick - Method With Databind

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:imagebuttonlike this:

<asp:ImageButton ID="hlCancel" runat="server" ImageUrl="/details.png"
Width="10px" Height="10px" OnClick=<%#
showDetails(HttpUtility.HtmlEncode(Convert.ToStrin g(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 ?

Nov 5 '07 #1
4 3527
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...agridclick.asp

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

*************************************************
| Think outside the box!
|
*************************************************
"Jl_G_0" <jl*********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.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
<asp:imagebuttonlike this:

<asp:ImageButton ID="hlCancel" runat="server" ImageUrl="/details.png"
Width="10px" Height="10px" OnClick=<%#
showDetails(HttpUtility.HtmlEncode(Convert.ToStrin g(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 ?

Nov 5 '07 #2
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)
"Jl_G_0" wrote:
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:imagebuttonlike this:

<asp:ImageButton ID="hlCancel" runat="server" ImageUrl="/details.png"
Width="10px" Height="10px" OnClick=<%#
showDetails(HttpUtility.HtmlEncode(Convert.ToStrin g(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 ?

Nov 5 '07 #3
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...

Nov 5 '07 #4
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.

Nov 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3683
by: LU | last post by:
I have a datagrid for viewing and deleting. Under that I have a drop down with binded info and a button to add the select drop down item into database. 1User can view datagrid and delete info....
2
2258
by: Stephen | last post by:
Hey everyone. I was wondering if someone could help me with a small problem. I have designed a user control and I would have inserted it on a aspz page (WebForm1). The User control is being used to...
4
2311
by: Stephen | last post by:
Hey everyone. I was wondering if someone could help me with a small problem. I have designed a user control and I would have inserted it on a aspz page (WebForm1). The User control is being used to...
3
11994
by: Marty McDonald | last post by:
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate a DataTable whose data should appear in the asp:Table. I created my own code to populate the asp:Table with the...
0
3136
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
1
1358
by: simon | last post by:
If I put button into the header of my DataGrid: <asp:TemplateColumn> <HeaderTemplate><asp:Button ID=btnNew Text="New Type" Runat=server></asp:Button></HeaderTemplate> <ItemTemplate> ..... ...
2
1968
by: martyn_wynne | last post by:
Hi, I have found a odd one, my submit button is not submitting on a method="get" form after using any form of DataBind? Has anyone struck this problem before? here is snipits of the code as...
2
2068
by: Nemo | last post by:
Hi. I Have a CheckBoxList and I would like the add the onclick attribute. But when I add the attribute only the first one in my list get the onclick attribute. Here is my code. protected...
4
6454
by: Ryan | last post by:
Hello, I have a standard HTML button on an aspx web form that I have set to runat server. The button is named reset1 and its tag is as follows: <INPUT id="btnReset1" style="WIDTH: 60px;...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.