Panel与PlaceHoder容器的使用

本文详细介绍了ASP.NET中Panel与PlaceHolder控件的使用方法,包括如何通过Panel的Visible属性实现页面元素的隐藏与显示,以及如何利用PlaceHolder控件动态加载其他控件,为页面布局和动态内容呈现提供了实用的技巧。
Panel的使用:

     Panel,在我的使用中,常常是应用它的visible属性,用来对页面进行排版,达到隐藏和显示的效果,当然我只是个小菜。但是容器其实是个很好的东西,最大的亮点就是可以往容器中放入各种各样的控件。这次就来演示一下往里面放入各种控件的方法。

一定要注意,Panel刚放进来的时候,是有宽和高的,根据情况去修改他。

写给自己:为了能让添加文字的那一项中可以输入HTML代码,需要在Page节中加入validateRequest=false 来禁止验证


PlaceHoder的用法:

       PlaceHoder用法跟Panel差不多,但不会跟Panel一样生成DIV这样的HTML代码。这个控件可以用作文档内的一个容器控件以便动态的加载其他控件。

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//添加事件
InBlock.gif        
//int a = int.Parse(TextBox1.Text); //将文本框里的值转化后传给整形a
InBlock.gif        
//int a =Convert.ToInt32(Math.Round(Convert.ToDecimal(TextBox1.Text))); //这里用到了一个Math类里的Round属性,将值舍入到最接近的整数或指定的小数位数。
InBlock.gif        
//int a = Convert.ToInt32(Math.Truncate(Convert.ToDecimal(TextBox1.Text)));//这里用到了一个Math类里的Truncate 属性,计算一个数字的整数部分。
InBlock.gif

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//////////////////////////////////////////////////////
InBlock.gif
InBlock.gif
InBlock.gif        
int a = int.Parse(DropDownList2.SelectedValue);//将要添加的数量给一个整形以便循环添加
InBlock.gif
        for (int i = 1; i <= a; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label lab 
= new Label();
InBlock.gif            lab.ID 
= "lab" + i;
InBlock.gif            lab.Text 
= "我是第" + i + "个Laber<br>"//Laber里是可以放HTML代码的
InBlock.gif
            Panel1.Controls.Add(lab);//添加laber到panel中
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//////////////////////////////////////////////////////
InBlock.gif
InBlock.gif
InBlock.gif        
int k = int.Parse(DropDownList1.SelectedValue); //将要添加的数量给一个整形以便循环添加       
InBlock.gif
        for (int j = 1; j <= k; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TextBox tb 
= new TextBox();
InBlock.gif            tb.ID 
= "TextBox" + j;
InBlock.gif            tb.Text 
= "我是第" + j + "个Textbox";//TextBox里是不能放html代码的
InBlock.gif

InBlock.gif            Literal lt 
= new Literal();//需要用Literal来放入换行代码
InBlock.gif
            lt.Text = "<br>";
InBlock.gif
InBlock.gif            Panel1.Controls.Add(tb);
//添加textbox到panel中
InBlock.gif
            Panel1.Controls.Add(lt);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/////////////////////////////////////////////////////
InBlock.gif
InBlock.gif        Literal txt 
= new Literal();
InBlock.gif        txt.Text 
= TextBox2.Text;
InBlock.gif        Panel1.Controls.Add(txt);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//Panel的隐藏与显示
InBlock.gif
        if (!CheckBox1.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Panel1.Visible 
= true;
InBlock.gif            CheckBox1.Text 
= "隐藏panel";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Panel1.Visible 
= false;
InBlock.gif            CheckBox1.Text 
= "显示panel";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button2_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//添加控件
InBlock.gif

InBlock.gif        HtmlButton hbtn 
= new HtmlButton();//添加一个HTML控件
InBlock.gif
        hbtn.InnerText = "HTML按钮控件";
InBlock.gif        PlaceHolder1.Controls.Add(hbtn);
InBlock.gif
InBlock.gif        Literal lt 
= new Literal();//添加一个换行
InBlock.gif
        lt.Text = "<br>";
InBlock.gif        PlaceHolder1.Controls.Add(lt);
InBlock.gif
InBlock.gif        Button btn 
= new Button();//添加一个服务器控件
InBlock.gif
        btn.ID = "btn1";
InBlock.gif        btn.Text 
= "服务器控件";
InBlock.gif        PlaceHolder1.Controls.Add(btn);
InBlock.gif
InBlock.gif        Literal html 
= new Literal();//添加普通HTML代码
InBlock.gif
        html.Text = "<br><h1><b><i>我是HTML代码</i></b></h1>";
InBlock.gif        PlaceHolder1.Controls.Add(html);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


点此下载代码

转载于:https://www.cnblogs.com/mgod/archive/2007/03/31/695231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值