Scenario :-->>
I have created a user Control, which have a Gridview Control(any Databound Control you can set) and one Export button
, which is used to Export Current displayed Data in Grid to Excel. After Creating user Control, i have created a new ASPX page
and registered this user control to this newly created ASPX page. and Simply Run this Project. But this give me a RUN Time
Error, saying that "Control 'UserControlID1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server".
So Get rid of this Problem, I have tried following Solution.
Solution :-->>
1. User Control ASPX :
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlRenderingInMainPage.ascx.cs" Inherits="testApplication.UserControl.UserControlRenderingInMainPage" %>
<asp:Panel ID="pnlUserControl" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True">
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Font-Italic="True" ForeColor="#FF66FF"
onclick="Button1_Click" Text="Export to Excel" Width="100px" Visible="false"/>
<br />
<br />
</asp:Panel>
2. User COntrol Code behind Page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace testApplication.UserControl
{
public partial class UserControlRenderingInMainPage : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindToGridView();
}
private void BindToGridView()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["attribiuteStr"].ConnectionString))
{
using (SqlDataAdapter da = new SqlDataAdapter("select * from GeneralInformation", con))
{
da.Fill(ds);
if (ds.Tables[0]!= null && ds.Tables[0].Rows.Count > 0)
{
Button1.Visible = true;
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=>MyTestFile_" + DateTime.Now.ToShortDateString() + ".xls");
Response.Charset = "";
Page.EnableViewState = false;
Response.Buffer = true;
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter ExcelHtmlWriter = new HtmlTextWriter(stringWrite);
pnlUserControl.RenderControl(ExcelHtmlWriter);
string style = @"<style> .text { mso-number-format:0\.00;text-align:right; } </style> ";
Response.Write(style);
Response.Write("<table><tr><td width = '100%' align = 'Left'><font face='verdana, arial' size='2'><b>Summary Details</b></font></td></tr></Table> ");
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}
}
}
3. My ASPX Page where i have used this newly created User COntrol :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderUserControlPage.aspx.cs" Inherits="testApplication.ASPX.RenderUserControlPage" %>
<%@ Register Src="~/UserControl/UserControlRenderingInMainPage.ascx" TagPrefix="UC1" TagName="UserControl1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<UC1:UserControl1 ID="UserControlID1" runat="server"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
4. Code behind for this ASPX Page :
using System;
using System.Web.UI;
using System.IO;
namespace testApplication.ASPX
{
public partial class RenderUserControlPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RenderUserControlToStringForm();
}
private string RenderUserControlToStringForm()
{
System.Web.UI.UserControl myControl = (System.Web.UI.UserControl)LoadControl("~/UserControl/UserControlRenderingInMainPage.ascx");
using (TextWriter myTextWriter = new StringWriter())
using (HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter))
{
myControl.RenderControl(myWriter);
return myTextWriter.ToString();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Do nothing */
}
public override bool EnableEventValidation
{
get { return false; }
set { /* Do nothing */}
}
}
}
Conclusion :-->>
And it Works like a charm.
Hope this helps to You.
Correct Me, If I am Wrong!!!