Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I am trying to create buttons on a weform at runtime, based on entries from an .ini file. I can read the file just fine, but I am not sure how to dynamically create button controls on the form, or how to locate them in an orderly fashion.

 

Should I use the repeater control? Should I use a datagrid? Can I just create a button right onto the form programatically?

 

I know it can be done, I just don't know how to approach it.

 

Thanks,

 

Cesar

Posted
  Quote
Hello,

 

I am trying to create buttons on a weform at runtime, based on entries from an .ini file. I can read the file just fine, but I am not sure how to dynamically create button controls on the form, or how to locate them in an orderly fashion.

 

Should I use the repeater control? Should I use a datagrid? Can I just create a button right onto the form programatically?

 

I know it can be done, I just don't know how to approach it.

 

Thanks,

 

Cesar

 

Cesar,

 

This is the way I would approach it:

 

1.) Add an ASP:Panel "pnlButtons" to your form using HTML.

2.) From the code behind do the following:

 

Dim myBtn as System.Web.UI.WebControls.Button

Dim iniEntry as String = String.Empty

Dim iniEntries() as String = String.Empty

 

For Each iniEntry In iniEntries

myBtn = New System.Web.UI.WebControls.Button

myBtn.Text = iniEntry

pnlButtons.Controls.Add(myBtn)

Next

 

Add any other attributes you need to add to myBtn before adding it to the panel's controls collection.

 

Hope this helps!

 

Jeff

Posted

Thanks Jeff, worked beautifully.

 

Just added a few spacers to make it look good and now I have dynamic button generation based on the contents of an .ini file.

 

The problem I have now is... How do I handle the button click event? I am trying to use the CommandName and CommandArgument properties, but they apparently do nothing without having the OnCommand property set, and that is a protected property that I cannot access programatically. Is there a generic button click event that is raised whenever any button is clicked?

 

Any ideas?

 

Thanks,

 

Cesar

Posted

Never mind, I figured it out.

 

Had to add the CommandName and CommandArgument properties to the buttons as I created them to make them command buttons, and then bubble the click event of the buttons to the panel. Worked perfectly.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...