Sign in to follow this  

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

This topic is 2659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this