Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically write ASP.NET code to .aspx file using C#

Is it possible to have a method that return a string with ASP.NET code get called in an aspx file and have that code run before the page is reloaded?

So pretty much have something like the following:

<asp:DataList //blahblah>
   <ItemTemplate>
      <%= GenerateTable() %>
   </ItemTemplate>
</asp:DataList>

GenerateTable() creates a table with asp:Label objects in them whose values are determined by the DataSource of the asp:DataList.

Right now, I have the ASP.NET code generating properly. The problem is that it's not translated into HTML before the page loads so you can't see it.

Update: The reason why I want a separate method for generating the ASP.NET code is because I want to make the columns displayed configurable, so the same columns are not always displayed by the website.

Update for Solution Attempt: I tried creating the following User Control to insert my ASP.NET code, but it's still running into the same problem. Am I doing this wrong?

User Control:

 <%@ Control Language="C#" CodeBehind="TableGenerator.ascx.cs"      Inherits="DagReport_WebRole.DynamicData.FieldTemplates.TableGenerator" %>

<div class="tableRow">
    <%= this.GetASPCode() %>
</div>

User Control C#: using System.Web.UI;

namespace DagReport_WebRole.DynamicData.FieldTemplates
{
    public partial class TableGenerator : System.Web.DynamicData.FieldTemplateUserControl
    {
        public string Code { get; set; }

        public string GetASPCode()
        {
             return Code;
        }
    }   
}

In My ASPX File:

            <ItemTemplate>
                <div class="dagRow"></div>
                <userControl:TableGenerator
                    Code="<%=GetRowASPCode()%>"></userControl:TableGenerator>
            </ItemTemplate>
like image 488
corgichu Avatar asked Dec 12 '22 12:12

corgichu


1 Answers

Instead of direct code injection like you're trying now, you can add a user control into your ItemTemplate and execute your code in the user control. The code would dynamically build needed ASP.NET controls and add them to user control's .Controls collection.

This way you will have the best of both worlds: your dynamic code execution and (since user control participates in page lifecycle) proper HTML generation.

UPDATE

Here is a basic example. Let's say you created user control "WebUserControl2.ascx" and added it to your main page:

<%@ Register src="WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>

Then you can add it to your DataList ItemTemplate:

 <asp:DataList runat="server" ID="DataList1">
 <ItemTemplate>
     <uc1:WebUserControl2 ID="MyWebUserControl" runat="server" />                
  </ItemTemplate>
 </asp:DataList>

And in your code for web user control WebUserControl2.ascx.cs you add a label, a textbox and a button:

protected void Page_Load(object sender, EventArgs e)
{
    Label Label1 = new Label();

    Label1.ID="Label1";
    Label1.Text = "Please enter info: ";
    this.Controls.Add(Label1);

    TextBox Textbox1 = new TextBox();
    Textbox1.ID="Textbox1";
    this.Controls.Add(Textbox1);


    Button Button1 = new Button();
    Button1.ID = "Button1";
    Button1.Text = "Submit";
    this.Controls.Add(Button1);

}

When the page runs and DataList is bound, you will get something like:

enter image description here

You can add properties to the user control and assign their values from the main page, for example DataList ItemCreated event, this way the control will be aware which item is currently being created and act accordingly.

like image 85
Yuriy Galanter Avatar answered Dec 23 '22 12:12

Yuriy Galanter