[web] [ASPX] Handling input of dynamically created buttons

Started by
0 comments, last by ArchG 13 years, 7 months ago
For my site's admin panel, i am displaying a big table of all of our media files pulled from a SQL database. Next to each one, I am creating a few buttons such as "Edit" "Delete" etc. All these are created dynamically on page load, and attached to a placeholder.

I am having some problem with handling the input, however. I am using:

Dim myButton as Button = newButton
AddHandler myButton.OnClick, AddressOf Somefunction
somePlaceholder.Controls.Add( myButton )

However, this only works if I re-create the buttons each pageload. What I wanted to do is create the list of media only when we are not in postback, and when a user clicks the "edit" button it refreshes the page and creates an Edit dialogue, without the list being re-generated. However, if I do not re-create the button in postback, the handler never gets called (which makes sense).

How can I dynamically create buttons that will execture functions on post back (and pass in a parameter, such as an ID of the media we are supposed to edit)? Do I need to create a new form for every single entry in my list, with hidden input elements that store a command and parameter I parse on postback? Or is there a better way?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Advertisement
I did something similar recently.

I used a GridView to display a bunch of records, and each record had a couple buttons (Edit, Delete, View or something).

I used an Item template with it like this
<asp:TemplateField HeaderText="Options">    <ItemTemplate>        <asp:Button ID="bEdit" Text="Edit Record" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" runat="server" />     </ItemTemplate></asp:TemplateField>


then in my code behind, i had something like this (code in C# cause in VB i'd just be guessing)
protected void gridRows_Command(object sender, GridViewCommandEventArgs e){    if (e.CommandName == "Edit")    {        int id = (int)e.CommandArgument;        //reload page with your edit box for this particular item     }}



I think that's kinda what you're looking for, let me know if the grid view solution will work for you and I can help you more with it, or if you don't want anything to do with some of these nasty asp controls i'll try to get you a solution more like you asked for.

This topic is closed to new replies.

Advertisement