473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Adding event handler for dynamically created controls

Hello
Please excuse me if this come in as a duplicate.

Here is the situation that I am having: I am pulling data from a database and writing it out as a list. Next to each item that I write, I want to make a linkbutton control which will allow you to invalidate the database record. Since I don't know how many records will be present, I need to have all of the linkbuttons access the same event. The trick is to know WHICH linkbutton was pressed. Is there a way that anyone can suggest to accomplish this? I have been trying to do this for a good deal of time so far
Thank you
Andre

Nov 18 '05 #1
3 1888
Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

Bil

----- Andrew Wied wrote: ----

Hello
Please excuse me if this come in as a duplicate.

Here is the situation that I am having: I am pulling data from a database and writing it out as a list. Next to each item that I write, I want to make a linkbutton control which will allow you to invalidate the database record. Since I don't know how many records will be present, I need to have all of the linkbuttons access the same event. The trick is to know WHICH linkbutton was pressed. Is there a way that anyone can suggest to accomplish this? I have been trying to do this for a good deal of time so far
Thank you
Andre

Nov 18 '05 #2
Thank you for the reply
Actually, I'm not using a datalist. Basically, I am wondering if there is a way to get two or more (a variable quantity) of controls[linkbuttons, specifically] to fire the same event, and have the event handler know which one threw the event

Is there a way that anyone knows

Thanks
Andre

----- Bill Borg wrote: ----

Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

Bil

Nov 18 '05 #3
Just attach the same handler to the event you want from each of the controls, then in the handler use the "sender" object to figure out who sent it--e.g. you've got a bunch of buttons, you attach MyButtonHandler to each button's Click event, and then in MyButtonHandler you do something like (button)sender.name to figure out who was clicked. Does that help

----- Andrew Wied wrote: ----

Thank you for the reply
Actually, I'm not using a datalist. Basically, I am wondering if there is a way to get two or more (a variable quantity) of controls[linkbuttons, specifically] to fire the same event, and have the event handler know which one threw the event

Is there a way that anyone knows

Thanks
Andre

----- Bill Borg wrote: ----

Andrew, I presume you are writing it out as a datalist. No matter which button the user clicks in the datalist, the ItemCommand event fires. Inside ItemCommand, e.item is the datalistitem of the row that was clicked, and in e.item.controls you'll find the data that's shown in the grid. In addition to or instead of, you can bind the linkbutton's CommandArgument property to your primary key field, then retrieve CommandArgument in ItemCommand and use it to go get your data from somewhere, etc. (this is handy when you want a key or something that you're not showing in the grid, although you can do that too with hidden fields). ht

Bil

Nov 18 '05 #4

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

Similar topics

3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Greg | last post by:
In my ASP.NET application I have a button that when pressed, data needs to be saved based on what the user typed in. I have other controls on the same page that should be updated as a result of the...
2
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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,...
0
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...

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.