I got a problem with a button control in asp.net webforms.
After using mvc a bit i started using foreach in the aspx files in webforms too, instead of repeaters, mostly cause of all the junk viewstate a repeater creates.
It works great when you just iterate over html controls. But i got a hard time with button controls.
<% foreach (var r in Reports) { %> <asp:LinkButton OnClick='Click_DeleteResult' CommandArgument='<%= r.ResultId.ToString() %>' runat='server'>Delete</asp:LinkButton> <% } %>
On postback the commandargument on that button becomes ‘<%= r.ResultId.ToString() %>’. So that code doesn’t get executed. Is there a good way to fill control properties this way?
This is ugly. The #1 benefit of using ASP.NET (especially MVC) is separating the logic and presentation. I cringe every time I have to look in an aspx file for executing code.
If you don’t like the ViewState ‘junk’ then just turn it off for that control!
If you insist on this though… I’ve seen situations like this where ASP.NET would not parse a server tag correctly unless I dropped the surrounding quotes. Like so:
Might not be completely conforming HTML, but ASP.NET doesn’t care. If you really need to output those surrounding quotes for some reason you can put them inside the server tag instead of outside. It’s counter-intuitive, but sometimes you have to do it that way.