FormView 未传递“runat=server”中包含的值排
我的 FormView 的 EditItemTemplate 中有以下代码:
<tr id="primaryGroupRow" runat="server">
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText"
DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList></td>
</tr>
如果删除表行的 runat="server",则 iPrimaryGroup 字段将 100% 绑定并正确传递到业务逻辑层。然而,在上面的代码中,它以零值传递。
谁能告诉我这是为什么或者如何解决它?这是一个需要隐藏此表行的控件,具体取决于管理员或普通用户是否正在编辑它。即:某些字段只能由管理员写入,如果用户不是管理员,我想从视图中隐藏控件。
I have the following code in the EditItemTemplate of my FormView:
<tr id="primaryGroupRow" runat="server">
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText"
DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList></td>
</tr>
If I remove the runat="server" for the table row, then the iPrimaryGroup field is bound 100% and passed to the business logic layer properly. However in the case of the code above, it is passed with a value of zero.
Can anyone tell me why this is or how to get around it? This is in a control that needs to hide this table row, based on whether or not an administrator or a regular user is editing it. ie: some fields are admin writeable only and I'd like to hide the controls from the view if the user isn't an admin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果安全是一个问题,也许这可能会更好
如果我没有弄错的话,如果visible=false,面板内的任何标记都不会被渲染
If security is a concern perhaps this might work better
If I'm not mistaken any markup within the panel will not be rendered if visible=false
尝试一下:
删除 runat=server 属性
定义一个 css 类
然后根据用户是否是管理员设置类属性
Have a shot at this:
Remove the runat=server attribute
Define a css class
Then set the class attribute based on whether or not the user is an admin
看来此功能是设计使然,尽管尚未得到完全证实。
http://weblogs.asp.net/ rajbk/archive/2009/08/03/formview-binding-gotcha.aspx
使用 FormView 对象时,如果您有嵌套控件,则双向数据绑定将无法正常工作。您可以访问代码中的控件,也可以获取数据,但它不会像预期的那样自动更新业务逻辑层 (BLL) 后端的值。
幸运的是,有一个解决方法。让它工作的方法是为 ItemUpdating 创建一个事件。它将具有如下签名:
这使您可以访问 FormViewUpdateEventArgs,这反过来又允许您在 ObjectDataSource 值运行时以及命中 BLL 代码之前对它们进行更改,如下所示:
It appears that this functionality is by design, although that's not exactly confirmed.
http://weblogs.asp.net/rajbk/archive/2009/08/03/formview-binding-gotcha.aspx
When using the FormView object, if you have a nested control, then two-way databinding isn't going to work properly. You can access the controls in code, and you can get at the data, but it's just not going to automatically update the value in the back end of your Business Logic Layer(BLL) like it's supposed to.
Fortunately, there's a workaround. The way to get it working is to create an event for ItemUpdating. It will have a signature like this:
This gives you access to the FormViewUpdateEventArgs, which in turn allows you to make changes to the ObjectDataSource values while they are in flight and before they hit your BLL code, as follows: