2016-09-22 38 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。混合只是爲了以後節省痛苦。