一个控件(在同一页面)从另一控件中得到属性值

时间:2022-09-13 20:30:02

 

一个控件(在同一页面)从另一控件中得到属性值


public bool CheckUserCtrl()
  {
   try
   {
    System.Web.UI.Page p = this.Parent.Page;
    if(this.ControlID.Length == 0)
    {
     //控件是否存在
     return false;
    }

    System.Web.UI.UserControl uctrl = (System.Web.UI.UserControl)p.FindControl(ControlID); //通过给的属性(控件ID)得到整个控件
    if(uctrl == null)
    {    
     return false;
    }

    Type objType = uctrl.GetType();

    #region 判断 RecordID是否存在
    PropertyInfo idInfo = objType.GetProperty("RecordID");//到页面所调另一控件中得到属性值
    if(idInfo == null)
    {
     //RecordID(idInfo)属性是否存在
     return false;
    }
    object oRecordID = idInfo.GetValue(uctrl,null);   
    if(oRecordID == null || Convert.ToInt32(oRecordID) == 0)
    {
     //判断oRecordID是否为null
     return false;
    }
    this.RecordID = Convert.ToInt32(oRecordID);
    #endregion

    #region 判断RecordName是否存在
    PropertyInfo explainInfo = objType.GetProperty("RecordName");
    if(explainInfo == null)
    {
     //判断RecordName是否存在
     return false;
    }
    object oExplain = explainInfo.GetValue(uctrl,null);
    if(oExplain == null || oExplain.ToString() == string.Empty)
    {
     //判断oExplain(explainInfo)是否为null
     return false;
    }
    this.Explain = oExplain.ToString();
    #endregion

    #region 判断ModelName是否存在
    PropertyInfo modelInfo = objType.GetProperty("ModelName");//到页面所调另一控件中得到属性值
    if(modelInfo == null)
    {
     //判断ModelName是否存在
     return false;
    }
    object oModelName = modelInfo.GetValue(uctrl,null);
    if(oModelName == null || oModelName.ToString() == string.Empty)
    {
     //判断oModelName(ModelName)是否为null
     return false;
    }
    this.ModelName = oModelName.ToString();
    #endregion

    return true; 
   }
   catch(Exception ex)
   {
    return false;
   }
  }