Panel & PlaceHolder控件的使用

Panel控件的属性:Visible,BackImageUrl,HorizontalAlign,Wrap

PlaceHolder控件在定义时不能向其中添加子控件,添加工作必须在程序中完成。这正是PlaceHolder控件存在的意义,可以根据程序的执行情况,动态的添加需要的控件,而Panel控件则不具体动态添加的功能。

PanelandPlaceHolder.aspx代码:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”PanelandPlaceHolder.aspx.cs” Inherits=”PanelandPlaceHolder” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>无标题页</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Panel ID=”Panel1″ Wrap=true HorizontalAlign=Right runat=server>
<asp:Label Text=”This is first” runat=server/>
</asp:Panel>
<asp:Panel ID=”Panel2″ Wrap=true HorizontalAlign=Center runat=server>
<asp:Label Text=”This is second” runat=server/>
</asp:Panel>
<asp:Panel ID=”Panel3″ Wrap=true HorizontalAlign=Left runat=server>
<asp:Label Text=”This is third” runat=server/>
</asp:Panel>
<hr>
<asp:PlaceHolder ID=”Holder” runat=server/>
<hr>
Please choose the panel that you want:<br>
<asp:DropDownList ID=”DropDown” OnSelectedIndexChanged=”Selected” AutoPostBack=true runat=server>
<asp:ListItem>Panel1</asp:ListItem>
<asp:ListItem>Panel2</asp:ListItem>
<asp:ListItem>Panel3</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>

PanelandPlaceHolder.aspx.cs代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class PanelandPlaceHolder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Panel1.Visible = false;
Panel2.Visible = false;
Panel3.Visible = false;
}
}
public void Selected(object sender, EventArgs e)
{
Panel p = (Panel)FindControl(DropDown.SelectedItem.Text);
p.Visible = true;
p.ForeColor =Color.Red;

string selindex = DropDown.SelectedIndex.ToString();
Label lbl = new Label();

switch (selindex)
{
case “0”:
lbl.Text=”You selected Panel1″;
break;
case “1”:
lbl.Text=”You selected Panel2″;
break;
case “2”:
lbl.Text=”You selected Panel3″;
break;
default:
lbl.Text=”You haven’t selected anyone”;
break;

}

Holder.Controls.Add(lbl);

}
}

发表评论

邮箱地址不会被公开。 必填项已用*标注