“GridView控件'必须放在与RUNAT =服务器的表单标签内。 [英] 'GridView' must be placed inside a form tag with runat=server.

查看:690
本文介绍了“GridView控件'必须放在与RUNAT =服务器的表单标签内。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我研究了您的网站和我的情况是独一无二的。我有一个Web控件的.ascx,其上有一个GridView和代码看起来是这样的:

 <身体GT; 

< DIV>
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =false的AllowPaging =真正的
OnPageIndexChanging =GridView1_PageIndexChanging每页=10>
<柱体和GT;
< ASP:BoundField的数据字段=fb_login的HeaderText =用户ID=服务器/>
< ASP:BoundField的数据字段=fb_url的HeaderText =URL ___/>
< ASP:BoundField的数据字段=fb_response的HeaderText =答:你有没有找到你要找的东西? />
< ASP:BoundField的数据字段=fb_noResponse的HeaderText =无反应或忽略/>
< ASP:BoundField的数据字段=fb_date的HeaderText =日期/>
< ASP:BoundField的数据字段=fb_serviceCall的HeaderText =防止的服务呼叫/>
< ASP:BoundField的数据字段=fb_partsShipment的HeaderText =防止零件出货/>
< ASP:BoundField的数据字段=fb_warranty的HeaderText =在保修/>
< ASP:BoundField的数据字段=fb_cancel的HeaderText =已取消/>
< ASP:BoundField的数据字段=fb_none的HeaderText =以上皆非/>
< /专栏>
< / ASP:GridView的>
< ASP:按钮的ID =下载文本=下载到Excel的OnClick =dwnLoad=服务器/>
< / DIV>



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

 保护无效dwnLoad(对象发件人,EventArgs五)
{
Response.Clear();
Response.AddHeader(内容处置,附件;文件名= kbNotification.xls);
Response.Charset的=;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =应用程序/ vnd.xls
System.IO.StringWriter stringWrite =新System.IO.StringWriter();
System.Web.UI.HtmlTextWriter的HTMLWriter =新的HtmlTextWriter(stringWrite);
GridView1.RenderControl(的HTMLWriter);
到Response.End();
}

当我按下按钮,我歌厅以下错误:

 异常详细信息:System.Web.HttpException:类型的GridView'控制'pagecontent_0_GridView1'必须放在与RUNAT =服务器的表单标签内。 

源错误:


线54:Response.Cache.SetCacheability(HttpCacheability.NoCache);
线55:Response.ContentType =应用程序/ vnd.xls
线56:System.IO.StringWriter stringWrite =新System.IO.StringWriter();
线57:System.Web.UI.HtmlTextWriter的HTMLWriter =新的HtmlTextWriter(stringWrite);
线58:GridView1.RenderControl(的HTMLWriter);


源文件:C:\Projects\MEAU\trunk\Code\MEAU.Web\Components\SupportCenter\KB_Notification_rpt.ascx.cs线:56

我曾尝试下面的方法添加到代码隐藏:

 公共覆盖无效VerifyRenderingInServerForm(控制控制)
{
的回报;
}

这不起作用,因为这是一个页面的.ascx,所以我也试着将它添加到我的Default.aspx页......,现在还在gettng的错误,它无法找到overrid的方法。



请帮助,如果你能发现任何不正确,这将是大加赞赏。
的问候,


解决方案

 保护无效的Page_Load(对象发件人,EventArgs的发送) 
{
btnExcel_Click + = ................
如果
{
BindGridview()(的IsPostBack!);
}
}

保护无效BindGridview()
{
gvdetails.DataSourceID =dsdetails;
}

公共覆盖无效VerifyRenderingInServerForm(控制控制)
{
/ *验证控件呈现* /
}

保护无效btnExcel_Click(对象发件人,ImageClickEventArgs E)
{
Response.ClearContent();
将Response.Buffer = TRUE;
Response.AddHeader(内容处置的String.Format(附件;文件名= {0},Customers.xls));
Response.ContentType =应用程序/ MS-Excel的;
StringWriter的SW =新的StringWriter();
的HtmlTextWriter HTW =新的HtmlTextWriter(SW);
gvdetails.AllowPaging = FALSE;
BindGridview();
//改变标题行回白色
gvdetails.HeaderRow.Style.Add(背景色,#FFFFFF);
//应用stlye到GridView的头单元格
的for(int i = 0; I< gvdetails.HeaderRow.Cells.Count;我++)
{
gvdetails.HeaderRow.Cells [I] .Style.Add(背景色,#df5015);
}
gvdetails.RenderControl(HTW);
的Response.Write(sw.ToString());
到Response.End();
}

使用此代码从 GridView控件创先争优。


So, I have researched your site and my situation is unique. I have a Web control .ascx, it has a gridview on it and the code looks like this:

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

I have a download to Excel button that executes the following code:

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();
        }

When I press the button, I am geting the following error:

 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 

I have tried to add the following method to the codebehind:

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

this does not work because this is an .ascx page, so I have also tried to add it to my default.aspx page... and am still gettng errors that it cannot find the method of overrid.

Please help if you can spot anything that is incorrect, it would be much appreciated. Regards,

解决方案

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();
}

Use this code to download data from gridView to excel.

这篇关于“GridView控件'必须放在与RUNAT =服务器的表单标签内。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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