ASP.NET C# - 从数据列表中删除列
我在用户控件中有一个数据列表,该数据列表被加载到页面中,用户可以在其中根据某些复选框自定义报告。
然而,复选框之一是“隐藏工作日志”,它应该从结果集中隐藏工作日志列,因为它可能很长并且会干扰报告。
如果我这样做:
datatable1.Columns.Remove("WorkLog");
代码会抛出异常,因为:
<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>
不存在。
我对用户控制的理解是错误的吗?该用户控件应该始终能够显示工作日志,因此我认为将其绑定在那里也不错,但同时我希望能够在用户愿意的情况下隐藏它。
I have a datalist inside a usercontrol that gets loaded into a page where users can customize a report based on some checkboxes.
One of the checkboxes, however, is "Hide Worklog" which should hide the worklog column from the result set because it can be quite long and interfere with the report.
If I do:
datatable1.Columns.Remove("WorkLog");
the code throws an exception because:
<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>
doesn't exist.
Am I going about the usercontrol all wrong? This usercontrol should always be able to show the worklog, so I don't think it's bad to bind it in there, but at the same time I want to be able to hide it if the user wants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试从 DataList 中删除标签控件,而不是从数据源(即 DataTable)中删除列。
如果数据源的列数多于页面上显示的列数,则不会出现错误,但是,您会得到正如您所发现的,如果您尝试显示数据源中不存在的列,则会出现错误。
Try removing the label control from your DataList instead of removing the column from the data source (i.e. the DataTable)
You shouldn't get an error if the data source has more columns than you're displaying on the page, however, you will get an error, as you've discovered, if you're trying to display a column that doesn't exist in the data source.
检查某些条件后将其绑定在代码后面。就像
在删除控制时,
您可能需要更改会话变量的可见性:)
bind it in code behind after checking some condition. like
while removing control
you might need to change visible to session var :)