2012-07-11 57 views
2

我正在处理用户表单,我希望根据用户需要(在按钮单击事件上)动态生成尽可能多的辅助手机字段。我已经按照此页面上Daniel的动态控制实例:How can I re-instantiate dynamic ASP.NET user controls without using a database?动态ascx用户控件的空引用异常

我也实现了这个网页上提出lordscarlet“事件冒泡”的解决方案:Eventhandling in ascx usercontrols

我user.aspx页面的相关部分:

<asp:Panel ID="phonePnl" runat="server"></asp:Panel> 
      <tr><td><asp:Button ID="phoneBtn" Text="Add Secondary Phone" OnClick="phoneBtn_Click" runat="server" /></td></tr> 

user.aspx.cs的相关部分代码隐藏

// ============================================================================ 
protected void phoneBtn_Click(object sender, EventArgs e) 
{ 
    UserControl controlToAdd = new UserControl(); 
    controlToAdd = (UserControl)controlToAdd.LoadControl("~/DynamicData/FieldTemplates/SecondaryPhone.ascx"); 

    controlToAdd.ID = Guid.NewGuid().ToString(); 

    Panel phonePnl = (Panel)fvUser.FindControl("phonePnl"); 

    Literal lit = new Literal(); 
    lit.Text = "<tr><td>Secondary Phone</td><td>"; 
    phonePnl.Controls.Add(lit); 
    phonePnl.Controls.Add(controlToAdd); 
    lit = new Literal(); 
    lit.Text = "</td></tr>"; 
    phonePnl.Controls.Add(lit); 

    myControlList.Add(controlToAdd.ID, controlToAdd.AppRelativeVirtualPath); 
    Session["myControlList"] = myControlList; 
} 

// ============================================================================ 
protected override void OnInit(EventArgs e) 
{ 
    InitializeComponent(); 
    if (!IsPostBack) 
    { 
     myControlList = new Dictionary<string, string>(); 
     Session["myControlList"] = myControlList; 
    } 
    else 
    { 
     myControlList = (Dictionary<string, string>)Session["myControlList"]; 

     foreach (var registeredControlId in myControlList.Keys) 
     { 
      UserControl controlToAdd = new UserControl(); 
      controlToAdd = (UserControl)controlToAdd.LoadControl(myControlList[registeredControlId]); 
      Panel phonePnl = new Panel(); 
      phonePnl = (Panel)fvUser.FindControl("phonePnl"); 
      phonePnl.Controls.Add(controlToAdd); 
     } 
    } 
    base.OnInit(e); 
} 

// ============================================================================ 
private void InitializeComponent() 
{ 
    this.Load += new System.EventHandler(this.Page_Load); 
    SecondaryPhoneControl.BubbleClick += new EventHandler(admin_user_BubbleClick); 
} 

// ============================================================================ 
private void admin_user_BubbleClick(object sender, EventArgs e) 
{ 
    //todo 
} 

请注意,我添加了最后一行:
Session [“myControlList”] = myControlList;

另外,fvUser只是我在user.aspx页面中的FormView ID。

虽然这不是Daniel的解决方案,但我的想法是,对于您添加的每个控件,都需要将其添加到您的Dictionary中。我完全误导了吗?

我的用户控件是带有TextBox和Button的ascx(用于删除控件,如果用户决定不需要该字段)。请注意,此删除操作尚未实现,但在解决此问题后,该事件在我的父代码中。

SecondaryPhone.ascx

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

SecondaryPhone.ascx.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
using System.Web.DynamicData; 

namespace TwentyFourSeven.DynamicData.FieldTemplates 

{ 
public partial class SecondaryPhone : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    public void SetPhone(String phone) 
    { 
     secPhoneTxt.Text = phone; 
    } 

    protected override void LoadViewState(object savedState) 
    { 
     base.LoadViewState(savedState); 
     secPhoneTxt.Text = (String)ViewState["secPhoneTxt"]; 
    } 

    protected override object SaveViewState() 
    { 
     ViewState["secPhoneTxt"] = secPhoneTxt.Text; 
     return base.SaveViewState(); 
    } 

    private void secPhoneBtn_Click(object sender, EventArgs e) 
    { 
    } 

    public event EventHandler BubbleClick; 

    protected void OnBubbleClick(EventArgs e) 
    { 
     if (BubbleClick != null) 
     { 
      BubbleClick(this, e); 
     } 
    } 

    protected override void OnInit(EventArgs e) 
    { 
     InitializeComponent(); 
     base.OnInit(e); 
    } 

    private void InitializeComponent() 
    { 
     this.secPhoneBtn.Click += new System.EventHandler(this.secPhoneBtn_Click); 
     this.Load += new System.EventHandler(this.Page_Load); 
    } 
} 
} 

调试时,第一次加载页面时OnInit的正常运行,并且点击添加辅助电话按钮在fi上工作首先点击。第二次点击抛出一个空引用异常(对象引用不设置为一个对象的一个​​实例)在此行我的OnInit方法的:

phonePnl.Controls.Add(controlToAdd); 

在调试我注意到,在按钮的第一次点击,在我phoneBtn_Click方法,controlToAdd.Application,controlToAdd.Cache等也会抛出空引用异常,但它仍然生成控件并返回页面。它还将控件及其ID添加到词典中。

然后在按钮的第二次单击之前,就在它遇到导致异常的行之前,我的phonePnl变量为空。我可以推测这是导致异常的原因,但为什么我的phonePnl变量在这种情况下为空,而不是在第二次OnInit调用/第一次回发中?

我一直对这一切感慨万分,我希望有人能提供一些见解。

回答

0

,而不是controlToAdd.LoadControl使用Page.LoadControl,看看是否能工程

+0

同样发生异常 - 有引用页LoadControl,而不是特定的控制的LoadControl有区别吗? – AWollan 2012-07-11 17:02:45