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 設計工具產生的程式碼


        
public string from_pub_id 
        

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

}

 

posted on 2007-04-14 08:30  ipusr  阅读(690)  评论(1编辑  收藏  举报