PlaceHolder控件例子
PlaceHolder控件
lCode
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//文本框
HtmlInputText text = new HtmlInputText();
text.Value = "textbox";
text.MaxLength = 20;
text.Size = 22;
Place.Controls.Clear();
Place.Controls.Add(text);
//按钮
HtmlInputButton button = new HtmlInputButton();
button.Value = "Click Me";
button.Attributes.Add("onclick", "alert('Hello from the client side.')");
Place1.Controls.Clear();
Place1.Controls.Add(button);
//按钮,按钮,文本框,
HtmlInputReset reset1 = new HtmlInputReset();
reset1.ID = "ResetButton1";
reset1.Value = "Reset 1";
HtmlInputReset reset2 = new HtmlInputReset("reset");
reset2.ID = "ResetButton2";
reset2.Value = "Reset 2";
HtmlInputReset reset3 = new HtmlInputReset("custom");
reset3.ID = "ResetButton3";
reset3.Value = "Reset 3";
Place2.Controls.Clear();
Place2.Controls.Add(reset1);
Place2.Controls.Add(new LiteralControl("<br>"));
Place2.Controls.Add(reset2);
Place2.Controls.Add(new LiteralControl("<br>"));
Place2.Controls.Add(reset3);
//单选项
if (!IsPostBack)
{
HtmlInputRadioButton radio1 = new HtmlInputRadioButton();
radio1.Value = "Value1";
radio1.Checked = true;
radio1.Name = "RadioSet";
HtmlGenericControl span1 = new HtmlGenericControl("span");
span1.InnerHtml = "Radio Button 1 <br>";
Place3.Controls.Clear();
Place3.Controls.Add(radio1);
Place3.Controls.Add(span1);
// Create the second HtmlInputRadioButton control.
HtmlInputRadioButton radio2 = new HtmlInputRadioButton();
radio2.Value = "Value2";
radio2.Checked = false;
radio2.Name = "RadioSet";
// Create the caption for the second HtmlInputRadioButton control.
HtmlGenericControl span2 = new HtmlGenericControl("span");
span2.InnerHtml = "Radio Button 2 <br>";
// Add the control to the Controls collection of the
// PlaceHolder control.
Place3.Controls.Add(radio2);
Place3.Controls.Add(span2);
}
//隐藏控件
HtmlInputHidden hidden = new HtmlInputHidden();
hidden.ID = "HiddenValue";
hidden.Value = "Hidden Text";
Place4.Controls.Add(hidden);
// Display the value of the HtmlInputHidden control.
Message.InnerHtml = "This page contains an HtmlInputHidden control that contains " +
"the value \"" + ((HtmlInputHidden)Place.FindControl("HiddenValue")).Value + "\"";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
PlaceHolder控件的功能与Panel控件的功能相似,动态地添加新的控件时用PlaceHolder控件。
<h4>文本框 </h4>
<asp:PlaceHolder id="Place" runat="server"/>
<h4> 按钮 </h4>
<asp:PlaceHolder id="Place1" runat="server"/>
<h4> HtmlInputReset Example </h4>
<asp:PlaceHolder id="Place2" runat="server"></asp:PlaceHolder>
<h3> 单选框 </h3>
<asp:PlaceHolder id="Place3" runat="server"/>
<h3> HtmlInputHidden Constructor Example </h3>
<asp:PlaceHolder id="Place4" runat="server"/>
<h5>
<span id="Message" runat="server"></span>
</h5>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//文本框
HtmlInputText text = new HtmlInputText();
text.Value = "textbox";
text.MaxLength = 20;
text.Size = 22;
Place.Controls.Clear();
Place.Controls.Add(text);
//按钮
HtmlInputButton button = new HtmlInputButton();
button.Value = "Click Me";
button.Attributes.Add("onclick", "alert('Hello from the client side.')");
Place1.Controls.Clear();
Place1.Controls.Add(button);
//按钮,按钮,文本框,
HtmlInputReset reset1 = new HtmlInputReset();
reset1.ID = "ResetButton1";
reset1.Value = "Reset 1";
HtmlInputReset reset2 = new HtmlInputReset("reset");
reset2.ID = "ResetButton2";
reset2.Value = "Reset 2";
HtmlInputReset reset3 = new HtmlInputReset("custom");
reset3.ID = "ResetButton3";
reset3.Value = "Reset 3";
Place2.Controls.Clear();
Place2.Controls.Add(reset1);
Place2.Controls.Add(new LiteralControl("<br>"));
Place2.Controls.Add(reset2);
Place2.Controls.Add(new LiteralControl("<br>"));
Place2.Controls.Add(reset3);
//单选项
if (!IsPostBack)
{
HtmlInputRadioButton radio1 = new HtmlInputRadioButton();
radio1.Value = "Value1";
radio1.Checked = true;
radio1.Name = "RadioSet";
HtmlGenericControl span1 = new HtmlGenericControl("span");
span1.InnerHtml = "Radio Button 1 <br>";
Place3.Controls.Clear();
Place3.Controls.Add(radio1);
Place3.Controls.Add(span1);
// Create the second HtmlInputRadioButton control.
HtmlInputRadioButton radio2 = new HtmlInputRadioButton();
radio2.Value = "Value2";
radio2.Checked = false;
radio2.Name = "RadioSet";
// Create the caption for the second HtmlInputRadioButton control.
HtmlGenericControl span2 = new HtmlGenericControl("span");
span2.InnerHtml = "Radio Button 2 <br>";
// Add the control to the Controls collection of the
// PlaceHolder control.
Place3.Controls.Add(radio2);
Place3.Controls.Add(span2);
}
//隐藏控件
HtmlInputHidden hidden = new HtmlInputHidden();
hidden.ID = "HiddenValue";
hidden.Value = "Hidden Text";
Place4.Controls.Add(hidden);
// Display the value of the HtmlInputHidden control.
Message.InnerHtml = "This page contains an HtmlInputHidden control that contains " +
"the value \"" + ((HtmlInputHidden)Place.FindControl("HiddenValue")).Value + "\"";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
PlaceHolder控件的功能与Panel控件的功能相似,动态地添加新的控件时用PlaceHolder控件。
<h4>文本框 </h4>
<asp:PlaceHolder id="Place" runat="server"/>
<h4> 按钮 </h4>
<asp:PlaceHolder id="Place1" runat="server"/>
<h4> HtmlInputReset Example </h4>
<asp:PlaceHolder id="Place2" runat="server"></asp:PlaceHolder>
<h3> 单选框 </h3>
<asp:PlaceHolder id="Place3" runat="server"/>
<h3> HtmlInputHidden Constructor Example </h3>
<asp:PlaceHolder id="Place4" runat="server"/>
<h5>
<span id="Message" runat="server"></span>
</h5>
</div>
</form>
</body>
</html>
例2:
Code
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script language="C#" runat="server">
HtmlInputCheckBox cb = new HtmlInputCheckBox();
Label label = new Label();
HtmlButton bb = new HtmlButton();
protected void Page_Load(object sender, EventArgs e)
{
bb.InnerText = "Click";
label.Text = "checkbox";
Container.Controls.Add(cb);
Container.Controls.Add(label);
Container.Controls.Add(new LiteralControl("<br>"));
Container.Controls.Add(bb);
bb.ServerClick += new EventHandler(button_ServerClick);
}
void button_ServerClick(object sender, EventArgs e)
{
switch (cb.Checked)
{
case true:
Message1.InnerHtml = "Checkbox is checked.";
break;
default:
Message1.InnerHtml = "Checkbox is not checked.";
break;
}
}
</script>
</head>
<body>
<h3>HtmlInputCheckBox Constructor Sample</h3>
<form id="Form1" runat="server">
<asp:PlaceHolder id="Container" runat="server"></asp:PlaceHolder>
<br />
<span id="Message1" style="color:red" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script language="C#" runat="server">
HtmlInputCheckBox cb = new HtmlInputCheckBox();
Label label = new Label();
HtmlButton bb = new HtmlButton();
protected void Page_Load(object sender, EventArgs e)
{
bb.InnerText = "Click";
label.Text = "checkbox";
Container.Controls.Add(cb);
Container.Controls.Add(label);
Container.Controls.Add(new LiteralControl("<br>"));
Container.Controls.Add(bb);
bb.ServerClick += new EventHandler(button_ServerClick);
}
void button_ServerClick(object sender, EventArgs e)
{
switch (cb.Checked)
{
case true:
Message1.InnerHtml = "Checkbox is checked.";
break;
default:
Message1.InnerHtml = "Checkbox is not checked.";
break;
}
}
</script>
</head>
<body>
<h3>HtmlInputCheckBox Constructor Sample</h3>
<form id="Form1" runat="server">
<asp:PlaceHolder id="Container" runat="server"></asp:PlaceHolder>
<br />
<span id="Message1" style="color:red" runat="server"/>
</form>
</body>
</html>
例3:
Code
<%@ Page Language="C#" AutoEventWireup="True" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
HtmlInputFile file = (HtmlInputFile)Place.FindControl("File1");
if (Text1.Value == "")
{
Span1.InnerHtml = "Error: You must enter a file name.";
return;
}
if (file.PostedFile.ContentLength > 0)
{
try
{
file.PostedFile.SaveAs("c:\\123\\" + Text1.Value);
Span1.InnerHtml = "File uploaded successfully to " +"<b>c:\\123\\" + Text1.Value + "</b> on the Web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\123\\" +Text1.Value + "</b><br>" + exc.ToString() + ".";
}
}
else
{
Span1.InnerHtml = "Error: 上传的文件为空文件";
return;
}
}
void Page_Load(object sender, EventArgs e)
{
HtmlInputFile file = new HtmlInputFile();
file.ID = "File1";
Place.Controls.Clear();
Place.Controls.Add(file);
}
</script>
<html>
<head>
<title>HtmlInputFile Constructor Example</title>
</head>
<body>
<h3>HtmlInputFile Constructor Example</h3>
<form id="Form1" enctype="multipart/form-data" runat="server">
Specify the file to upload:
<asp:PlaceHolder id="Place" runat="server"/>
<p>
Save as file name (no path):
<input id="Text1" type="text" runat="server">
(自己起个名字)</p>
<p>
<span id=Span1 style="font: 8pt verdana;" runat="server" />
</p>
<p>
<input type=button id="Button1" value="Upload" onserverclick="Button1_Click" runat="server">
(上传到了c:\123)</p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
HtmlInputFile file = (HtmlInputFile)Place.FindControl("File1");
if (Text1.Value == "")
{
Span1.InnerHtml = "Error: You must enter a file name.";
return;
}
if (file.PostedFile.ContentLength > 0)
{
try
{
file.PostedFile.SaveAs("c:\\123\\" + Text1.Value);
Span1.InnerHtml = "File uploaded successfully to " +"<b>c:\\123\\" + Text1.Value + "</b> on the Web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\123\\" +Text1.Value + "</b><br>" + exc.ToString() + ".";
}
}
else
{
Span1.InnerHtml = "Error: 上传的文件为空文件";
return;
}
}
void Page_Load(object sender, EventArgs e)
{
HtmlInputFile file = new HtmlInputFile();
file.ID = "File1";
Place.Controls.Clear();
Place.Controls.Add(file);
}
</script>
<html>
<head>
<title>HtmlInputFile Constructor Example</title>
</head>
<body>
<h3>HtmlInputFile Constructor Example</h3>
<form id="Form1" enctype="multipart/form-data" runat="server">
Specify the file to upload:
<asp:PlaceHolder id="Place" runat="server"/>
<p>
Save as file name (no path):
<input id="Text1" type="text" runat="server">
(自己起个名字)</p>
<p>
<span id=Span1 style="font: 8pt verdana;" runat="server" />
</p>
<p>
<input type=button id="Button1" value="Upload" onserverclick="Button1_Click" runat="server">
(上传到了c:\123)</p>
</form>
</body>
</html>