帮帮忙啊,什么没人理我啊,datagridtoexcel问题
aspx文件:
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
Text="Button"></asp:Button>
<asp:DataGrid id="WU1DataGrid1" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 64px" runat="server"
AllowSorting="True" AutoGenerateColumns="True" AllowPaging="True">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="btnview" Runat="server" CausesValidation="False" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
</form>
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer=true;
Response.ContentType="application/vnd.ms-excel";
Response.Charset="";
this.EnableViewState=false;
System.IO.StringWriter oStringWriter=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter=new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(WU1DataGrid1);
WU1DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//*********************************************************************************************************
private void ClearControls(Control control)
{
for(int i=control.Controls.Count-1;i>0;i--)
{
ClearControls(control.Controls[i]);
}
if(!(control is TableCell))
{
if(control.GetType().GetProperty("SelectedItem")!=null)
{
LiteralControl literal=new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text=(string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if(control.GetType().GetProperty("Text")!=null)
{
LiteralControl literal=new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text=(string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
出现错误:
Exception Details: System.Web.HttpException: Control 'WU1DataGrid1__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.
Source Error:
Line 504:
Line 505: this.ClearControls(WU1DataGrid1);
Line 506: WU1DataGrid1.RenderControl(oHtmlTextWriter);
Line 507: Response.Write(oStringWriter.ToString());
Line 508: Response.End();
Source File: c:\inetpub\wwwroot\xxh\dlsbh\main.aspx.cs Line: 506
可我明明都已放在<form runat=server>中的啊
帮帮忙,各位!!