c#_DropdownList Panel Textbox 控件交互使用,有autopostback和没有的区别

时间:2023-03-09 01:09:53
c#_DropdownList Panel Textbox 控件交互使用,有autopostback和没有的区别
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PanelDemo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Panel Controls</h1>
<h2>Dynamically Generated Controls</h2>
<asp:Panel ID="pnlDynamic" runat="server" Height="150" Width="80%"
BackColor="Beige" Font-Names="Courier New"
HorizontalAlign="Center" Style="padding:20px" ScrollBars="Auto">
<br />This is panel setting
<p/> <p/>
</asp:Panel>
<table>
<tr>
<td>
Number of Labels:
</td>
<td>
<asp:DropDownList ID="ddlLabels" runat="server">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Number of TextBoxs:
</td>
<td>
<asp:DropDownList ID="ddlBoxes" runat="server">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
 
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="chkHide" runat="server" Text="Hide Panel"/>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Refresh Panel" />
</td>
</tr>
</table>
<hr />
<h2>ScrollBars and Wrapping</h2>
<asp:Panel ID="pnlScroll" runat="server" Height="200px"
Width="90%" GroupingText="ScrollBars & Wrap">
<asp:Label ID="lbPanelContent" runat="server" Text="Label"></asp:Label>
</asp:Panel>
<br />
<table>
<tr>
<td align="right">
ScrollBars:
</td>
<td>
<asp:DropDownList ID="ddlScrollBars" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="ddlScrollBars_SelectedIndexChanged">
<asp:ListItem Text="None" Selected="True"></asp:ListItem>
<asp:ListItem Text="Auto" ></asp:ListItem>
<asp:ListItem Text="Both" ></asp:ListItem>
<asp:ListItem Text="Horizontal" ></asp:ListItem>
<asp:ListItem Text="Vertical"></asp:ListItem>
</asp:DropDownList>
</td>
<td align="right" with="75">
Wrap:
</td>
<asp:RadioButtonList ID="rblWrap" runat="server"
AutoPostBack="true"
RepeatDirection="Horizontal" OnSelectedIndexChanged="rblWrap_SelectedIndexChanged">
<asp:ListItem Text="True" Value="true" Selected="True"></asp:ListItem>
<asp:ListItem Text="False" Value="false"></asp:ListItem>
</asp:RadioButtonList>
</tr> </table> </div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace PanelDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (chkHide.Checked)
{
pnlDynamic.Visible = false;
}
else
{
pnlDynamic.Visible = true;
} int numlabels = Int32.Parse(ddlLabels.SelectedItem.Value); for (int i = 1; i < numlabels; i++)
{
Label lb1 = new Label();
lb1.Text = "Label " + (i).ToString();
pnlDynamic.Controls.Add(lb1);
pnlDynamic.Controls.Add(new LiteralControl("<br/>"));
} int numBoxes = Int32.Parse(ddlBoxes.SelectedItem.Value);
for (int i = 1; i < numBoxes; i++)
{
TextBox txt = new TextBox();
txt.Text = "TextBox " + i.ToString();
txt.ID = "TextBox " + i.ToString();
pnlDynamic.Controls.Add(txt);
pnlDynamic.Controls.Add(new LiteralControl("<br/>")); }
string strText = "填写长内容";
lbPanelContent.Text = strText;
} protected void ddlScrollBars_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string strValue = ddl.SelectedValue; ScrollBars scrollBar = (ScrollBars)Enum.Parse(typeof(ScrollBars), strValue);
pnlScroll.ScrollBars = scrollBar;
} protected void rblWrap_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList rbl = (RadioButtonList)sender;
pnlScroll.Wrap = Convert.ToBoolean(rbl.SelectedValue);
}
}
}