0

I have a asp.net application where in i am displaying a grid view whose code is shown below

 <center><asp:GridView ID="GridView1" runat="server" 
        RowStyle-HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" 
         GridLines="None">
 <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" BackColor="#FFFBD6" ForeColor="#333333"></RowStyle>
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
 </center>
 <center> <table>
 <tr>
 <td>
    <asp:Button ID="btnWordConvert" runat="server" Text="Button" 
        onclick="btnWordConvert_Click" />
</td>
</tr>
</table>
</center>

in cs

 protected void btnWordConvert_Click(object sender, EventArgs e)
{
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    Response.ClearContent();
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
    Response.Charset = "";
    Response.ContentType = "application/ms-word";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

}

i am getting error on GridView1.RenderControl(htw); line and the error is Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. Any help are appreciated.

1
  • 2
    the error tells itself the solution. Place your gridview inside the form tag Commented Dec 26, 2013 at 8:48

4 Answers 4

1

The reason you are getting this is, a server control is rendered outside of a form tag by calling GridView.RenderControl(HtmlTextWriter).

You could avoid this by overriding VerifyRenderingInServerForm.

Have a look at this.

Sign up to request clarification or add additional context in comments.

2 Comments

Its downloading.But when i open the downloaded document a File Coversion window is been opening in Microsoft word.The document shows only <div> </div>
It is because you are saving your response to an ms-word content type. If you want to save it as excel, you should change it to the correct content type.
1

Put your code inside form tag.

<form id="form1" runat="server">

   //put your code here
</form>

Comments

0

Please make sure that your gridview is inside form tag.

 <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" 
        RowStyle-HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" 
         GridLines="None">
                        ... ... ... 
    </asp:GridView>
 </form>

2 Comments

i tried that thing and its giving error as page can use only one server side tag
are u using master page approach
0

put following method

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.