【问题标题】:'GridView' must be placed inside a form tag with runat=server.'GridView' 必须放在带有 runat=server 的表单标签内。
【发布时间】:2013-06-25 18:56:22
【问题描述】:

所以,我研究了您的网站,我的情况很独特。我有一个 Web 控件 .ascx,上面有一个 gridview,代码如下所示:

<body>

    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
            OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10">
            <Columns>
                <asp:BoundField DataField="fb_login" HeaderText="User Id" runat="server" />
                <asp:BoundField DataField="fb_url" HeaderText="URL___" />
                <asp:BoundField DataField="fb_response" HeaderText="Answer: Did you find what you were looking for?" />
                <asp:BoundField DataField="fb_noResponse" HeaderText="No Response or Ignore" />
                <asp:BoundField DataField="fb_date" HeaderText="Date" />
                <asp:BoundField DataField="fb_serviceCall" HeaderText="Prevented Service Call" />
                <asp:BoundField DataField="fb_partsShipment" HeaderText="Prevented Parts Shipment" />
                <asp:BoundField DataField="fb_warranty" HeaderText="Under Warranty" />
                <asp:BoundField DataField="fb_cancel" HeaderText="Cancelled" />
                <asp:BoundField DataField="fb_none" HeaderText="None of the Above" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="download" Text=" Download to Excel " OnClick="dwnLoad" runat="server" />
    </div>

我有一个下载到 Excel 按钮,它执行以下代码:

protected void dwnLoad(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=kbNotification.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWriter);
            Response.End();
        }

当我按下按钮时,我收到以下错误:

 Exception Details: System.Web.HttpException: Control 'pagecontent_0_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error: 


Line 54:             Response.Cache.SetCacheability(HttpCacheability.NoCache);
Line 55:             Response.ContentType = "application/vnd.xls";
Line 56:             System.IO.StringWriter stringWrite = new System.IO.StringWriter();
Line 57:             System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
Line 58:             GridView1.RenderControl(htmlWriter);


Source File: C:\Projects\MEAU\trunk\Code\MEAU.Web\Components\SupportCenter\KB_Notification_rpt.ascx.cs    Line: 56 

我尝试在代码隐藏中添加以下方法:

public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }

这不起作用,因为这是一个 .ascx 页面,所以我也尝试将它添加到我的 default.aspx 页面中......并且仍然出现无法找到覆盖方法的错误。

如果您发现任何不正确的地方,请提供帮助,我们将不胜感激。 问候,

【问题讨论】:

    标签: c# gridview


    【解决方案1】:
    protected void Page_Load(object sender, EventArgs e)
        {           
           btnExcel_Click +=................
            if (!IsPostBack)
            {
                BindGridview();
            }
        }
    
        protected void BindGridview()
        {
            gvdetails.DataSourceID = "dsdetails";      
        }
    
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    
        protected void btnExcel_Click(object sender, ImageClickEventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvdetails.AllowPaging = false;
            BindGridview();
            //Change the Header Row back to white color
            gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
            {
                gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            gvdetails.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
    }
    

    使用此代码从gridView 下载数据到excel。

    【讨论】:

    • 刚被这个咬了一口。这其中的关键点是 VerifyRenderingInServerForm() 覆盖
    【解决方案2】:

    WebControl (ascx) 不应包含 &lt;body&gt; 标记。

    它会生成一个 HTML 片段,它应该被放置在(内部)一个 HTML 页面和asp:Form 元素内部。

    【讨论】:

    • 你是如何解决这个问题的?你能解释一下吗。我也有同样的问题!
    • @Kimi:你这里有2个答案,仔细阅读,当你的情况足够不同时,发布一个问题。
    【解决方案3】:

    分两步

    第一步

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    

    第 2 步

    EnableEventValidation="false"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-28
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2015-05-10
      相关资源
      最近更新 更多