简体   繁体   中英

Control xxx of type 'LinkButton' must be placed inside a form tag with runat=server

I have a DataGrid that I am trying to export when an ASP.NET Button is clicked.

错误屏幕

The exact error is:

Control 'ctl00_body_RollupDG_ctl02_btnShowGLdetail' of type 'LinkButton' must be placed inside a form tag with runat=server.

I found similar questions on here, but all seem to indicate that this comes from the ASP.NET control NOT being placed within a ContentPlaceHolder or a ContentPlaceHolder NOT being placed within a RunAt Server form.

I have both of these, so that is not the case here.

My ExportExcelFile method catches the HttpException at RenderControl() (as shown in the screenshot above). That code is as follows:

protected void ExportExcelFile(object Sender, EventArgs e) { //export to excel
    var grdResults = (periodCriteria.SelectedValue == "year") ? RollupDG : QuarterDG;
    var response = HttpContext.Current.Response;
    response.Clear();
    response.Charset = String.Empty;
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader("Content-Disposition", "attachment; filename=GlBudgetReport.xls");
    using (var sw = new StringWriter()) {
        using (var htw = new HtmlTextWriter(sw)) {
            grdResults.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
    }
}

As shown in my Find Results box below, there are several ASP.NET LinkButton controls, but each of them does contain the runat="server" clause.

查找结果

It appears the LinkButton data in the DataGrid has difficulty rendering as Plain Text .

If not, is there something in my export method that can be configured so that the DataGrid data is all interpreted as text?

I could create a blank DataGrid, then walk through the filled DataGrid, writing in only the text of each field - but I only want to do this if it is absolutely necessary.

I believe you just need to override the method that checks for a form. You don't need to add any logic to the method. Just add the following to your code behind:

public override void VerifyRenderingInServerForm(Control control) { } 

ETA: You may also need to set EnableEventValidation="false" in the <%@Page /> element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM