loki
loki

Reputation: 2966

How to use gridView in Mvc without adding form runat server?

My question is too simple but puzziling. i have a develop a mvc pages to learn. i used Repeater server control.


<asp:Repeater ID="ProductList" runat="server">
<ItemTemplate>
<li>
<%#Eval("Name") %>
</li>
</ItemTemplate>

also : (VIEW)

public partial class ListProduct : ViewPage
{
   protected void Page_Load(object sender, EventArgs e)
   {

                ProductList.DataSource = ViewData["Products"];
                ProductList.DataBind();
   }
}

My Repeater Server control is working good. BUT i've added a GridView control. An Error returns me :


<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="id" HeaderText="" Visible="false" />
    <asp:BoundField DataField="Name" HeaderText="" Visible="true" />

    </Columns>
    </asp:GridView>

<asp:Repeater ID="ProductList" runat="server">
<ItemTemplate>
<li>
<%#Eval("Name") %>
</li>
</ItemTemplate>
</asp:Repeater>

</asp:Content>

VIEW:



            GridView1.DataSource = ViewData["Products"];
            GridView1.DataBind();

Error**: Control 'ContentPlaceHolder2_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.**

i understood and solved probblem by adding form runat server. But i dont understand REASON? Repeater and GridView are Server control. There is no error repeater in only repeater usage. GRidView return error. Why?

Upvotes: 0

Views: 3363

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

What are you doing? Server side controls in an ASP.NET MVC application? Please don't. Stop this madness.

No.

The Repeater might work as it doesn't depend on ViewState but GridView -> forget about that. Also forget about Repeater. Also forget about anything that has runat="server" in an ASP.NET MVC application (except the content placeholders if you are using the WebForms view engine).

In ASP.NET MVC you could use the WebGrid helper to render a grid. And you could replace your Repeater with Editor or Display templates.

There really is no point of doing ASP.NET MVC if you need to use GridViews and stuff like that. Use a classic WebForm for this purpose.

Upvotes: 6

Related Questions