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

本文介绍了一种方法,用于在一个ASP.NET页面上从一个控件获取另一个控件的属性值,包括RecordID、RecordName和ModelName等属性,并检查这些属性的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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


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;
   }
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值