2016-09-22 87 views
0

我有一个使用GridView的MVC ASP.NET项目。 “启用编辑”,“启用删除”已安装,数据列转换为TemplateField。 SqlDataSource已连接。表有一个主键。为SqlDataSource安装“生成INSERT,UPDATE和DELETE语句”。点击“编辑”或“删除”生成页面重新加载,但数据不能被编辑或删除。编辑/删除项目在GridView中不起作用

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" > 
 
     <Columns> 
 
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
 
      <asp:TemplateField HeaderText="Id" SortExpression="Id"> 
 
       <EditItemTemplate> 
 
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label> 
 
       </EditItemTemplate> 
 
       <ItemTemplate> 
 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label> 
 
       </ItemTemplate> 
 
      </asp:TemplateField> 
 
      <asp:TemplateField HeaderText="Caption" SortExpression="Caption"> 
 
       <EditItemTemplate> 
 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Caption") %>'></asp:TextBox> 
 
       </EditItemTemplate> 
 
       <ItemTemplate> 
 
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Caption") %>'></asp:Label> 
 
       </ItemTemplate> 
 
      </asp:TemplateField> 
 
      <asp:TemplateField HeaderText="Text" SortExpression="Text"> 
 
       <EditItemTemplate> 
 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Text") %>'></asp:TextBox> 
 
       </EditItemTemplate> 
 
       <ItemTemplate> 
 
        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Text") %>'></asp:Label> 
 
       </ItemTemplate> 
 
      </asp:TemplateField> 
 
      
 
      <asp:CheckBoxField HeaderText="ABC" DataField="Checked" /> 
 
     </Columns> 
 
     <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
 
     <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
 
     <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
 
     <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
 
     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
 
     <SortedAscendingCellStyle BackColor="#FFF1D4" /> 
 
     <SortedAscendingHeaderStyle BackColor="#B95C30" /> 
 
     <SortedDescendingCellStyle BackColor="#F1E5CE" /> 
 
     <SortedDescendingHeaderStyle BackColor="#93451F" /> 
 
    </asp:GridView>

+2

在MVC项目中使用WebForms控件只有在混合了WebForms和MVC项目时才有效,并且仅在WebForms部件中使用控件。 – Richard

+0

谢谢aswer!所以我不能在View中使用GridView? – AN90

+2

您无法使用MVC的操作和控制器来使用WebForms控件(命名空间'System.Web.UI.WebControls'和'System.Web.UI.HtmlControls')。 WebForms控件假定页面生命周期在服务器上发生。 *可能有些功能可能被黑客入侵,但是您可以更好地切换范例:页面可以是MVC或WebForms:在该级别进行任何混合都可能存在持续性问题。 – Richard

回答

2

你不能使用的WebForms控件(命名空间System.Web.UI.WebControlsSystem.Web.UI.HtmlControls)与MVC的行动和控制器。

WebForms控件假定页面生命周期发生在服务器上:但是这一系列事件(和相关的代码组织)并不存在于MVC控制器和操作中。

你会发现在不同级别的黑客中已经完成的例子。为了取得一些成功的增长,但你会抵抗潮流。你最好完全切换范例:一个页面是MVC或WebForms。混合只是为了以后节省痛苦。