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