Export Datagrid Data to Excel error ‘DataGridLinkButton’ must be placed inside a form tag with runat=server.

I have been browsing on web to find the exact solution. Unfortunately, lots of different suggestions which also not working, and finally I have found an article that’s very interesting.

One reason this error comes up is when the datagrid contains a server control, so you need to verify it before complaining.

There are 2 (maybe more) ways to solve this issue that I have proven and tested.

1. Disable features of the datagrid like sorting and remove unnecessary server controls.

2. If the above solution is not possible because you really need it. So, what you need to do is to clear the server controls on the fly, means you have to implement a sort of mechanism to work, but don’t worry about it there’s already a script that will help you to ease the work.

  1.  
  2.     Public Sub ClearControls(ByVal control As Control)
  3.         'This function is used to Clear all the controls in the datagrid
  4.         'Will Clear all the ServerControls Which are in the Datagrid
  5.         'Such as edit command, paging, checkboxes in datagrid and all
  6.         'the server controls
  7.         'it will replace the server control with simple text for that control
  8.         'Where Can this be Used:
  9.         'Exporting Datagrid and Clearing Datagrid
  10.         Dim i As Integer
  11.         For i = control.Controls.Count - 1 To 0 Step -1
  12.             ClearControls(control.Controls(i))
  13.         Next i
  14.  
  15.         If Not TypeOf control Is System.Web.UI.WebControls.TableCell Then
  16.             If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
  17.                 Dim literal As New LiteralControl
  18.                 control.Parent.Controls.Add(literal)
  19.                 Try
  20.                     literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
  21.                 Catch
  22.                 End Try
  23.                 control.Parent.Controls.Remove(control)
  24.             Else
  25.                 If Not (control.GetType().GetProperty("Text") Is Nothing) Then
  26.                     Dim literal As New LiteralControl
  27.                     control.Parent.Controls.Add(literal)
  28.                     literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
  29.                     control.Parent.Controls.Remove(control)
  30.                 End If
  31.             End If
  32.         End If
  33.         Return
  34.     End Sub 'ClearControls
  35.  
  36.     Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
  37.         Response.Clear()
  38.         Dim filename As String = "filename_" & Strings.Format(DateTime.Today, "MM-dd-yyyy") & ".xls"
  39.  
  40.         Response.Clear()
  41.         Response.AddHeader("content-disposition", "attachment; filename=" & filename)
  42.         Response.Charset = ""
  43.         Response.ContentType = "application/vnd.xls"
  44.         Me.EnableViewState = False
  45.         Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
  46.         Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
  47.         Response.Buffer = False
  48.         ClearControls(DataGrid1)
  49.         Me.DataGrid1.RenderControl(htmlWrite)
  50.         Response.Write(stringWrite.ToString())
  51.         'stringWrite.Dispose()
  52.         'htmlWrite.Dispose()
  53.  
  54.         Response.End()
  55.  
  56.         'Download()
  57.  
  58.     End Sub

, , ,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth