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?
[web] [ASPX] Handling input of dynamically created buttons
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
then in my code behind, i had something like this (code in C# cause in VB i'd just be guessing)
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.
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
Popular Topics
Advertisement