पर उपयोगकर्ता नियंत्रण जोड़ना मेरे पास एक वर्ग (MyClass) है जो एक नेस्टेड पदानुक्रम का प्रतिनिधित्व करता है, इसलिए कक्षा में एक संपत्ति है जो MyClass का संग्रह है। MyClass में शीर्षक शीर्षकगतिशील रूप से दोहराना
इसे किसी वेब पेज पर दिखाने के लिए, मैं एक उपयोगकर्ता नियंत्रण बनाने की उम्मीद कर रहा था जिसमें दोहराना था। आइटम टेम्पलेट में मुझे शीर्षक संपत्ति प्रदर्शित करने के लिए शाब्दिक होगा, और पुनरावर्तक की आइटमक्रेटेड घटना पर, मैं उपयोगकर्ता नियंत्रण का एक नया उदाहरण तैयार करूंगा, और इसे दोहराने वाले वर्तमान आइटम में जोड़ दूंगा।
मेरी समस्या यह है कि जब उपयोगकर्ता नियंत्रण में पेज_लोड ईवेंट चला जाता है, यदि नियंत्रण गतिशील रूप से जोड़ा गया था, तो repMyClass repeater poroperty शून्य है, भले ही मैं EnsureChildControls को कॉल करता हूं। क्या मुझसे कोई चूक हो रही है? यदि मैं दोहराना बना देता हूं और आइटम टेम्पलेट में अपना userctonrol ड्रॉप करता हूं तो यह ठीक काम करता है। मैं इसे गतिशील रूप से काम करने के लिए नहीं मिल सकता?
उपयोगकर्ता नियंत्रण:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="MyControl" %>
Items:<br/>
<asp:Repeater ID="repMyClass" runat="server" EnableViewState="false"
OnItemCreated="repMenuItems_ItemCreated">
<HeaderTemplate><ul><HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %>
<div><asp:PlaceHolder ID="SubItemPlaceholder" runat="server" /></div>
</li></ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
उपयोगकर्ता नियंत्रण कोड:
public partial class MyControl: System.Web.UI.UserControl
{
public IEnumerable<MyClass> ChildItems { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.repMyClass.DataSource = ChildItems;
this.repMyClass.DataBind();
}
protected void repMenuItems_ItemCreated(object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
//Get the MyClass instance for this repeater item
MyClass mcCurrent = (MyClass)e.Item.DataItem;
//If the MyClass instance has child instances
if (mcCurrent.Children != null && mcCurrent.Children.Length > 0)
{
//Add a new user control and set it's property so it can bind
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("SubItemPlaceholder");
MyControl ctl = (MyControl)Page.LoadControl(typeof(MyControl),new object[] {});
ctl.ChildItems = mcCurrent.Children;
ph.Controls.Add(ctl);
}
}
}
}
क्या आप इसे समझाने और इसे साफ़ करने के लिए संपादित कर सकते हैं। मुझे लगता है कि शायद इस वाक्य में आपके पास आधा संपादन हो गया है जो पूरा नहीं हुआ? "मेरी समस्या यह है कि जब पृष्ठ-लोड ईवेंट पहले नियंत्रण के लिए पहले किया गया था, तो repNyClass repeater poroperty शून्य है" – chrismay