必须将带有'GridView'类型的控件'GridView1'放在带有runat = server的表单标签内。

时间:2011-04-12 07:43:55

标签: asp.net vb.net

我的My dataGridVew

有问题

我正在尝试将数据从我的数据库导出到excel fie

这是一个小页面,其中只有数据网格视图和要导出的按钮,如下所示:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="SID" DataSourceID="SqlDataSource1" EnableModelValidation="True">
    <Columns>
        <asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" 
            ReadOnly="True" SortExpression="SID" />
        <asp:BoundField DataField="G1Q1" HeaderText="G1Q1" SortExpression="G1Q1" />
        <asp:BoundField DataField="G1Q2" HeaderText="G1Q2" SortExpression="G1Q2" />
        <asp:BoundField DataField="G1Q3" HeaderText="G1Q3" SortExpression="G1Q3" />
        <asp:BoundField DataField="G1Q4" HeaderText="G1Q4" SortExpression="G1Q4" />
        <asp:BoundField DataField="G1Q5" HeaderText="G1Q5" SortExpression="G1Q5" />
        <asp:BoundField DataField="G1Q6" HeaderText="G1Q6" SortExpression="G1Q6" />
        <asp:BoundField DataField="G1Q7" HeaderText="G1Q7" SortExpression="G1Q7" />
        <asp:BoundField DataField="G1Q8" HeaderText="G1Q8" SortExpression="G1Q8" />
        <asp:BoundField DataField="G1Q9" HeaderText="G1Q9" SortExpression="G1Q9" />
        <asp:BoundField DataField="G1Q10" HeaderText="G1Q10" SortExpression="G1Q10" />
        <asp:BoundField DataField="G1Q11" HeaderText="G1Q11" SortExpression="G1Q11" />
        <asp:BoundField DataField="G1Q12" HeaderText="G1Q12" SortExpression="G1Q12" />
        <asp:BoundField DataField="G1Q13" HeaderText="G1Q13" SortExpression="G1Q13" />
        <asp:BoundField DataField="G2Q1" HeaderText="G2Q1" SortExpression="G2Q1" />
        <asp:BoundField DataField="G2Q2" HeaderText="G2Q2" SortExpression="G2Q2" />
        <asp:BoundField DataField="G2Q3" HeaderText="G2Q3" SortExpression="G2Q3" />
        <asp:BoundField DataField="G2Q4" HeaderText="G2Q4" SortExpression="G2Q4" />
        <asp:BoundField DataField="G2Q5" HeaderText="G2Q5" SortExpression="G2Q5" />
        <asp:BoundField DataField="G2Q6" HeaderText="G2Q6" SortExpression="G2Q6" />
        <asp:BoundField DataField="G2Q7" HeaderText="G2Q7" SortExpression="G2Q7" />
        <asp:BoundField DataField="G2Q8" HeaderText="G2Q8" SortExpression="G2Q8" />
        <asp:BoundField DataField="G2Q9" HeaderText="G2Q9" SortExpression="G2Q9" />
        <asp:BoundField DataField="G2Q10" HeaderText="G2Q10" SortExpression="G2Q10" />
        <asp:BoundField DataField="G2Q11" HeaderText="G2Q11" SortExpression="G2Q11" />
        <asp:BoundField DataField="G3P1Q1" HeaderText="G3P1Q1" 
            SortExpression="G3P1Q1" />
        <asp:BoundField DataField="G3P1Q2" HeaderText="G3P1Q2" 
            SortExpression="G3P1Q2" />
        <asp:BoundField DataField="G3P1Q3" HeaderText="G3P1Q3" 
            SortExpression="G3P1Q3" />
        <asp:BoundField DataField="G3P1Q4" HeaderText="G3P1Q4" 
            SortExpression="G3P1Q4" />
        <asp:BoundField DataField="G3P1Q5" HeaderText="G3P1Q5" 
            SortExpression="G3P1Q5" />
        <asp:BoundField DataField="G3P1Q6" HeaderText="G3P1Q6" 
            SortExpression="G3P1Q6" />
        <asp:BoundField DataField="G3P1Q7" HeaderText="G3P1Q7" 
            SortExpression="G3P1Q7" />
        <asp:BoundField DataField="G3P1Q8" HeaderText="G3P1Q8" 
            SortExpression="G3P1Q8" />
        <asp:BoundField DataField="G3P1Q9" HeaderText="G3P1Q9" 
            SortExpression="G3P1Q9" />
        <asp:BoundField DataField="G3P1Q10" HeaderText="G3P1Q10" 
            SortExpression="G3P1Q10" />
        <asp:BoundField DataField="G3P2Q1" HeaderText="G3P2Q1" 
            SortExpression="G3P2Q1" />
        <asp:BoundField DataField="G3P2Q2" HeaderText="G3P2Q2" 
            SortExpression="G3P2Q2" />
        <asp:BoundField DataField="G3P2Q3" HeaderText="G3P2Q3" 
            SortExpression="G3P2Q3" />
        <asp:BoundField DataField="G3P2Q4" HeaderText="G3P2Q4" 
            SortExpression="G3P2Q4" />
        <asp:BoundField DataField="G3P2Q5" HeaderText="G3P2Q5" 
            SortExpression="G3P2Q5" />
        <asp:BoundField DataField="G3P2Q6" HeaderText="G3P2Q6" 
            SortExpression="G3P2Q6" />
        <asp:BoundField DataField="G3P2Q7" HeaderText="G3P2Q7" 
            SortExpression="G3P2Q7" />
        <asp:BoundField DataField="G3P2Q8" HeaderText="G3P2Q8" 
            SortExpression="G3P2Q8" />
        <asp:BoundField DataField="G3P2Q9" HeaderText="G3P2Q9" 
            SortExpression="G3P2Q9" />
        <asp:BoundField DataField="G3P2Q10" HeaderText="G3P2Q10" 
            SortExpression="G3P2Q10" />
        <asp:BoundField DataField="G3P2Q11" HeaderText="G3P2Q11" 
            SortExpression="G3P2Q11" />
        <asp:BoundField DataField="G3P3Q1" HeaderText="G3P3Q1" 
            SortExpression="G3P3Q1" />
        <asp:BoundField DataField="G3P3Q2" HeaderText="G3P3Q2" 
            SortExpression="G3P3Q2" />
        <asp:BoundField DataField="G3P3Q3" HeaderText="G3P3Q3" 
            SortExpression="G3P3Q3" />
        <asp:BoundField DataField="G3P3Q4" HeaderText="G3P3Q4" 
            SortExpression="G3P3Q4" />
        <asp:BoundField DataField="G3P3Q5" HeaderText="G3P3Q5" 
            SortExpression="G3P3Q5" />
        <asp:BoundField DataField="G3P3Q6" HeaderText="G3P3Q6" 
            SortExpression="G3P3Q6" />
        <asp:BoundField DataField="G3P3Q7" HeaderText="G3P3Q7" 
            SortExpression="G3P3Q7" />
        <asp:BoundField DataField="G3P3Q8" HeaderText="G3P3Q8" 
            SortExpression="G3P3Q8" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       ConnectionString="<%$ ConnectionStrings:HaronSurveyConnectionString %>"SelectCommand="SELECT * FROM [Results]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""

    Me.EnableViewState = False
    Dim oStringWriter As New System.IO.StringWriter()


    Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)


    'render html content to textwriter
    GridView1.RenderControl(oHtmlTextWriter)
    Response.Write(oStringWriter.ToString())
    Response.[End]()

End Sub

当我点击按钮时出现错误信息:“类型'GridView'的控件'GridView1'必须放在带有runat = server的表单标签内。”即使我把它放在形式???

4 个答案:

答案 0 :(得分:10)

只需编写此方法:

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

答案 1 :(得分:0)

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ad As New results()
    Dim dt As results.ResultsDataTable
    dt = ad.Read()

    Dim attachment As String = "attachment; filename=USurvey.xls"
    Response.ClearContent()
    Response.AddHeader("content-disposition", attachment)
    Response.ContentType = "application/vnd.ms-excel"
    Dim tab As String = ""
    For Each dc As DataColumn In dt.Columns
        Response.Write(tab + dc.ColumnName)
        tab = vbTab
    Next
    Response.Write(vbLf)

    Dim i As Integer
    For Each dr As DataRow In dt.Rows
        tab = ""
        For i = 0 To dt.Columns.Count - 1
            Response.Write(tab & dr(i).ToString())
            tab = vbTab
        Next
        Response.Write(vbLf)
    Next
    Response.[End]()
    'export to excel
End Sub

答案 2 :(得分:0)

只需将其添加到您的页面

即可
public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
 server control at run time. */
}

并将EnableEventValidation =“false”

你可以在这里找到它

GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag

RegisterForEventValidation can only be called during Render

答案 3 :(得分:-1)

使用ENTITY FRAMEWORK绑定数据网格时也会发生此错误。改为使用数据表。