Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

error screen

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.

Find Results

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.

like image 441
jp2code Avatar asked Feb 11 '23 02:02

jp2code


1 Answers

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.

like image 123
Malk Avatar answered Feb 13 '23 19:02

Malk