需要从动态添加的文本框中检索数据

时间:2013-11-23 06:45:21

标签: asp.net

我在ASP.Net中从服务器端动态添加了文本框。通过使用以下链接

中显示的一个示例

http://www.dotnettips4u.com/2013/03/dynamically-creating-text-boxes-using-c.html

但我无法找到如何从这些文本框中检索值..

请帮我从这里出来..

我在这里发布我的代码..

客户端代码:

<asp:TextBox ID="NoOfPsngr" runat="server" />
<asp:Button ID="AddP" runat="server" Text="Add Passengers" OnClick="AddP_Click" />
<asp:Panel runat="server" ID="passengerdet">
</asp:Panel>

服务器端代码:

protected void AddP_Click(object sender, EventArgs e)
{
   int rowCount = Convert.ToInt32(NoOfPsngr.Text);

   Table table = new Table();
   table.ID = "PsngrTbl";

   //Create the textboxes and labels each time the button is clicked.
   for (int i = 0; i < rowCount; i++)
   {
       TableRow row = new TableRow();

       TableCell namelblCell = new TableCell();
       Label namelbl = new Label();
       namelbl.Text = "Name";

       TableCell nameTxtCell = new TableCell();
       TextBox nameTxt = new TextBox();            

       TableCell typelblCell = new TableCell();
       Label typelbl = new Label();
       typelbl.Text = "Type";

       TableCell typeSelectCell = new TableCell();
       DropDownList typeSelect = new DropDownList();

       ListItem adultItem = new ListItem();
       adultItem.Text = "Adult";
       adultItem.Value = "Adult";
       typeSelect.Items.Add(adultItem);

       ListItem childItem = new ListItem();
       childItem.Text = "Child";
       childItem.Value = "Child";
       typeSelect.Items.Add(childItem);

       ListItem infantItem = new ListItem();
       infantItem.Text = "Infant";
       infantItem.Value = "Infant";
       typeSelect.Items.Add(infantItem);

       TableCell etktlblCell = new TableCell();
       Label etktlbl = new Label();
       etktlbl.Text = "Eticket No";

       TableCell etktTxtCell = new TableCell();
       TextBox etktTxt = new TextBox();

       //Adding.....
       namelblCell.Controls.Add(namelbl);
       typelblCell.Controls.Add(typelbl);
       etktlblCell.Controls.Add(etktlbl);

       nameTxtCell.Controls.Add(nameTxt);
       typeSelectCell.Controls.Add(typeSelect);
       etktTxtCell.Controls.Add(etktTxt);

       nameTxt.ID = "PName" + i;
       typeSelect.ID = "PType" + i;
       etktTxt.ID = "ETkt" + i;

       row.Controls.Add(namelblCell);
       row.Controls.Add(nameTxtCell);
       row.Controls.Add(typelblCell);
       row.Controls.Add(typeSelectCell);
       row.Controls.Add(etktlblCell);
       row.Controls.Add(etktTxtCell);

       table.Rows.Add(row);
   }

   passengerdet.Controls.Add(table);
}

1 个答案:

答案 0 :(得分:1)

使用像

这样的javascript
 $('#PsngrTbl').find('input[type=radio]').each(function (index, element) {
                    var o = $(this);
                    var oID = o.attr("id");
                    var oValue;
                    var controlName = $(this).attr('name');
                    if ($('[name=' + controlName + ']:checked').val() == undefined) {
                        oValue = "";
                    }
                    else {
                        oValue = $('[name=' + controlName + ']:checked').val();
                    }


                });
                $('#PsngrTbl').find('input[type=checkbox]').each(function (index, element) {
                    var o = $(this);
                    var value;
                    if (o.on == true) {
                        value = 1;
                    }
                    else {
                        value = 0;
                    }
                    var oID = o.attr("id");
                    var oValue = value;


                });
                $('#PsngrTbl').find('textarea').each(function (index, element) {
                    var o = $(this);
                    var oID = o.attr("id");
                    var oValue = o.val();


                });
                $('#PsngrTbl').find('select').each(function (index, element) {
                    var o = $(this);
                    var oID = o.attr("id");
                    var oValue = o.val();


                });

它将返回该表的所有控件值