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

Is it possible to have a method that returns a string with ASP.NET code, is called in an aspx file, and should this code be run before the page reloads?

So I have something like the following:

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

GenerateTable () creates a table with asp: puts objects in them whose values ​​are determined by the asp: DataList data source.

Right now, I have correctly formed ASP.NET code. The problem is that it does not translate into HTML until the page loads, so you cannot see it.

Update: The reason I need a separate method for generating ASP.NET code is because I want the columns to appear customizable, so the same columns do not always appear on the website.

Update for Solution Attempt: I tried to create the following user control to insert my ASP.NET code, but it still faces the same problem. Am I doing it wrong?

User control:

  <%@ Control Language="C#" CodeBehind="TableGenerator.ascx.cs" Inherits="DagReport_WebRole.DynamicData.FieldTemplates.TableGenerator" %> <div class="tableRow"> <%= this.GetASPCode() %> </div> 

C # user control: 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> 
+4
source share
3 answers

Instead of directly entering the code, as you are trying now, you can add a user control to your ItemTemplate and execute your code in a user control. The code will dynamically create the required ASP.NET controls and add them to the user control. Collection of teams.

Thus, you will have the best of both worlds: the execution of dynamic code and (since user control is involved in the life cycle of the page) the correct formation of HTML.

UPDATE

Here is an example. Say you created a user control called "WebUserControl2.ascx" and added it to your home 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 WebUserControl2.ascx.cs code for managing web users, you add a shortcut, text box and 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 starts up and the 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, the DataList ItemCreated event, so the control will know which item is currently being created and is acting accordingly.

+10
source

No, because the GenerateTable() code will not be executed until the data is DataList to the DataList , which is at the time of Page_Load or later, depending on whether the event handler was performing the data binding instead of the built-in page event.

UPDATE:

To load data beyond the page life cycle, you need to do this outside the page life cycle.

To enter HTML on a page between page updates, you will need to invoke an AJAX call on the client side, for example:

Here we have a click handler that loads an HTML fragment from the server, but it can be easily adapted to return dynamic HTML through server logic or templates.

 <script type="text/javascript"> $(document).ready(function () { $("#myButton").click(function() { $.get("YourFile.html", function(data) { $("#YourDIV").append(data); }); }); }); </script> 

NOTE. Remember that page loading will destroy this dynamically loaded data.

+3
source

Not sure what exactly you are trying to accomplish, but what about nested repeaters:

 <asp:Repeater ID="repeaterConversation" runat="server"> <ItemTemplate> <asp:Repeater ID="rptPosts" runat="server"> <ItemTemplate> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater> 

Then in the code:

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load repeaterConversation.DataSource = source repeaterConversation.DataBind() End Sub Protected Sub repeaterConversation_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repeaterConversation.ItemDataBound If (item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem) Then //here you can do calulations based on the data bound in the parent repeater //and also bind to controls and child repeaters Repeater_Posts = item.FindControl("rptPosts") Repeater_NewPostBtn = item.FindControl("btnNewPost") Repeater_Posts.DataSource = source Repeater_Posts.DataBind() End If End Sub 
0
source

Source: https://habr.com/ru/post/1497510/


All Articles