By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,570 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

export datagrid to Excel

P: n/a
Hello All,

I ham using VS.NET 2003.

Have followed instructions from
http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several
other articles to no avail. I am pulling my hair out at this point. What I
have is this :

Members.aspx file:
<HTML>
<HEAD>
<title>members</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<mz:Header_Admin id="Heading1" runat="server"></mz:Header_Admin>
<mz:Menu id="Menu1" runat="server"></mz:Menu>
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 144px;
POSITION: absolute; TOP: 248px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="members.aspx">Members</asp:HyperLink>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
152px; POSITION: absolute; TOP: 280px"
runat="server" ForeColor="#EA9919">Add</asp:LinkButton>
<asp:LinkButton id="LinkButton2" style="Z-INDEX: 104; LEFT:
152px; POSITION: absolute; TOP: 312px"
runat="server" ForeColor="#EA9919">Remove</asp:LinkButton>
<asp:LinkButton id="LinkButton3" style="Z-INDEX: 102; LEFT:
152px; POSITION: absolute; TOP: 344px"
onclick="LinkButton3_Click" runat="server"
ForeColor="#EA9919">View</asp:LinkButton>
<asp:HyperLink id="HyperLink2" style="Z-INDEX: 103; LEFT: 136px;
POSITION: absolute; TOP: 376px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="#.aspx">Community</asp:HyperLink>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 504px;
POSITION: absolute; TOP: 320px" runat="server"
Text="Export to Excel"></asp:Button>
<BR>
<asp:Datagrid id="dg" runat="server" AutoGenerateColumns="True"
AllowSorting="true" AllowPaging="true"
CellPadding="3" PageSize="3" style="Z-INDEX: 107; LEFT:
464px; POSITION: absolute; TOP: 368px">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Edit"
CausesValidation="false" ID="btnView" Text="Edit" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>
</form>
</body>
</HTML>

in code behind file:

namespace myComponent.mzControls
{

public class members : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dg;
protected MZBusinessLayer bizObj;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
bizObj = new MZBusinessLayer();
dg.DataSource = bizObj.GetMembers();
DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton3.Click += new
System.EventHandler(this.LinkButton3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.dg.SelectedIndexChanged += new
System.EventHandler(this.dg_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.No Cache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

dg.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

public void LinkButton3_Click(object sender, System.EventArgs e)
{

}

private void dg_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
And the error message:

Control 'dg__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside
a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'dg__ctl2__ctl0' of
type 'DataGridLinkButton' must be placed inside a form tag with
runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:
[HttpException (0x80004005): Control 'dg__ctl2__ctl0' of type
'DataGridLinkButton' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Con trol control) +151
System.Web.UI.WebControls.LinkButton.AddAttributes ToRender(HtmlTextWriter
writer) +38
System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
writer) +17
System.Web.UI.WebControls.DataGridLinkButton.Rende r(HtmlTextWriter
writer) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.TableCell.RenderContents (HtmlTextWriter writer)
+55
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.WebControls.Table.RenderContents(Htm lTextWriter writer) +99
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.BaseDataList.Render(Html TextWriter writer) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
MarketZar.mzControls.members.Button1_Click(Object sender, EventArgs e)
+186
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

I don't have any controls outside of the form. What the heck is going on
here?


Jan 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
To get around this same exception, I had to hide the link button column when
rendering the datagrid to Excel. In my case, the link button column was the
last column so I used this code:

UserList_dg.Columns[UserList_dg.Columns.Count-1].Visible = false;

I only run that code when the datagrid was being rendered to Excel.

In another instance, where the link button text was data that needed to be
seen in Excel, I handled the ItemDatabound event and, in the event handler,
manually generated the content for the column as a link button when rendering
to a page and as text when rendering to Excel.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Frank" wrote:
Hello All,

I ham using VS.NET 2003.

Have followed instructions from
http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several
other articles to no avail. I am pulling my hair out at this point. What I
have is this :

Members.aspx file:
<HTML>
<HEAD>
<title>members</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<mz:Header_Admin id="Heading1" runat="server"></mz:Header_Admin>
<mz:Menu id="Menu1" runat="server"></mz:Menu>
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 144px;
POSITION: absolute; TOP: 248px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="members.aspx">Members</asp:HyperLink>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
152px; POSITION: absolute; TOP: 280px"
runat="server" ForeColor="#EA9919">Add</asp:LinkButton>
<asp:LinkButton id="LinkButton2" style="Z-INDEX: 104; LEFT:
152px; POSITION: absolute; TOP: 312px"
runat="server" ForeColor="#EA9919">Remove</asp:LinkButton>
<asp:LinkButton id="LinkButton3" style="Z-INDEX: 102; LEFT:
152px; POSITION: absolute; TOP: 344px"
onclick="LinkButton3_Click" runat="server"
ForeColor="#EA9919">View</asp:LinkButton>
<asp:HyperLink id="HyperLink2" style="Z-INDEX: 103; LEFT: 136px;
POSITION: absolute; TOP: 376px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="#.aspx">Community</asp:HyperLink>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 504px;
POSITION: absolute; TOP: 320px" runat="server"
Text="Export to Excel"></asp:Button>
<BR>
<asp:Datagrid id="dg" runat="server" AutoGenerateColumns="True"
AllowSorting="true" AllowPaging="true"
CellPadding="3" PageSize="3" style="Z-INDEX: 107; LEFT:
464px; POSITION: absolute; TOP: 368px">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Edit"
CausesValidation="false" ID="btnView" Text="Edit" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>
</form>
</body>
</HTML>

in code behind file:

namespace myComponent.mzControls
{

public class members : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dg;
protected MZBusinessLayer bizObj;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
bizObj = new MZBusinessLayer();
dg.DataSource = bizObj.GetMembers();
DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton3.Click += new
System.EventHandler(this.LinkButton3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.dg.SelectedIndexChanged += new
System.EventHandler(this.dg_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.No Cache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

dg.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

public void LinkButton3_Click(object sender, System.EventArgs e)
{

}

private void dg_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
And the error message:

Control 'dg__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside
a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'dg__ctl2__ctl0' of
type 'DataGridLinkButton' must be placed inside a form tag with
runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:
[HttpException (0x80004005): Control 'dg__ctl2__ctl0' of type
'DataGridLinkButton' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Con trol control) +151
System.Web.UI.WebControls.LinkButton.AddAttributes ToRender(HtmlTextWriter
writer) +38
System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
writer) +17
System.Web.UI.WebControls.DataGridLinkButton.Rende r(HtmlTextWriter
writer) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.TableCell.RenderContents (HtmlTextWriter writer)
+55
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.WebControls.Table.RenderContents(Htm lTextWriter writer) +99
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.BaseDataList.Render(Html TextWriter writer) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
MarketZar.mzControls.members.Button1_Click(Object sender, EventArgs e)
+186
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

I don't have any controls outside of the form. What the heck is going on
here?


Jan 26 '06 #2

P: n/a
Thanks for your excellent reply. I searched the Web for a day and a
half trying to solve this issue. Others may have given good advice but
yours was the post that made the most sense and easy to implement.

Dale wrote:
To get around this same exception, I had to hide the link button column when
rendering the datagrid to Excel. In my case, the link button column was the
last column so I used this code:

UserList_dg.Columns[UserList_dg.Columns.Count-1].Visible = false;

I only run that code when the datagrid was being rendered to Excel.

In another instance, where the link button text was data that needed to be
seen in Excel, I handled the ItemDatabound event and, in the event handler,
manually generated the content for the column as a link button when rendering
to a page and as text when rendering to Excel.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Frank" wrote:
Hello All,

I ham using VS.NET 2003.

Have followed instructions from
http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several
other articles to no avail. I am pulling my hair out at this point. What I
have is this :

Members.aspx file:
<HTML>
<HEAD>
<title>members</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<mz:Header_Admin id="Heading1" runat="server"></mz:Header_Admin>
<mz:Menu id="Menu1" runat="server"></mz:Menu>
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 144px;
POSITION: absolute; TOP: 248px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="members.aspx">Members</asp:HyperLink>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
152px; POSITION: absolute; TOP: 280px"
runat="server" ForeColor="#EA9919">Add</asp:LinkButton>
<asp:LinkButton id="LinkButton2" style="Z-INDEX: 104; LEFT:
152px; POSITION: absolute; TOP: 312px"
runat="server" ForeColor="#EA9919">Remove</asp:LinkButton>
<asp:LinkButton id="LinkButton3" style="Z-INDEX: 102; LEFT:
152px; POSITION: absolute; TOP: 344px"
onclick="LinkButton3_Click" runat="server"
ForeColor="#EA9919">View</asp:LinkButton>
<asp:HyperLink id="HyperLink2" style="Z-INDEX: 103; LEFT: 136px;
POSITION: absolute; TOP: 376px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="#.aspx">Community</asp:HyperLink>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 504px;
POSITION: absolute; TOP: 320px" runat="server"
Text="Export to Excel"></asp:Button>
<BR>
<asp:Datagrid id="dg" runat="server" AutoGenerateColumns="True"
AllowSorting="true" AllowPaging="true"
CellPadding="3" PageSize="3" style="Z-INDEX: 107; LEFT:
464px; POSITION: absolute; TOP: 368px">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Edit"
CausesValidation="false" ID="btnView" Text="Edit" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>
</form>
</body>
</HTML>

in code behind file:

namespace myComponent.mzControls
{

public class members : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dg;
protected MZBusinessLayer bizObj;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
bizObj = new MZBusinessLayer();
dg.DataSource = bizObj.GetMembers();
DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton3.Click += new
System.EventHandler(this.LinkButton3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.dg.SelectedIndexChanged += new
System.EventHandler(this.dg_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.No Cache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

dg.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

public void LinkButton3_Click(object sender, System.EventArgs e)
{

}

private void dg_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
And the error message:

Control 'dg__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside
a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'dg__ctl2__ctl0' of
type 'DataGridLinkButton' must be placed inside a form tag with
runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:
[HttpException (0x80004005): Control 'dg__ctl2__ctl0' of type
'DataGridLinkButton' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Con trol control) +151
System.Web.UI.WebControls.LinkButton.AddAttributes ToRender(HtmlTextWriter
writer) +38
System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
writer) +17
System.Web.UI.WebControls.DataGridLinkButton.Rende r(HtmlTextWriter
writer) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.TableCell.RenderContents (HtmlTextWriter writer)
+55
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.WebControls.Table.RenderContents(Htm lTextWriter writer) +99
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.BaseDataList.Render(Html TextWriter writer) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
MarketZar.mzControls.members.Button1_Click(Object sender, EventArgs e)
+186
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

I don't have any controls outside of the form. What the heck is going on
here?



Feb 15 '06 #3

P: n/a
I'm glad I was able to help!
--
Dale Preston
MCAD C#
MCSE, MCDBA
"greyhound" wrote:
Thanks for your excellent reply. I searched the Web for a day and a
half trying to solve this issue. Others may have given good advice but
yours was the post that made the most sense and easy to implement.

Dale wrote:
To get around this same exception, I had to hide the link button column when
rendering the datagrid to Excel. In my case, the link button column was the
last column so I used this code:

UserList_dg.Columns[UserList_dg.Columns.Count-1].Visible = false;

I only run that code when the datagrid was being rendered to Excel.

In another instance, where the link button text was data that needed to be
seen in Excel, I handled the ItemDatabound event and, in the event handler,
manually generated the content for the column as a link button when rendering
to a page and as text when rendering to Excel.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Frank" wrote:
Hello All,

I ham using VS.NET 2003.

Have followed instructions from
http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several
other articles to no avail. I am pulling my hair out at this point. What I
have is this :

Members.aspx file:
<HTML>
<HEAD>
<title>members</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<mz:Header_Admin id="Heading1" runat="server"></mz:Header_Admin>
<mz:Menu id="Menu1" runat="server"></mz:Menu>
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 144px;
POSITION: absolute; TOP: 248px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="members.aspx">Members</asp:HyperLink>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
152px; POSITION: absolute; TOP: 280px"
runat="server" ForeColor="#EA9919">Add</asp:LinkButton>
<asp:LinkButton id="LinkButton2" style="Z-INDEX: 104; LEFT:
152px; POSITION: absolute; TOP: 312px"
runat="server" ForeColor="#EA9919">Remove</asp:LinkButton>
<asp:LinkButton id="LinkButton3" style="Z-INDEX: 102; LEFT:
152px; POSITION: absolute; TOP: 344px"
onclick="LinkButton3_Click" runat="server"
ForeColor="#EA9919">View</asp:LinkButton>
<asp:HyperLink id="HyperLink2" style="Z-INDEX: 103; LEFT: 136px;
POSITION: absolute; TOP: 376px"
runat="server" ForeColor="#F2BD73"
NavigateUrl="#.aspx">Community</asp:HyperLink>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 504px;
POSITION: absolute; TOP: 320px" runat="server"
Text="Export to Excel"></asp:Button>
<BR>
<asp:Datagrid id="dg" runat="server" AutoGenerateColumns="True"
AllowSorting="true" AllowPaging="true"
CellPadding="3" PageSize="3" style="Z-INDEX: 107; LEFT:
464px; POSITION: absolute; TOP: 368px">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Edit"
CausesValidation="false" ID="btnView" Text="Edit" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>
</form>
</body>
</HTML>

in code behind file:

namespace myComponent.mzControls
{

public class members : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dg;
protected MZBusinessLayer bizObj;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
bizObj = new MZBusinessLayer();
dg.DataSource = bizObj.GetMembers();
DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton3.Click += new
System.EventHandler(this.LinkButton3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.dg.SelectedIndexChanged += new
System.EventHandler(this.dg_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.No Cache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

dg.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

public void LinkButton3_Click(object sender, System.EventArgs e)
{

}

private void dg_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
And the error message:

Control 'dg__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside
a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'dg__ctl2__ctl0' of
type 'DataGridLinkButton' must be placed inside a form tag with
runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:
[HttpException (0x80004005): Control 'dg__ctl2__ctl0' of type
'DataGridLinkButton' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Con trol control) +151
System.Web.UI.WebControls.LinkButton.AddAttributes ToRender(HtmlTextWriter
writer) +38
System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
writer) +17
System.Web.UI.WebControls.DataGridLinkButton.Rende r(HtmlTextWriter
writer) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.TableCell.RenderContents (HtmlTextWriter writer)
+55
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.WebControls.Table.RenderContents(Htm lTextWriter writer) +99
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
writer) +7
System.Web.UI.WebControls.BaseDataList.Render(Html TextWriter writer) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
MarketZar.mzControls.members.Button1_Click(Object sender, EventArgs e)
+186
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

I don't have any controls outside of the form. What the heck is going on
here?



Feb 17 '06 #4

P: n/a
Gents,

I have been able to generate the Excel document with no problems. Except
one thing that, of course, our users complain about.

The process seems to automatically insert a blank row at the top of the
Excel document. Or, in other words, it starts the output on Row 2.

Is there a way to prevent this and start at Row 1?

Thanx,

Shawn K Haase

===============================================

"Dale" wrote:
I'm glad I was able to help!
--
Dale Preston
MCAD C#
MCSE, MCDBA
"greyhound" wrote:
Thanks for your excellent reply. I searched the Web for a day and a
half trying to solve this issue. Others may have given good advice but
yours was the post that made the most sense and easy to implement.

Dale wrote:
To get around this same exception, I had to hide the link button column when
rendering the datagrid to Excel. In my case, the link button column was the
last column so I used this code:

UserList_dg.Columns[UserList_dg.Columns.Count-1].Visible = false;

I only run that code when the datagrid was being rendered to Excel.

In another instance, where the link button text was data that needed to be
seen in Excel, I handled the ItemDatabound event and, in the event handler,
manually generated the content for the column as a link button when rendering
to a page and as text when rendering to Excel.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Frank" wrote:

> Hello All,
>
> I ham using VS.NET 2003.
>
> Have followed instructions from
> http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several
> other articles to no avail. I am pulling my hair out at this point. What I
> have is this :
>
> Members.aspx file:
> <HTML>
> <HEAD>
> <title>members</title>
> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name="vs_defaultClientScript" content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <mz:Header_Admin id="Heading1" runat="server"></mz:Header_Admin>
> <mz:Menu id="Menu1" runat="server"></mz:Menu>
> <asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 144px;
> POSITION: absolute; TOP: 248px"
> runat="server" ForeColor="#F2BD73"
> NavigateUrl="members.aspx">Members</asp:HyperLink>
> <asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
> 152px; POSITION: absolute; TOP: 280px"
> runat="server" ForeColor="#EA9919">Add</asp:LinkButton>
> <asp:LinkButton id="LinkButton2" style="Z-INDEX: 104; LEFT:
> 152px; POSITION: absolute; TOP: 312px"
> runat="server" ForeColor="#EA9919">Remove</asp:LinkButton>
> <asp:LinkButton id="LinkButton3" style="Z-INDEX: 102; LEFT:
> 152px; POSITION: absolute; TOP: 344px"
> onclick="LinkButton3_Click" runat="server"
> ForeColor="#EA9919">View</asp:LinkButton>
> <asp:HyperLink id="HyperLink2" style="Z-INDEX: 103; LEFT: 136px;
> POSITION: absolute; TOP: 376px"
> runat="server" ForeColor="#F2BD73"
> NavigateUrl="#.aspx">Community</asp:HyperLink>
> <asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 504px;
> POSITION: absolute; TOP: 320px" runat="server"
> Text="Export to Excel"></asp:Button>
> <BR>
> <asp:Datagrid id="dg" runat="server" AutoGenerateColumns="True"
> AllowSorting="true" AllowPaging="true"
> CellPadding="3" PageSize="3" style="Z-INDEX: 107; LEFT:
> 464px; POSITION: absolute; TOP: 368px">
> <columns>
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:LinkButton runat="server" CommandName="Edit"
> CausesValidation="false" ID="btnView" Text="Edit" />
> </ItemTemplate>
> </asp:TemplateColumn>
> </columns>
> </asp:Datagrid>
> </form>
> </body>
> </HTML>
>
>
>
> in code behind file:
>
> namespace myComponent.mzControls
> {
>
> public class members : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.HyperLink HyperLink1;
> protected System.Web.UI.WebControls.LinkButton LinkButton1;
> protected System.Web.UI.WebControls.LinkButton LinkButton2;
> protected System.Web.UI.WebControls.LinkButton LinkButton3;
> protected System.Web.UI.WebControls.HyperLink HyperLink2;
> protected System.Web.UI.WebControls.Button Button1;
> protected System.Web.UI.WebControls.DataGrid dg;
> protected MZBusinessLayer bizObj;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if(!IsPostBack)
> {
> bizObj = new MZBusinessLayer();
> dg.DataSource = bizObj.GetMembers();
> DataBind();
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.LinkButton3.Click += new
> System.EventHandler(this.LinkButton3_Click);
> this.Button1.Click += new System.EventHandler(this.Button1_Click);
> this.dg.SelectedIndexChanged += new
> System.EventHandler(this.dg_SelectedIndexChanged);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
> private void Button1_Click(object sender, System.EventArgs e)
> {
> Response.Clear();
> Response.AddHeader("content-disposition",
> "attachment;filename=FileName.xls");
>
> Response.Charset = "";
>
> Response.Cache.SetCacheability(HttpCacheability.No Cache);
>
> Response.ContentType = "application/vnd.xls";
>
> System.IO.StringWriter stringWrite = new System.IO.StringWriter();
>
> System.Web.UI.HtmlTextWriter htmlWrite = new
> HtmlTextWriter(stringWrite);
>
> dg.RenderControl(htmlWrite);
>
> Response.Write(stringWrite.ToString());
>
> Response.End();
>
> }
>
> public void LinkButton3_Click(object sender, System.EventArgs e)
> {
>
> }
>
> private void dg_SelectedIndexChanged(object sender, System.EventArgs e)
> {
>
> }
> }
> }
>
>
> And the error message:
>
> Control 'dg__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside
> a form tag with runat=server.
> Description: An unhandled exception occurred during the execution of the
> current web request. Please review the stack trace for more information
> about the error and where it originated in the code.
>
> Exception Details: System.Web.HttpException: Control 'dg__ctl2__ctl0' of
> type 'DataGridLinkButton' must be placed inside a form tag with
> runat=server.
>
> Source Error:
>
> An unhandled exception was generated during the execution of the current web
> request. Information regarding the origin and location of the exception can
> be identified using the exception stack trace below.
>
> Stack Trace:
>
>
> [HttpException (0x80004005): Control 'dg__ctl2__ctl0' of type
> 'DataGridLinkButton' must be placed inside a form tag with runat=server.]
> System.Web.UI.Page.VerifyRenderingInServerForm(Con trol control) +151
> System.Web.UI.WebControls.LinkButton.AddAttributes ToRender(HtmlTextWriter
> writer) +38
> System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
> writer) +17
> System.Web.UI.WebControls.DataGridLinkButton.Rende r(HtmlTextWriter
> writer) +25
> System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
> System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
> System.Web.UI.WebControls.TableCell.RenderContents (HtmlTextWriter writer)
> +55
> System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
> System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
> System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
> System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
> writer) +7
> System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
> System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
> System.Web.UI.WebControls.Table.RenderContents(Htm lTextWriter writer) +99
> System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +29
> System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
> System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer) +72
> System.Web.UI.WebControls.WebControl.RenderContent s(HtmlTextWriter
> writer) +7
> System.Web.UI.WebControls.BaseDataList.Render(Html TextWriter writer) +27
> System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
> MarketZar.mzControls.members.Button1_Click(Object sender, EventArgs e)
> +186
> System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
> System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
> eventArgument) +57
> System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
> sourceControl, String eventArgument) +18
> System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
> System.Web.UI.Page.ProcessRequestMain() +1292
>
>
>
>
> --------------------------------------------------------------------------------
> Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
> Version:1.1.4322.2032
>
>
>
> I don't have any controls outside of the form. What the heck is going on
> here?
>
>
>
>
>


Mar 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.