Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...