【问题标题】:.NET framework 4.5 loads default edit template on GridView?.NET 框架 4.5 在 GridView 上加载默认编辑模板?
【发布时间】:2014-02-10 04:53:32
【问题描述】:

我创建了一个带有编辑功能的网格视图。单击 gridview 行编辑图像时,它使自定义编辑面板可见,用户可以编辑和保存 gridview 行数据,或者她/他可以单击取消按钮而不进行任何更新。它与.NET framwork 2.0. 完美配合

但是,问题在于.NET framework 4.5 -

当用户点击取消或保存按钮时,而不是加载 普通的gridview,它在顶部加载默认的编辑模板 用户之前点击过的 gridview 行。

那么,我该如何解决这个问题呢?这发生在我们 ASP.NET 网站中使用的所有 Gridview 中。

这是原始的 Gridview 行:Original GridRow

这是带有保存/取消功能的自定义编辑面板:Custom Edit Panel

这里是错误:Default Template loaded

【问题讨论】:

  • 显示您在网格视图行数据绑定事件中覆盖编辑项模板默认行为的代码。以及标记的 sn-p。
  • 我在 GridView 中使用了 OnRowEditing 事件
  • 这里是链接pastebin.com/CN8muLrQ
  • 在 OnRowEditing 事件处理程序中,我已将自定义编辑面板设为可见,并将 gridview 设为可见 false :pastebin.com/WvzWx4E5@AbideMasaraure
  • 谢谢。我去看看。

标签: c# asp.net .net .net-4.5


【解决方案1】:

我通常会在行数据绑定事件中实现你想要做的事情。

尝试像这样设置您的网格视图

<asp:Gridview id="ClientManagementGridView runat="server"/>
<columns>
<asp:TemplateField>
 <itemtemplate>
 //put edit button here
</itemtemplate>
<edititemtemplate>
// put  your update and cancel buttons here
</edititemtemplate>
</asp:Templatefield>
<asp:templatefield>
<itemtemplate>
<aspTextBox Text='<#Eval("ClientId") %>' runat="server" />
</itemtemplate>
 <edititemtemplate>
 <asp:Panel id="EditPanel">
 //your panel contents
 </asp:Panel>
</edititemtemplate>
</asp:templateField>
 <asp:BoundField DataField="ClientName" HeaderText="Name" />
 <asp:BoundField DataField="Address" HeaderText="Name" />
    </columns>
<asp:GridView>

现在在你的 gridview 的行数据绑定事件中放入这段神奇的代码

  If e.Row.RowState.ToString().Contains("Edit") Then
            Dim editGrid As GridView = TryCast(sender, GridView)
            Dim colSpan As Integer = editGrid.Columns.Count
            e.Row.Cells(0).Visible = True
            e.Row.Cells(1).Visible = True
            For i As Integer = 2 To colSpan - 1
                e.Row.Cells(i).Visible = False
                e.Row.Cells(i).Controls.Clear()
            Next

            e.Row.Cells(1).Attributes("ColSpan") = (colSpan).ToString()
  End if

如您所见,我通过清除默认控件然后更改单元格的列跨度以适合我的自定义编辑面板来覆盖默认编辑项模板。以及隐藏我想要隐藏的单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多