Data binding FormView未传递包含在“中”的值;runat=server";一行

Data binding FormView未传递包含在“中”的值;runat=server";一行,data-binding,binding,formview,runat,Data Binding,Binding,Formview,Runat,我的FormView的EditItemTemplate中有以下代码: <tr id="primaryGroupRow" runat="server"> <td class="Fieldname">Primary Group:</td> <td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" Cs

我的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%绑定并正确地传递到业务逻辑层。但是,在上面的代码中,传递的值为零

有人能告诉我为什么会这样,或者如何避开它吗?这在一个需要隐藏此表行的控件中,具体取决于管理员或普通用户是否正在编辑它。ie:某些字段仅可由管理员写入,如果用户不是管理员,我想在视图中隐藏控件。

尝试一下:

删除runat=server属性

定义一个css类

.hidden{ display:hidden;}
然后根据用户是否是管理员设置class属性

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

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

<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,面板中的任何标记都不会被呈现,这似乎是出于设计,尽管这还没有得到确切的确认

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

幸运的是,有一个解决办法。使其工作的方法是为ItemUpdate创建一个事件。它将有如下签名:

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

我会试试看,然后告诉你。我认为这会带来安全风险,因为控制仍然会发送给客户。使用Firebug之类的工具,他们可以删除CSS类,然后修改这些控件中的数据。这些控件处理应用的安全权限。我会尝试一下,让你知道,但我怀疑它并不能满足我的需要。我真的不想重构代码,但会做需要做的事情。有人想过为什么会这样吗?谢谢Michael,但问题是它在FormView中。当DropDownList包含在一组指定runat=server的标记中时,iPrimaryGroup的绑定值将作为零传递到后端,而不管组件是否显示。我需要找到解决这个问题的方法,因为这些值没有被更新。嘿,Mike,我对此做了一些测试,当行具有runat=server属性时发现了相同的问题。不太清楚引擎盖下面发生了什么。但是,我使用上面的代码成功地检索到了这些值。我将对您的尝试给予赞扬,并投票支持您的答案,但我找到了真正的答案以及如何绕过它。