4

それで、私はあなたのサイトを調査しましたが、私の状況は独特です。Web コントロール .ascx があり、グリッドビューがあり、コードは次のようになります。

<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 ページにも追加しようとしましたが、オーバーライドの方法が見つからないというエラーが引き続き発生します。

間違っている点を見つけていただければ助かります。よろしくお願いします。よろしく、

4

3 に答える 3

5

WebControl (ascx) には<body>タグを含めないでください。

HTML フラグメントを生成し、HTML ページ上 (内部) およびasp:Form要素内に配置する必要があります。

于 2013-06-25T19:03:40.330 に答える
0

2 つの手順に従います

ステップ1

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

ステップ2

EnableEventValidation="false"
于 2015-05-14T05:31:03.080 に答える