我正在尝试在面板内显示 5x5 网格的文本框。单击 后Show
,空文本框将为空 ( visible: false;
),非空文本框将显示在适当的位置。
我当前的 .aspx 文件:
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" />
</form>
</body>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
//Declare the array
//int[,] wsArray = new int[5, 5];
//char[] delimiterChars = { ' ', ' ', ' ' };
StringWriter stringWriter = new StringWriter();
// Put HtmlTextWriter in using block because it needs to call Dispose.
using (HtmlTextWriter hw = new HtmlTextWriter(stringWriter))
{
int number = 1;
//hw.Write("<table>");
for (int i = 0; i <= 4; i++)
{
//hw.Write("<tr>");
for (int j = 0; j <= 4; j++)
{
System.Random rnd = new Random();
TextBox tb = new TextBox();
tb.ID = number.ToString();
tb.MaxLength = 1;
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
Panel1.Controls.Add(tb);
number++;
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (Control text in Panel1.Controls)
{
if (text == null)
{
text.Visible = false;
}
else
{
text.Visible = true;
}
}
}