[研究][C#][ASP.NET][WebForm] 依下拉選單數量,動態生成 TextBox 輸入欄位
2020-11-16
Default.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!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>
<script>
function createInputTexts(Xselect) {
if (Xselect.selectedIndex > 0) {
var p1 = document.getElementById("p1");
p1.innerHTML = "";
var count = parseInt(Xselect.options[Xselect.selectedIndex].value);
for (var i = 0; i < count; i++)
p1.innerHTML = p1.innerHTML + "<input type='text' name='DynaTextBox" + i + "' /><br/>";
p1.innerHTML = p1.innerHTML + "<input type='hidden' name='textCount' value='" + count + "' />";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" onchange="createInputTexts(this)">
<asp:ListItem>請選擇…</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<p id="p1"></p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request["textCount"] != null)
{
int textCount = int.Parse(Request["textCount"]);
for (int i = 0; i < textCount; i++)
Response.Write(Request["DynaTextBox" + i.ToString()] + "<br/>");
}
}
}
} |
(完)
相關
Google "ASP.NET 動態 textBox 值取得"
動態產生的TextBox,如何抓取填入的值?
註:textBox會連在一起,this.Panel1.Controls.Add("<br />"); 不行,因為不是 Control,另外 FindControl 抓值麻煩。
沒有留言:
張貼留言