2013-01-16 76 views
3

我在usercontrol中遇到了一些問題。我已經待了好幾個小時,所以現在不得不尋求幫助。將值綁定到usercontrol的問題

如果我把從加價頁面類似這樣的用戶控件:

<uc7:MenuMain2 ID="MenuMain2" runat="server" RootCategoryId="4" ImageHeight="40" 
         ImageWidth="260" /> 

它工作正常。


我也可以把它叫做:

Control cont = LoadControl(@"../usercontrols/MenuMain2.ascx"); 
ContentPlaceHolder1.Controls.Add(cont); 

而且也能正常工作。但是,我不能設置像我需要的屬性。


但是,如果我將它稱爲像下面的代碼中的代碼佔位符,它不起作用。我收到了我在頁面底部發布的錯誤。

Usercontrol代碼文件 - MenuMain.ascx.cs

public partial class MenuMain2 : System.Web.UI.UserControl { 

public int ActiveCategoryId { get; set; } 
public int RootCategoryId { get; set; } 
public int articleID = 0; 
public int ImageWidth ; 
public int ImageHeight ; 

    protected void Page_Load(object sender, EventArgs e) { 
     if (!Page.IsPostBack) 
      BindData(); 
    } 

    private void BindData() { 
     CategoriesService catSvc = new CategoriesService(); 
     if (ActiveCategoryId == 0) { 
      if (articleID > 0) { 
       M02ArticlesService artSvc = new M02ArticlesService(); 
       List<M02ArticlesEntity> artList = artSvc.FindById(articleID); 
       if (artList.Count > 0) 
        ActiveCategoryId = artList.First().CategoryId; 
      } 
     } 
     List<CategoriesEntity> catList = new List<CategoriesEntity>(); 
     catList = catSvc.GetPublishedByParentCategoryId(RootCategoryId); 
     ViewState.Add("ActiveCategoryId", ActiveCategoryId); 
     Repeater1.DataSource = catList; 
     Repeater1.DataBind(); 
    } 
} 

代碼隱藏用於Masterpage

 public HttpCookie authCookie; 
     public FormsAuthenticationTicket ticket; 
     private string name; 
     private MenuMain2 mn; 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      mn = new MenuMain2 {RootCategoryId = 4,ImageHeight = 100, ImageWidth = 260} 
      //Control cont = LoadControl(@"../usercontrols/MenuMain2.ascx"); 

      if (Request.Cookies[".ASPXAUTH"] != null) 
      { 
       authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
       ticket = FormsAuthentication.Decrypt(authCookie.Value); 
       name = ticket.Name; 
      } 

      if (name != null) 
      { 
       if (name.Equals("Jhon")) 
       { 
        mn.RootCategoryId = 1; 
       } 
       if (name.Equals("Bob")) 
       { 
        mn.RootCategoryId = 2; 
       } 
       if (name.Equals("Tom")) 
       { 
        mn.RootCategoryId = 3; 
       } 
      } 

      ContentPlaceHolder1.Controls.Add(mn); 
      CategoriesEntity cat; 
      int catId = Request.GetCategoryId(); 

      if (catId > 0) 
      { 
       cat = new CategoriesService().GetById(catId); 
       if (cat != null && cat.RequireLogin && Request.Url.LocalPath != "/login.aspx") 
       { 
        if (!view_M06PersonsCategoriesService.HasAccess(HttpContext.Current.User.Identity.Name, catId)) 
        { 
         Response.Redirect(string.Format("~/login.aspx?ReturnUrl={0}&qs={1}&CatId={2}{3}", 
                 Server.UrlEncode(Request.Url.LocalPath), 
                 Server.UrlEncode(Request.Url.Query), 
                 Request.QueryString["CatId"], 
                 Request.QueryString["ArtId"].IsEmpty() ? String.Empty : "&ArtId=" + Request.QueryString["ArtId"])); 
        } 
       } 
      } 

      if (!Page.IsPostBack) 
      { 
       litAdmin.Text = ConfigurationManager.AppSettings["WebName"].ToString(); 
       string url = Request.ServerVariables["URL"].ToString(); 
       if (url.ToLower().StartsWith("/default.aspx")) 
        GetPhotoalbum(); 
      } 
     } 
     } 
    } 

錯誤消息 - 在應用中的線42是這樣的一個 - Repeater1.DataSource = catList; -

`Object reference not set to an instance of an object.` 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.] 
    No.Itl.Web.WebApplication.usercontrols.MenuMain2.BindData() in e:\WorkFolder\usercontrols\MenuMain2.ascx.cs:42 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +41 
    System.Web.UI.Control.OnLoad(EventArgs e) +131 
    System.Web.UI.Control.LoadRecursive() +65 
    System.Web.UI.Control.LoadRecursive() +190 
    System.Web.UI.Control.LoadRecursive() +190 
    System.Web.UI.Control.LoadRecursive() +190 
    System.Web.UI.Control.LoadRecursive() +190 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427 
+0

你能找到我們這行'MenuMain2.ascx.cs:42'嗎? – Aristos

+0

是的,它的Repeater1.DataSource = catList; – Darkmage

+0

所以,'GetPublishedByParentCategoryId'返回null ...?或DataBind發生錯誤,請在控件的呈現內找到空點。 – Aristos

回答

1

試試這個:

MenuMain2 mn = (MenuMain2)LoadControl(@"../usercontrols/MenuMain2.ascx"); 

.... 

if (name != null) 
{ 
    if (name.Equals("Jhon")) 
    { 
     mn.RootCategoryId = 1; 
    } 
    if (name.Equals("Bob")) 
    { 
     mn.RootCategoryId = 2; 
    } 
    if (name.Equals("Tom")) 
    { 
     mn.RootCategoryId = 3; 
    } 
} 

正在加載的用戶控件到contmn定義類型的MainMenu2並加入尚未加載的對象。

+0

我已經嘗試過了,那條線應該已經被註釋掉了,我確實參考了我正在嘗試的帖子的頂部。這個工作將會繼續,這將工作'cont.GetType()。GetProperty(「RootCategoryId」).setValue(cont,49,null);' 'cont.GetType()。GetProperty(「ImageWidth」).SetValue(cont,240, null);' 'cont.GetType()。GetProperty(「ImageHeight」).setValue(cont,37,null);' 但是這是乾淨的,唯一的方法呢? – Darkmage

+0

對不起,但我無法關注你已經註釋掉的代碼,仍然存在於你的問題中,因爲未註釋...在UserControl類型上使用反射不是要走的路。基本上,你正在實例化一種'MainMenu2',但不是使用'LoadControl()'加載它,同時將一個控件加載到'cont'中,但是卻沒有做任何事情。那裏似乎有脫節。 – andleer

+0

我現在評論了wasent假設在那裏的那條線,對此抱歉。 – Darkmage