I want to get the GridView's html code before page render. I just used GridView.RenderControl() method. However I got a message that said GridView Type Control must be put in Form tag with runat=server. What should I do?
----------Here Comes My Code---------------
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Sometimes you can get "...must be placed inside a form tag with runat=server" error, when you deal with "asp:panel" or "asp:linkbutton" or whatever else scenarious when htmlform control needs to be rendered for the specified control at run time. The problem
can be solved (thanks, Alex) ovverriding
Page.VerifyRenderingInServerForm Method. Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
So, the code would look like the following (rendering asp:panel containing controls itself):
private void Page_Load(object sender, EventArgs e)
{
System.IO.StringWriter stringWriter =
new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter =
new System.Web.UI.HtmlTextWriter(stringWriter);
contentPanel.RenderControl(htmlWriter);
string s = stringWriter.ToString();
Response.Write(s);
Response.End();
}
public override void
VerifyRenderingInServerForm(Control control)
{
return;
}
Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
This posting is provided "AS IS" with on warranties, and confers no rights.
Very Thank you guys. Your line of code really save me time for research. I'm trying to do the exact same thing, and it works now with your override void VerifyRenderingInServerForm(Control control) code.
Everything that I've tried doesn't work, despite it seems to be successful for everyone else! There are lots of links that describe using the VerifyRenderingInServerForm, but it's not working for me. I'm currently in a VB.NET project that's using framework
3.5 and I've added this, which I believe, is similar to the code that you recommended that I add....
Public
Overloads
Sub VerifyRenderingInServerForm(ByVal control
As Control)
' Verifies that the control is rendered
End
Sub
You need to specify 'Overrides' instead of 'Overloads':
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
' Verifies that the control is rendered
' No code required here.
End Sub
What if I'm building the controls dynamicly (they are not as a part of the page when the VerifyRenderingInServerForm do fire)?!
...... Building the panel dynamicly from xsd template
Do While xpni.MoveNext()
Dim filename As String = xpni.Current.GetAttribute("id", String.Empty) & ".ascx"
Dim ieb As InputElementBase = CType(LoadControl(Pages.Dirs.Controls.InputElements & filename), InputElementBase)
p.Controls.Add(ieb)
p.Controls.Add(New LiteralControl("<table><tr style=""height:2px;""><td></td></tr></table>"))
Loop
Session("InputForm" & form.ID) = p
....... Then in other page
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
Return
End Sub
Public Shadows Function RenderControl(ByVal ctrl As Control) As String
Dim sb As StringBuilder = New StringBuilder
Dim tw As StringWriter = New StringWriter(sb)
Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
ctrl.RenderControl(hw)
Return sb.ToString
End Function
sBody = Tools.MakeHtmlBody(CType(Session("toolstoexcels"), DataTable), _
sTolList, _
CType(Session("second").id, Integer), _
RenderControl(CType(Session("InputForm1"), Panel)), _
RenderControl(CType(Session("InputForm2"), Panel)), _
RenderControl(CType(Session("InputForm3"), Panel)), _
RenderControl(CType(Session("InputForm4"), Panel)))
// Here I'm falling when performing RenderControl
...... and finaly it supose to do
Public Shared Function MakeHtmlBody(ByVal dt As DataTable, _
ByVal ToolList As String, _
ByVal SecondaryID As Integer, _
ByVal InputForm1 As String, _
ByVal InputForm2 As String, _
ByVal InputForm3 As String, _
ByVal InputForm4 As String) As String
.
.
.
s += InputForm1 + _
InputForm2 + _
InputForm3 + _
InputForm4 + _
"</td></tr>"
ganymade
Member
65 Points
21 Posts
Problem with GridView's RenderControl
Aug 12, 2006 02:13 AM|LINK
I want to get the GridView's html code before page render. I just used GridView.RenderControl() method. However I got a message that said GridView Type Control must be put in Form tag with runat=server. What should I do?
----------Here Comes My Code---------------
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gv = new GridView();
gv.DataSourceID = "SqlDataSource1";
this.Panel1.Controls.Add(gv);
gv.DataBind();
try
{
gv.RenderControl(htmlWrite);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
this.Label1.Text = stringWrite.ToString();
rexlin
Star
9403 Points
1751 Posts
Re: Problem with GridView's RenderControl
Aug 14, 2006 05:59 AM|LINK
Hi,ganymade:
Sometimes you can get "...must be placed inside a form tag with runat=server" error, when you deal with "asp:panel" or "asp:linkbutton" or whatever else scenarious when htmlform control needs to be rendered for the specified control at run time. The problem can be solved (thanks, Alex) ovverriding Page.VerifyRenderingInServerForm Method. Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
So, the code would look like the following (rendering asp:panel containing controls itself):
private void Page_Load(object sender, EventArgs e) { System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stringWriter); contentPanel.RenderControl(htmlWriter); string s = stringWriter.ToString(); Response.Write(s); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { return; }Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
This posting is provided "AS IS" with on warranties, and confers no rights.
ganymade
Member
65 Points
21 Posts
Re: Problem with GridView's RenderControl
Aug 14, 2006 02:52 PM|LINK
it does work, thank you
andiisuper
Member
32 Points
18 Posts
Re: Problem with GridView's RenderControl
Feb 23, 2007 02:18 PM|LINK
Thank you guys. Your line of code really save me time for research.
I'm trying to do the exact same thing, and it works now with your override void VerifyRenderingInServerForm(Control control)
code.
info.sunil
Member
2 Points
1 Post
Re: Problem with GridView's RenderControl
Jul 21, 2008 10:33 AM|LINK
Very Thank you guys. Your line of code really save me time for research. I'm trying to do the exact same thing, and it works now with your override void VerifyRenderingInServerForm(Control control) code.
regards
sunil
skydiverMN
Member
9 Points
38 Posts
Re: Problem with GridView's RenderControl
Apr 13, 2009 04:44 AM|LINK
Everything that I've tried doesn't work, despite it seems to be successful for everyone else! There are lots of links that describe using the VerifyRenderingInServerForm, but it's not working for me. I'm currently in a VB.NET project that's using framework 3.5 and I've added this, which I believe, is similar to the code that you recommended that I add....
What am I missing? Thoughts?
gridview excel VerifyRenderingInServerForm
MdV
Member
199 Points
254 Posts
Re: Problem with GridView's RenderControl
Apr 15, 2009 08:02 PM|LINK
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
' Verifies that the control is rendered
' No code required here.
End Sub
skydiverMN
Member
9 Points
38 Posts
Re: Problem with GridView's RenderControl
Apr 16, 2009 05:04 PM|LINK
What if I'm using all this code in a usercontrol? There isn't this method in the base class...... Now what?
ronwelb
Member
20 Points
11 Posts
Re: Problem with GridView's RenderControl
Jul 01, 2009 07:15 AM|LINK
Really Thanks. The working VB code
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control) End Submoodi_z
Member
14 Points
100 Posts
Re: Problem with GridView's RenderControl
Dec 24, 2009 07:25 AM|LINK
Hi,
What if I'm building the controls dynamicly (they are not as a part of the page when the VerifyRenderingInServerForm do fire)?!
...... Building the panel dynamicly from xsd template Do While xpni.MoveNext() Dim filename As String = xpni.Current.GetAttribute("id", String.Empty) & ".ascx" Dim ieb As InputElementBase = CType(LoadControl(Pages.Dirs.Controls.InputElements & filename), InputElementBase) p.Controls.Add(ieb) p.Controls.Add(New LiteralControl("<table><tr style=""height:2px;""><td></td></tr></table>")) Loop Session("InputForm" & form.ID) = p ....... Then in other page Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) Return End Sub Public Shadows Function RenderControl(ByVal ctrl As Control) As String Dim sb As StringBuilder = New StringBuilder Dim tw As StringWriter = New StringWriter(sb) Dim hw As HtmlTextWriter = New HtmlTextWriter(tw) ctrl.RenderControl(hw) Return sb.ToString End Function sBody = Tools.MakeHtmlBody(CType(Session("toolstoexcels"), DataTable), _ sTolList, _ CType(Session("second").id, Integer), _ RenderControl(CType(Session("InputForm1"), Panel)), _ RenderControl(CType(Session("InputForm2"), Panel)), _ RenderControl(CType(Session("InputForm3"), Panel)), _ RenderControl(CType(Session("InputForm4"), Panel))) // Here I'm falling when performing RenderControl ...... and finaly it supose to do Public Shared Function MakeHtmlBody(ByVal dt As DataTable, _ ByVal ToolList As String, _ ByVal SecondaryID As Integer, _ ByVal InputForm1 As String, _ ByVal InputForm2 As String, _ ByVal InputForm3 As String, _ ByVal InputForm4 As String) As String . . . s += InputForm1 + _ InputForm2 + _ InputForm3 + _ InputForm4 + _ "</td></tr>"