用户控件的加载应用

aspx:
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< hr  />
           
< asp:PlaceHolder  ID  = "Pholder"  runat  ="server" ></ asp:PlaceHolder >
    
</ div >
    
</ form >
</ body >
</ html >
cs:
     protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            LoadControls();
        }

    }


    
protected   void  LoadControls()
    
{
        Control con1 
= new Control();
        Control con2 
= new Control();

        con1 
= this.LoadControl("WebUserControl.ascx");
        con2 
= this.LoadControl("WebUserControl2.ascx");
        Pholder.Controls.Add(con1);
        Pholder.Controls.Add(con2);
    }


注意点:用户控件在加载后或者多次加载后,部分JS文件调用内部控件ID的时候可能会产生变化
解决办法:
1、在cs中定义一个全局变量 如: public string strConName;
2、在加载控件(Pholder.Controls.Add(con1);)之后加     如:strConName = con1.ClientID;   // 获取加载控件的控件代号
3、在aspx页面中增加   <script type="text/javascript">  var strName = '<%=strConName%>';   </script> // 在JS中获取
4、页面预览之后,页面上的控件ID 变为  strName+"_tbxLoadPortEN" (_+未加载前控件ID)      document.getElementById(strName+"_tbxLoadPortEN");

   foreach (GridViewRow row in gvList.Rows)
            {
                CheckBox delItem = row.FindControl("cbxItem") as CheckBox;

                if (delItem != null && delItem.Checked)
                {
     bll.Delete(Convert.ToInt32(gvList.DataKeys[row.RowIndex].Values[0]),Convert.ToInt32(gvList.DataKeys[row.RowIndex].Values[1]));
                }
            }
Panel pl = this.FindControl(panelName) as Panel;
        TextBox tbxPersonName = GvPerson.Rows[e.RowIndex].Cells[1].FindControl("tbxPersonName") as TextBox;
        DropDownList ddSex = GvPerson.Rows[e.RowIndex].Cells[2].FindControl("ddSex") as DropDownList;
(TextBox)Findcontrol("").Text

多次加载问题
cs:

Control Con  =   new  Control(); 
  
private   void  Page_Load( object  sender, System.EventArgs e)
  
{
   
// 在這裡放置使用者程式碼以初始化網頁
   if(!this.IsPostBack)
   
{
    
for(int i=0;i<5;i++)
    
{
    
// Con = new wc(); 
     Con = (wc)this.LoadControl("New/wc.ascx"); 
     ((wc)Con).ID 
= i.ToString();
     ((wc)Con).from_pub_id 
= i.ToString(); 
     PlaceHolder1.Controls.Add(Con);
    }

   }

  }

*.ascx
放置一个Label
*.ascx.cs:

     /**/ /// <summary>
    
///        wc 的摘要描述。
    
/// </summary>

     public   class  wc : System.Web.UI.UserControl
    
{
        
protected System.Web.UI.WebControls.Label Label1;

        
public string str_from_pub_id; 

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在這裡放置使用者程式碼以初始化網頁
            Label1.Text = from_pub_id;
        }


        
Web Form 設計工具產生的程式碼#region Web Form 設計工具產生的程式碼
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼叫。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/**//// <summary>
        
///        此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
        
///        這個方法的內容。
        
/// </summary>

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

        }

        
#endregion



        
public string from_pub_id 
        

            
get return str_from_pub_id; } 
            
set { str_from_pub_id = value; } 
        }
 
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值