如何使用GridView的MVC中无需添加形式RUNAT服务器? [英] How to use gridView in Mvc without adding form runat server?

查看:180
本文介绍了如何使用GridView的MVC中无需添加形式RUNAT服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题太简单了,但puzziling。我有一个开发一个MVC页面学习。我用中继服务器控件。

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>

也:(查看)

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

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

我的中继服务器控件做工不错。但我已经添加GridView控件。返回一个错误我:

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>

查看:



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

错误**:类型GridView的控制ContentPlaceHolder2_GridView1'必须放在与RUNAT =服务器的表单标签内**

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

我的理解,并通过添加RUNAT形式解决服务器probblem。但我不明白什么原因呢?直放站和GridView的服务器控件。目前仅在中继器的使用没有错误中继器。 GridView控件返回错误。为什么呢?

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?

推荐答案

你在做什么?在ASP.NET MVC应用程序的服务器端控件?请不要。停止这种疯狂。

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

中继器可能会奏效,因为它不依赖于ViewState的,但GridView控件 - >忘记这一点。也忘了复读。也忘记任何有 =服务器在ASP.NET MVC应用程序(除了内容占位符,如果你正在使用的WebForms视图引擎)。

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).

在ASP.NET MVC中,你可以使用的WebGrid 助手来解析一个网格。你可以用编辑器或显示模板替换您的中继器。

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.

真的是没有做ASP.NET MVC,如果你需要使用GridView的和类似的东西点。使用经典的WebForm用于这一目的。

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.

这篇关于如何使用GridView的MVC中无需添加形式RUNAT服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆