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. Adding control and serverside script to page dynamically

Adding control and serverside script to page dynamically

Scheduled Pinned Locked Moved ASP.NET
sysadmintoolsquestion
9 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.
  • F Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    I have some text (shown below) which I want to add to the page. This text contains server side script which I would also like to add dynamically. Is this a possibility? So far I have attempted to add the text as controls using Page.ParseControl. This successfully adds the controls (textbox and button) to the page but the button events are not working (postback so far looks good). After researching this further, several possibilities came into play. The first was CodeCompiler and the second was Dynamic Language Extensibility Model (DLEM). I have not read too much into DLEM but the article I read talked mostly about IronPython so this approach may not work in my scenario. In any case, both of these seem like overkill. I figured before I continue with my research, I would ask for suggestions from the community. Here are my questions: 1) Are the above possibilities a reasonable approach? 2) Is there something else I should be looking into? 3) Is it even possible to add server side scripts to a page dynamically? Thanks in advance.

    protected override void OnInit(EventArgs e)
    {
    
        string formText = @"
    
    protected void Page\_Load(object sender, EventArgs e)
    {
        
    }
    
    protected void Button1\_Click(object sender, EventArgs e)
    {
        ((Button)sender).Text = TextBox1.Text;
    }
    

    ";
    Control dControl = Page.ParseControl(formText);

        Page.Form.Controls.Add(dControl);
    
        base.OnInit(e);
    }
    
    N N 2 Replies Last reply
    0
    • F Fayu

      I have some text (shown below) which I want to add to the page. This text contains server side script which I would also like to add dynamically. Is this a possibility? So far I have attempted to add the text as controls using Page.ParseControl. This successfully adds the controls (textbox and button) to the page but the button events are not working (postback so far looks good). After researching this further, several possibilities came into play. The first was CodeCompiler and the second was Dynamic Language Extensibility Model (DLEM). I have not read too much into DLEM but the article I read talked mostly about IronPython so this approach may not work in my scenario. In any case, both of these seem like overkill. I figured before I continue with my research, I would ask for suggestions from the community. Here are my questions: 1) Are the above possibilities a reasonable approach? 2) Is there something else I should be looking into? 3) Is it even possible to add server side scripts to a page dynamically? Thanks in advance.

      protected override void OnInit(EventArgs e)
      {
      
          string formText = @"
      
      protected void Page\_Load(object sender, EventArgs e)
      {
          
      }
      
      protected void Button1\_Click(object sender, EventArgs e)
      {
          ((Button)sender).Text = TextBox1.Text;
      }
      

      ";
      Control dControl = Page.ParseControl(formText);

          Page.Form.Controls.Add(dControl);
      
          base.OnInit(e);
      }
      
      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      You can't add the server side script to the page from you code-behind. Think about it, what would be the point? You are already in the code-behind, so just add the controls. You are trying to make it much more complicated than it is.


      I know the language. I've read a book. - _Madmatt

      F 1 Reply Last reply
      0
      • F Fayu

        I have some text (shown below) which I want to add to the page. This text contains server side script which I would also like to add dynamically. Is this a possibility? So far I have attempted to add the text as controls using Page.ParseControl. This successfully adds the controls (textbox and button) to the page but the button events are not working (postback so far looks good). After researching this further, several possibilities came into play. The first was CodeCompiler and the second was Dynamic Language Extensibility Model (DLEM). I have not read too much into DLEM but the article I read talked mostly about IronPython so this approach may not work in my scenario. In any case, both of these seem like overkill. I figured before I continue with my research, I would ask for suggestions from the community. Here are my questions: 1) Are the above possibilities a reasonable approach? 2) Is there something else I should be looking into? 3) Is it even possible to add server side scripts to a page dynamically? Thanks in advance.

        protected override void OnInit(EventArgs e)
        {
        
            string formText = @"
        
        protected void Page\_Load(object sender, EventArgs e)
        {
            
        }
        
        protected void Button1\_Click(object sender, EventArgs e)
        {
            ((Button)sender).Text = TextBox1.Text;
        }
        

        ";
        Control dControl = Page.ParseControl(formText);

            Page.Form.Controls.Add(dControl);
        
            base.OnInit(e);
        }
        
        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        I think what you're trying to do is something along these lines (excuse my VB code - you'll have to trasnlate it yourself if you want) First, add a placeholder control on the form where you want to add the new control, and also a literal (just so we can test things_ -eg:

        Then in your code behind, add this: Private Sub btnNewButton_Click(ByVal Sender As Object, ByVal e As EventArgs) litClick.Text = "Clicked!" End Sub and in the Page_Load event, add this: Dim btn As New Button btn.ID = "btnNewButton" btn.Text = "My new button" AddHandler btn.Click, AddressOf btnNewButton_Click phHere.Controls.Add(btn)

        F 1 Reply Last reply
        0
        • N NeverHeardOfMe

          I think what you're trying to do is something along these lines (excuse my VB code - you'll have to trasnlate it yourself if you want) First, add a placeholder control on the form where you want to add the new control, and also a literal (just so we can test things_ -eg:

          Then in your code behind, add this: Private Sub btnNewButton_Click(ByVal Sender As Object, ByVal e As EventArgs) litClick.Text = "Clicked!" End Sub and in the Page_Load event, add this: Dim btn As New Button btn.ID = "btnNewButton" btn.Text = "My new button" AddHandler btn.Click, AddressOf btnNewButton_Click phHere.Controls.Add(btn)

          F Offline
          F Offline
          Fayu
          wrote on last edited by
          #4

          Thanks for your response. Thats not exactly what I am trying to do. I want to be able to add dynamic code after my app has been build... just lke VSA.

          1 Reply Last reply
          0
          • N Not Active

            You can't add the server side script to the page from you code-behind. Think about it, what would be the point? You are already in the code-behind, so just add the controls. You are trying to make it much more complicated than it is.


            I know the language. I've read a book. - _Madmatt

            F Offline
            F Offline
            Fayu
            wrote on last edited by
            #5

            I want to be able to be able to add scripts AFTER my application has already been compiled. Like VSA. In this example I have added it to my codebehind but the real-life example will not have the string in the code behind.

            modified on Sunday, June 20, 2010 10:49 AM

            N 1 Reply Last reply
            0
            • F Fayu

              I want to be able to be able to add scripts AFTER my application has already been compiled. Like VSA. In this example I have added it to my codebehind but the real-life example will not have the string in the code behind.

              modified on Sunday, June 20, 2010 10:49 AM

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Why would you? Without a great deal more complexity you can't. There are other ways to accomplish your task I'm sure, why don't you tell us what you want to accomplish and what you think you are gaining by trying to insert code in the manner.


              I know the language. I've read a book. - _Madmatt

              F 1 Reply Last reply
              0
              • N Not Active

                Why would you? Without a great deal more complexity you can't. There are other ways to accomplish your task I'm sure, why don't you tell us what you want to accomplish and what you think you are gaining by trying to insert code in the manner.


                I know the language. I've read a book. - _Madmatt

                F Offline
                F Offline
                Fayu
                wrote on last edited by
                #7

                I am trying to create a 'Content Management System' type of app. I want to add 'pages' dynamically with server controls markup and attach them to events. So bascially, I want to be able to write an entire asp.net page w/ server side scripts in text and execute it at run-time. What I have been able to accomplish so far is creating the control. However, hooking on to the events is whats causing me problems.

                N 1 Reply Last reply
                0
                • F Fayu

                  I am trying to create a 'Content Management System' type of app. I want to add 'pages' dynamically with server controls markup and attach them to events. So bascially, I want to be able to write an entire asp.net page w/ server side scripts in text and execute it at run-time. What I have been able to accomplish so far is creating the control. However, hooking on to the events is whats causing me problems.

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  You do understand that SERVER side scripts are processed on the SERVER? Injecting them in the page on the server is not going to accomplsih anything. Take a look at other CMS applications such as SharePoint and you will fined they use templates and page level parsers to inject dynamic controls into the output stream. To add a control and event handler to the page you do something like this.

                  protected void OnInit(...)
                  {
                  Button myBtn = new Button();
                  myBtn.Text = "Some text";
                  myBtn.Click += new OnClick;

                  Page.Controls.Add(myBtn);
                  }

                  protected void OnClick(object sender, EventArgs e)
                  {
                  ...
                  }


                  I know the language. I've read a book. - _Madmatt

                  F 1 Reply Last reply
                  0
                  • N Not Active

                    You do understand that SERVER side scripts are processed on the SERVER? Injecting them in the page on the server is not going to accomplsih anything. Take a look at other CMS applications such as SharePoint and you will fined they use templates and page level parsers to inject dynamic controls into the output stream. To add a control and event handler to the page you do something like this.

                    protected void OnInit(...)
                    {
                    Button myBtn = new Button();
                    myBtn.Text = "Some text";
                    myBtn.Click += new OnClick;

                    Page.Controls.Add(myBtn);
                    }

                    protected void OnClick(object sender, EventArgs e)
                    {
                    ...
                    }


                    I know the language. I've read a book. - _Madmatt

                    F Offline
                    F Offline
                    Fayu
                    wrote on last edited by
                    #9

                    I understand the server side scripts are executed in the server but I figured that there has to be a point during the life cyle where the markup, including the scripts, are parsed and compiled into dlls. This is where I wanted to inject my text. Seems like I may have to find another solution. Thanks for your input.

                    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