asp.net dynamic generation of ascx user control

Dynamically generate ascx user control to write click event to div in. aspx

**//
//
//
//
Because my ascx user control is dynamically generated with div and there is no click event, I haven't found the right one on the Internet for a long time
Finally, after reading an answer from Daniel's sp1234, I finally got a look
Record the process today
Paste ascx code first
/ / front end

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="student.ascx.cs" Inherits="one.scripts.teacher.student" %>
<div style="background-image:url(../image/173.jpg);height:95px;width:170px;margin:20px 20px 20px 45px;float:left;" id="mydiv" runat="server">
   
     <div style="margin-left:75px;margin-top:12px;font-size:15px;"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
   <div style="margin-left:75px;margin-top:12px;font-size:15px;">  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
  <div style="margin-left:75px;margin-top:12px;font-size:12px;"><asp:Label ID="Label3" runat="server" Text="Label"></asp:Label></div>
    
</div>

//.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using System.ComponentModel;
namespace one.scripts.teacher
{
    public partial class student : System.Web.UI.UserControl,IPostBackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [DefaultValue(typeof(string), ""), Browsable(true)
     , Description("Control Lable Of Text attribute"), Category("Action")]
        public string LableText1
        {
            get
            {
                return Label1.Text;
            }
            set
            {
                this.Label1.Text = value;
            }
        }
        [DefaultValue(typeof(string), ""), Browsable(true)
        , Description("Control Lable2 Of Text attribute"), Category("Action")]
        public string LableText2
        {
            get
            {
                return Label2.Text;
            }
            set
            {
                this.Label2.Text = value;
            }
        }
        [DefaultValue(typeof(string), ""), Browsable(true)
    , Description("Control Lable3 Of Text attribute"), Category("Action")]
       
        public string LableText3
        {
            get
            {
                return Label3.Text;
            }
            set
            {
                this.Label3.Text = value;
            }
        }

//This paragraph is to write the text attribute to three lable s
//

  public event EventHandler<EventArgs> Click;
        public void  RaisePostBackEvent(string eventArgument)
        {
            if (this.Click != null)
                this.Click(this, EventArgs.Empty);
        }
       protected void Page_PreRender(object sender,EventArgs e)
        {
            var ct1 = this.mydiv;
            var script = this.Page.ClientScript.GetPostBackEventReference(this, string.Empty);
             ((HtmlContainerControl)ct1).Attributes["onclick"] = script;
          
        }
    //This section is to get the ID of the div, and then register the div with the click script

//
I use PlaceHolder as container in aspx

 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Add ascx user control to PlaceHolder dynamically
I wrote a for loop here, and I won't let it out

for(int i=1;i<10;i++){
  student f=(student)Page.LoadControl("~/scripts/teacher/student.ascx");
    PlaceHolder1.Controls.Add(f);
 f.Click += g_Click;     //And then add a click event to it
}

student is my ascx user control
By the way, register the ascx user control on the aspx page like this
Just put it at the top of the aspx page

<%@ Register Src="~/scripts/teacher/student.ascx" TagName="student1" TagPrefix="student1" %>

//
//
G'click wrote it by himself
Mine is like this

 private void g_Click(object sender, EventArgs e)
        {
          Response.Write("<script>alert('Click Finish')</script>");
}

At the end of the article, I will put a link to Daniel's answer
[https://bbs.csdn.net/topics/330152344]

Published 2 original articles, won 0 praise and 256 visitors
Private letter follow

Tags: Attribute

Posted on Sat, 11 Jan 2020 07:56:42 -0800 by dayang