Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Could somebosy help me this rendercontrol issue

Could somebosy help me this rendercontrol issue

Scheduled Pinned Locked Moved ASP.NET
helphtmlcom
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T The Man from U N C L E

    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

    A Offline
    A Offline
    Abhishek Sur
    wrote on last edited by
    #5

    ;) 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

    1 Reply Last reply
    0
    • T The Man from U N C L E

      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

      E Offline
      E Offline
      Ersan Ercek
      wrote on last edited by
      #6

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

      1 Reply Last reply
      0
      • A Abhishek Sur

        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 a ContentItem of UpdatePanel, it will not take it as a direct element of HtmlForm. Hence it cant execute RenderControl 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-->**

        E Offline
        E Offline
        Ersan Ercek
        wrote on last edited by
        #7

        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.

        A 1 Reply Last reply
        0
        • E Ersan Ercek

          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.

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #8

          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

          E 1 Reply Last reply
          0
          • A Abhishek Sur

            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

            E Offline
            E Offline
            Ersan Ercek
            wrote on last edited by
            #9

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

            A 1 Reply Last reply
            0
            • E Ersan Ercek

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

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #10

              Yes.. I think there must be something wrong with the code. It must be working. If there is no UpdatePanel then only RenderControl 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

              E 1 Reply Last reply
              0
              • A Abhishek Sur

                Yes.. I think there must be something wrong with the code. It must be working. If there is no UpdatePanel then only RenderControl 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

                E Offline
                E Offline
                Ersan Ercek
                wrote on last edited by
                #11

                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); } } }

                A 1 Reply Last reply
                0
                • E Ersan Ercek

                  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); } } }

                  A Offline
                  A Offline
                  Abhishek Sur
                  wrote on last edited by
                  #12

                  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

                  E 1 Reply Last reply
                  0
                  • A Abhishek Sur

                    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

                    E Offline
                    E Offline
                    Ersan Ercek
                    wrote on last edited by
                    #13

                    At last, I managed it... I am saying as if I did something, just copied and pasted :). Thanks alot man.

                    A 1 Reply Last reply
                    0
                    • E Ersan Ercek

                      At last, I managed it... I am saying as if I did something, just copied and pasted :). Thanks alot man.

                      A Offline
                      A Offline
                      Abhishek Sur
                      wrote on last edited by
                      #14

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups