FormView控件没有通过包含在“RUNAT =服务器”行的值
-
19-09-2019 - |
题
我在我的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 =“服务器”为表行,则iPrimaryGroup字段绑定100%,并传递到业务逻辑层正常。然而,在上面的代码的情况下,它是通过用零值。
谁能告诉我这是为什么或如何解决它?这是一个需要隐藏此表行,基于管理员或普通用户是否正在编辑它控制。即:某些字段只可写管理员和我想从视图隐藏的控件,如果用户不是管理员
解决方案 3
看来,这个功能是由设计,尽管这是不完全证实。
HTTP://weblogs.asp达网络/ rajbk /存档/ 2009/08/03 / FormView的结合-gotcha.aspx
在使用FormView的对象,如果你有一个嵌套的控制,然后双向数据绑定是不会正常工作。您可以访问控件的代码,你可以在数据获取,但它只是不会像它应该在你的业务逻辑层(BLL)的后端自动更新的价值。
幸运的是,有一种替代方法。的方式得到它的工作是创造ItemUpdating事件。这将有一个这样的签名:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
这使您可以访问到FormViewUpdateEventArgs,这反过来又可以让你,而他们在飞行中,他们打你的BLL代码之前更改到ObjectDataSource值,如下所示:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (frmProfile.FindControl("iPrimaryGroupDropDownList") != null)
{
DropDownList iPrimaryGroupDropDownList = ((DropDownList)frmProfile.FindControl("iPrimaryGroupDropDownList"));
e.NewValues["iPrimaryGroup"] = iPrimaryGroupDropDownList.Text;
}
}
其他提示
如果担心安全问题也许这可能更好地工作。
<tr>
<td colspan='2'>
<asp:panel runat='server' visible='<%= IsUserAdmin %>'>
<table>
<tr>
<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>
</table>
</asp:panel>
</td>
如果我没有记错面板内的任何标记将不被呈现如果可见=假
在此有一个镜头:
取出RUNAT =服务器属性
定义的CSS类
.hidden{ display:hidden;}
然后设置基于类属性的用户是否为管理员
<tr class='<%= if(IsUserAdmin) "" else "hidden" %>' >
不隶属于 StackOverflow