FormView 未传递“runat=server”中包含的值排

发布于 2024-08-11 01:07:49 字数 649 浏览 11 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风流物 2024-08-18 01:07:49

如果安全是一个问题,也许这可能会更好

<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>

如果我没有弄错的话,如果visible=false,面板内的任何标记都不会被渲染

If security is a concern perhaps this might work better

<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>

If I'm not mistaken any markup within the panel will not be rendered if visible=false

陪你搞怪i 2024-08-18 01:07:49

尝试一下:

删除 runat=server 属性

定义一个 css 类

.hidden{ display:hidden;}

然后根据用户是否是管理员设置类属性

<tr class='<%= if(IsUserAdmin) "" else "hidden" %>' >

Have a shot at this:

Remove the runat=server attribute

Define a css class

.hidden{ display:hidden;}

Then set the class attribute based on whether or not the user is an admin

<tr class='<%= if(IsUserAdmin) "" else "hidden" %>' >
岁月静好 2024-08-18 01:07:49

看来此功能是设计使然,尽管尚未得到完全证实。

http://weblogs.asp.net/ rajbk/archive/2009/08/03/formview-binding-gotcha.aspx

使用 FormView 对象时,如果您有嵌套控件,则双向数据绑定将无法正常工作。您可以访问代码中的控件,也可以获取数据,但它不会像预期的那样自动更新业务逻辑层 (BLL) 后端的值。

幸运的是,有一个解决方法。让它工作的方法是为 ItemUpdating 创建一个事件。它将具有如下签名:

protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)

这使您可以访问 FormViewUpdateEventArgs,这反过来又允许您在 ObjectDataSource 值运行时以及命中 BLL 代码之前对它们进行更改,如下所示:

protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    if (frmProfile.FindControl("iPrimaryGroupDropDownList") != null)
    {
        DropDownList iPrimaryGroupDropDownList = ((DropDownList)frmProfile.FindControl("iPrimaryGroupDropDownList"));
        e.NewValues["iPrimaryGroup"] = iPrimaryGroupDropDownList.Text;
    }
}

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:

protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)

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:

protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    if (frmProfile.FindControl("iPrimaryGroupDropDownList") != null)
    {
        DropDownList iPrimaryGroupDropDownList = ((DropDownList)frmProfile.FindControl("iPrimaryGroupDropDownList"));
        e.NewValues["iPrimaryGroup"] = iPrimaryGroupDropDownList.Text;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文