简体   繁体   中英

ASP.NET Can't dynamically add new controls to the page

I want to add some FileUploads to the page dynamically and control them it postback, because if to use plain html tags and not asp.net controls it may cause issues with the file upload.

I've defined in ASPX page the panel:

<asp:Panel ID="panelViewer" BackColor="Azure" Width="400" Height="400" runat="server">
</asp:Panel>

And in <% %> tags trying to add dynamically the correct count of file uploads forms. The count of file upload forms depends on MySQL record in table, it could be different for the users.

I'm trying to add dynamically so, just for the start:

Button btn = new Button();
btn.Text = "click me!";
panelViewer.Controls.Add(btn);

FileUpload fileUpload = new FileUpload();
fileUpload.BorderColor = System.Drawing.Color.Red;
panelViewer.Controls.Add(fileUpload);

But no success, when I push Ctrl + U in HTML response from server, I've got:

<div id="panelViewer" style="background-color:Azure;height:400px;width:400px;">
</div>

There are no controls in HTML response, so in rendered page from server.

I've tried not to use earlier defined panel and make it dynamically added to the runat form , but again the same result.

How could I fix such a problem? Thank you!

尝试将btn.ID包含为控件的属性。

you can use this

Style style = new Style();
style.BorderColor = System.Drawing.Color.Red;
fileUpload.ApplyStyle(style);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM