Reputation: 146
I want to create a download list where each available file has its own ASPxButton and I would like to dynamically create them rather than creating like 30 manually and dynamically setting them to visible.
Since I don't have a form within my .aspx.cs file but create my buttons within a table in my .aspx code, I can't use the form.AddButton method. Any ideas on how to do something like that?
Not sure if that helps but here is the code for the 2 buttons I already have:
<dx:TabPage Name="Downloads" Text="Downloads" Enabled="false">
<ContentCollection>
<dx:ContentControl runat="server">
<table class="grid_centered">
<tr>
<td>
<dx:ASPxButton ID="btnDownload0" runat="server" Text="Anschreiben" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px"></dx:ASPxButton>
</td>
</tr>
<tr>
<td>
<dx:ASPxButton ID="btnDownload1" runat="server" Text="Serie 1" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px" Visible="false" Enabled="false"></dx:ASPxButton>
</td>
</tr>
</table>
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
Upvotes: 0
Views: 331
Reputation: 146
The solution to this is to use an asp:Repeater
. This way, I only have to write the details for one and use the OnItemCommand
event to handle button clicks. I had to switch to using asp:Button
instead of dx:ASPxButton
, though since I needed the CommandName
field to pass the info which button was pressed along to my event handler.
<asp:Repeater
ID="MyRepeater"
runat="server"
DataSourceID="MyDataSource"
OnItemCommmand="MyClickedEventHandler">
<ItemTemplate>
<asp:Button
ID="MyButton"
runat="server"
Text="Download"
CommandName='<%# $"{Eval("keyField")}" %>' />
</ItemTemplate>
</asp:Repeater>
Upvotes: 1
Reputation: 174
I am not too familiar with dx:ASPxButton but I am assuming it would be similar to an aspx:Button. To dynamically create controls you have to create the controls in the Page_Load() event.
protected void Page_Load()
{
//get your files
string targetDirectory = "your directory";
string [] fileEntries = Directory.GetFiles(targetDirectory);
int i = 0;
foreach(string fileName in fileEntries)
{
ASPxButton btn = new Button();
btn.Text = fileName;
btn.ID = "btnDownload" + i.ToString();
btn.CssClass = "button centered";
btn.Attributes.Add("Theme", "Metropolis");
//add any other properties you need
//add the event handler
btn.Click += new EvenHandler(btn_Clik);
form.AddButton(btn);
}
}
Upvotes: 0