Computing.Net > Forums > Web Development > ASP.Net, dynamically create button

ASP.Net, dynamically create button

Reply to Message Icon

Original Message
Name: alwinlee
Date: June 9, 2005 at 21:35:27 Pacific
Subject: ASP.Net, dynamically create button
OS: win2000
CPU/Ram: 512
Comment:

Hi all,

I am using C# in ASP.Net web application. what i want to do is that
1. display the data which from the sql database in datagrid
2. there is a "delete" button at the end of each row of datagrid, whenever i click the button, the whole row will remove from the datagrid.

My problem is that because of i don't know how many row in the database, how can i dynamically create the button object for each row? And how to create event handle function for each button to achieve what i want to do??

thanks for your time

alwin


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: June 9, 2005 at 22:22:36 Pacific
Subject: ASP.Net, dynamically create button
Reply: (edit)

OK, I haven't used C# or ASP.net yet, but I did the same thing in VBScript in ASP. Very simple.

When you query the database make sure one of the columns you return is the ID column for the particular data. When you create your datagrid, have the buttons run a function or go to another page to delete the particular row in the table. In my case I believe I created each button to go to the page www.something.com/somepage.asp?action=delete&id=XXX where the somepage was the same page that displayed the datagid. The action value would trigger a function at the beginning of the page to delete the record with the id that was passed on the query string.


Report Offensive Follow Up For Removal

Response Number 2
Name: SN
Date: June 10, 2005 at 08:49:29 Pacific
Subject: ASP.Net, dynamically create button
Reply: (edit)

Michael's way of doing it would work, but it's how you would typically do it in languages like ASP or PHP, not ASP.NET. ASP.NET provides a more elegant solution.

There is a ButtonColumn class that you add to the datagrid just like a regular bound column. You should change the type to PushButton, and change the headerText and Text properties to whatever you want. Then you give it a CommandName..."Delete" is a good choice. This will add the button to each row automatically.

When the use clicks one of these buttons, the event that gets raised is the DataGrid.OnItemCommand event. Handle this event, check to make sure the item command is "Delete" and you should get the row that you want to delete from the event args.

I haven't done ASP.NET for nearly a year, so I'm a little rusty. See a full example on how to edit and delete items from a grid here. Interestingly, this example is on the DeleteCommandEvent page, but uses the OnItemCommand method I described above. I believe you can use the DeleteCommandEvent just as easily, but I remember preferring the item command for some reason.

Good luck,
-SN


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: ASP.Net, dynamically create button 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge