PlaceHodler动态添加控件

    PlaceHodler动态添加控件在Web窗体上起到占位置的作用,可向动态地添加需要的控件。

   如页面呈现的“确定”按钮和文本框都是在页面载入时动态生成的。

  

asp界面

<div>
       <asp:PlaceHolder ID="plhTest" runat="server" ></asp:PlaceHolder><br />
        <asp:Button ID="btnAcquire_Click" runat="server" Text="获取"
            Onclick="btnAcquire_Clcik" />
    </div>


        protected void Page_Load(object sender, EventArgs e)
        {
            Button btnSubmit = new Button();                                  //定义btnSubmit控件按钮
            btnSubmit.ID = "btnSubmit";                                       //设置btnSubmit控件的ID属性
            btnSubmit.Text = "确定";                                          //设置btn按钮控件的Text属性
            btnSubmit.Click += new EventHandler(btnSubmit_Click);             //注册Clcik时间
            plhTest.Controls.Add(btnSubmit);                                  //将btnSubmit按钮控件添加到plhTest
            TextBox txtInput = new TextBox();                                 //定义txtInput文本框控件
            txtInput.ID = "txtInput";
            plhTest.Controls.Add(txtInput);
 
        }
       
        protected void btnAcquire_Clcik(object sender, EventArgs e)
        {
            Response.Write("出发了”确定“按钮 的Click事件!");
        }
        protected void btnSubmit_Click(object sender,EventArgs e)
        {
            //查找txtInput文本框控件
            TextBox txtInput = (TextBox)plhTest.FindControl("txtInput");
            Response.Write(txtInput.Text);
        }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值