Could somebosy help me this rendercontrol issue
-
Ah.. I am proved wrong. He posted even as I was typing. Lucky break eh?
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk
;) Just was busy for the whole day bro... Got time to help few questions. Actually I reply mostly at night from here. Cheers. :rose:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Ah.. I am proved wrong. He posted even as I was typing. Lucky break eh?
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk
Sorry, I am normally patient guy but I was stuck with the problem so I got crazy, I read so many posts in regards to that problem but did not work, Additionally I was checking his other posts, he was replying other questions and I thought he gave up.. That was the reason to respost..
-
Hey.... I am very sorry, I didnt saw your last replies. First of all, it seems that your GridView is inside an updatepanel. If this is the case it cant be rendered. It will throw an error like
Control 'Grid' of type 'GridView'must be placed inside a form tag with runat=server.
Actually RenderControl looks for Form Element when it Renders. So if you place it as aContentItem
ofUpdatePanel
, it will not take it as a direct element of HtmlForm. Hence it cant executeRenderControl
of the control. So, here is the solution : //Place this in your code behind to remove the controls. This might be a general function which can be used for any types.private void ClearElements(Control ctrl)
{
for (int i = ctrl.Controls.Count - 1; i >= 0; i--)
{
ClearElements(ctrl.Controls[i]);
}if (!(ctrl is TableCell)) //Check for TableCell and replace with Literal { if (ctrl.GetType().GetProperty("SelectedItem") != null) { LiteralControl literal = new LiteralControl(); ctrl.Parent.Controls.Add(literal); try { literal.Text =ctrl.GetType().GetProperty("SelectedItem").GetValue(ctrl, null) as string; } catch {} ctrl.Parent.Controls.Remove(ctrl); } else if (ctrl.GetType().GetProperty("Text") != null)//Get parent control and remove it { LiteralControl literal = new LiteralControl(); ctrl.Parent.Controls.Add(literal); literal.Text = ctrl.GetType().GetProperty("Text").GetValue(ctrl, null) as string; ctrl.Parent.Controls.Remove(ctrl); } } return;
}
//Replace the code snippet in your code just where you wrote :
StringBuilder sb = new StringBuilder();
StringWriter strw = new StringWriter(sb);
HtmlTextWriter htmlw = new HtmlTextWriter(strw);
ClearElements(dgSiparisler);
HtmlForm form = new HtmlForm();
Controls.Add(form);
form.Controls.Add(dgSiparisler);
form.RenderControl(htmlw);snd.sendconMail("cinersan@hotmail.com", sb.ToString(), "Üyelik Bilgileri");
I think this will solve your issue. :thumbsup:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->**
Dear Abhishek, you are really a good guy with a lot of patience... I did not write earlier since I wanted to check the code first. As you said, I suspected from the update panels, and I have already removed it. Therefore I assume I do not need clear element. I checked in both ways anyway. The thing is, when I render gridview by adding form, program gives, a page can only have one form tag. When I try to render it alone I receive should be within form tag. I really do not understand. Thanks alot.
-
Dear Abhishek, you are really a good guy with a lot of patience... I did not write earlier since I wanted to check the code first. As you said, I suspected from the update panels, and I have already removed it. Therefore I assume I do not need clear element. I checked in both ways anyway. The thing is, when I render gridview by adding form, program gives, a page can only have one form tag. When I try to render it alone I receive should be within form tag. I really do not understand. Thanks alot.
Yes, But I have used this at least thousand times. It works. Are you using a simple gridview or any 3rd party gridviews? I dont know why you are getting this error :doh: :doh:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Yes, But I have used this at least thousand times. It works. Are you using a simple gridview or any 3rd party gridviews? I dont know why you are getting this error :doh: :doh:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using JavascriptI do not understand either, I send you may aspx page there are no 3rd party element. Could it be caused by a service pack or something like that? Am I supposed to load anything for VS08? I had a code for exporting data to excel in (VS03) where I did also render a gridview. It does not work either. It is deadlock..
-
I do not understand either, I send you may aspx page there are no 3rd party element. Could it be caused by a service pack or something like that? Am I supposed to load anything for VS08? I had a code for exporting data to excel in (VS03) where I did also render a gridview. It does not work either. It is deadlock..
Yes.. I think there must be something wrong with the code. It must be working. If there is no
UpdatePanel
then onlyRenderControl
does all. Create a new page, place a gridview with only few elements. And then try to use the same code. See if it exports in your server. If it does, increase the design a bit and gradually move towards the code you made and find out where exactly the problem is. :)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Yes.. I think there must be something wrong with the code. It must be working. If there is no
UpdatePanel
then onlyRenderControl
does all. Create a new page, place a gridview with only few elements. And then try to use the same code. See if it exports in your server. If it does, increase the design a bit and gradually move towards the code you made and find out where exactly the problem is. :)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using JavascriptDear Abhishek, Here are the test you asked for... Completely different project the same result. I know I am asking alor but, could it be possible for you to test it. Aspx page: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="dgSiparisler" runat="server"> <Columns> <asp:TemplateField HeaderText="Adet" HeaderStyle-HorizontalAlign="Left"> <ItemTemplate> <asp:Label ID="lblAdet" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DataAdi")%>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Left" /> <ItemStyle Width="50px" /> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html> cs page: using System; using System.Collections; using System.Configuration; using System.Data; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Test { public partial class _Default : System.Web.UI.Page { Class1 u = new Class1(); protected void Page_Load(object sender, EventArgs e) { dgSiparisler.DataSource = u.tbl_Al(); dgSiparisler.DataMember = u.tbl_Al().TableName; dgSiparisler.DataBind(); StringBuilder sb = new StringBuilder(); StringWriter strw = new StringWriter(sb); HtmlTextWriter htmlw = new HtmlTextWriter(strw); //ClearElements(dgSiparisler); HtmlForm form = new HtmlForm(); Controls.Add(form); form.Controls.Add(dgSiparisler); form.RenderControl(htmlw); } } }
-
Dear Abhishek, Here are the test you asked for... Completely different project the same result. I know I am asking alor but, could it be possible for you to test it. Aspx page: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="dgSiparisler" runat="server"> <Columns> <asp:TemplateField HeaderText="Adet" HeaderStyle-HorizontalAlign="Left"> <ItemTemplate> <asp:Label ID="lblAdet" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DataAdi")%>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Left" /> <ItemStyle Width="50px" /> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html> cs page: using System; using System.Collections; using System.Configuration; using System.Data; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Test { public partial class _Default : System.Web.UI.Page { Class1 u = new Class1(); protected void Page_Load(object sender, EventArgs e) { dgSiparisler.DataSource = u.tbl_Al(); dgSiparisler.DataMember = u.tbl_Al().TableName; dgSiparisler.DataBind(); StringBuilder sb = new StringBuilder(); StringWriter strw = new StringWriter(sb); HtmlTextWriter htmlw = new HtmlTextWriter(strw); //ClearElements(dgSiparisler); HtmlForm form = new HtmlForm(); Controls.Add(form); form.Controls.Add(dgSiparisler); form.RenderControl(htmlw); } } }
Ohh... I am sorry Ersan, as you are about to render the same page which is loading, the server will actually put the gridview inside the Form until it passes the Page_Load. Do this : Place
<%@ Page ... **EnableEventValidation = "false"** %>
in the page directive of your page. Now place this in the code behind :public override void VerifyRenderingInServerForm(Control control)
{
}Overriding this function will automatically disable the checking of server side form element. And as I have also disabled Event Validation, so any event validation error generation while rendering the Gridview will also be suppressed. So your server side code will look like :
StringBuilder sb = new StringBuilder();
StringWriter strw = new StringWriter(sb);
HtmlTextWriter htmlw = new HtmlTextWriter(strw);
this.dgSiparisler.RenderControl(htmlw);
string html = sb.ToString();No need to create a new form element. Just do it, I am sure it will work. :rose:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Ohh... I am sorry Ersan, as you are about to render the same page which is loading, the server will actually put the gridview inside the Form until it passes the Page_Load. Do this : Place
<%@ Page ... **EnableEventValidation = "false"** %>
in the page directive of your page. Now place this in the code behind :public override void VerifyRenderingInServerForm(Control control)
{
}Overriding this function will automatically disable the checking of server side form element. And as I have also disabled Event Validation, so any event validation error generation while rendering the Gridview will also be suppressed. So your server side code will look like :
StringBuilder sb = new StringBuilder();
StringWriter strw = new StringWriter(sb);
HtmlTextWriter htmlw = new HtmlTextWriter(strw);
this.dgSiparisler.RenderControl(htmlw);
string html = sb.ToString();No need to create a new form element. Just do it, I am sure it will work. :rose:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using JavascriptAt last, I managed it... I am saying as if I did something, just copied and pasted :). Thanks alot man.
-
At last, I managed it... I am saying as if I did something, just copied and pasted :). Thanks alot man.
You are most welcome ... budd.. :thumbsup::thumbsup:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript