簡體   English   中英

將數據傳遞給動態創建的ascx用戶控件

[英]passing data to dynamically created ascx user control

我想顯示用戶控件,該控件根據傳遞給它的值生成不同的數據集。

用戶控件的數量在運行時確定

  • 現在它命中了ascx.cs Page_Load

Default.aspx

<%@ Register Src="~/UserControl/SquareEUA.ascx" TagName="Square" TagPrefix="EUA" %>

<body>
     <div id="divControls" runat="server"></div>
</body>

Default.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
    string[] Property;
    foreach (string sPropID in Property)
    {
SquareEUA userControl = (SquareEUA)Page.LoadControl("~/UserControl/SquareEUA.ascx"); 
        userControl.ID = "MyControl_" + sPropID.Trim();
        userControl.sCustID = CustomerID; 
        userControl.sCustPropID = sPropID.Trim(); 
        userControl.Visible = true;
        divControls.Controls.Add(userControl); 
        userControl.Dispose();
    }
    }

SquareEUA.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SquareEUA.ascx.cs" Inherits="UserControl.SquareEUA" %>
<div>
     // Some HTML Markup
</div>

SquareEUA.ascx.cs

public partial class SquareEUA : System.Web.UI.UserControl
{
public string sCustID { get; set; }
public string sCustPropID { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Some Code    
        // Didnt Hit Breakpoint Here;
    }
}        

您需要將用戶控件強制轉換為實際的SquareEUA類,才能訪問該類的屬性

SquareEUA userControl = (SquareEUA)(Page.LoadControl("~/UserControl/SquareEUA.ascx"));

應該可以解決的問題(您應該添加一些錯誤處理,空檢查等)

編輯:似乎我錯過了(頁...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM