2013-06-25 40 views
3

所以,我研究了你的網站,我的情況是獨一無二的。我有一個Web控件的.ascx,它上有一個GridView和代碼如下所示:'GridView'必須放置在runat = server的窗體標籤內。

<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頁面中,並且仍然gettng錯誤,它無法找到overrid方法。

請幫助,如果你能發現任何不正確的,它將不勝感激。 Regards,

回答

4
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中。

+1

剛剛被這個人咬了。其中的關鍵點是VerifyRenderingInServerForm()覆蓋 –

5

WebControl(ascx)不應包含<body>標記。

它產生一個HTML片段,它應該放在HTML頁面的內部(asp:Form元素的內部)。

+0

你是怎麼解決這個問題?你能解釋一下嗎?我有同樣的問題! – Kimi

+0

@Kimi:你在這裏有兩個答案,仔細閱讀,當你的情況不同時,發佈Qustion。 –

0

按照兩個步驟

步驟1

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

步驟2

EnableEventValidation="false" 
相關問題