Reputation: 67
I have a MVC ASP.NET project with GridView. "Enable Editing", "Enable Deleting" are installed, data columns convert into a TemplateField. SqlDataSource is connected. Table have a primary key. For SqlDataSource installed "Generate INSERT, UPDATE and DELETE statements". On click for “Edit” or “Delete” generates page reloading, but data cannot be edited or deleted.
<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>
Upvotes: 0
Views: 168
Reputation: 109080
You cannot1 use WebForms controls (namespaces System.Web.UI.WebControls
and System.Web.UI.HtmlControls
) with MVC's actions and controllers.
WebForms controls assume the page life cycle happening on the server: but this series of events (and associated code organisation) does not exist with MVC controllers and actions.
1 You will find examples where this has been done with varying levels of hacks. To with some degress of success but you will be fighting against the tide. You are better off switching paradigm fully: a page is either MVC or WebForms. Mixing is just saving pain for later.
Upvotes: 2