简体   繁体   English

检索按钮的click事件背后的代码中动态生成的文本框的值?

[英]Retrieving the value of dynamically generated text boxes in code behind on a click event of a button?

In the code below I am genrating some textboxes dynamically. 在下面的代码中,我正在动态生成一些文本框。 And I am trying to get there values on the click event of a asp button. 我正在尝试获取有关asp按钮的click事件的值。 I have tried many of the ways but none seems to be working. 我尝试了许多方法,但似乎都没有用。 Also I am trying to avoid using ajax to send the values. 另外,我试图避免使用Ajax发送值。 Any help in this matter will be truly appreciated. 在这方面的任何帮助将不胜感激。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="product_entry.aspx.cs"Inherits="product_entry" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="pos.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script>
    $(document).ready(function() {
        var maxrow = 1;
        $('#TextBox1').val(maxrow);
        //var scrolled = 0;
        $("td.sno").each(function(index, element) {
            $(element).text(index + 1);
        });
        $(document).on("click", "button.remove", function() {
            $(this).parent().parent().remove();
            adding();
        });
        $(document).on("click", ".add, .remove,.tabrow:last", function() {
            numbering();
        });
        $(document).on("focus", ".tabrow:last", function() {
            // scrolled = scrolled + 200;

            //$("#table").animate({
            //   scrollTop: scrolled,20:"linear"
            //});

            adding();

        });

        function numbering() {

            $("td.sno").each(function(index, element) {
                $(element).text(index + 1);
                maxrow = index + 1;
            });



        }
        function iding() {

            $("tr").each(function(trIdx, element) {
                var trIndex = 'textbox' + (trIdx);
                // alert('hello');
                $(this).find("td > input").attr('id', function(idx) {
                    return trIndex + (idx + 1)
                });
            });
        }

        function adding() {
            $("tr.tabrow:first").clone(true).appendTo("#Table1");
            numbering();
            iding();
      $(".tabrow:last").children("td").children("input").each(function(index, element) {
                $(element).val("");
            });


            $('#TextBox1').val(maxrow);


        }
        $(document).on("click", ".check", function() {
            setdata();
        });
        function setdata() {
            var rowValues

            $("tr").each(function(index, value) {
                rowValues = new Array(); $(this).find('input').each(function() {
                    if ($(this).val() != '') {
                        alert('hello');
                        rowValues.push($(this).val());
                    }


                })
                alert("row" + index + " : " + rowValues);
                //do something with rowValues
            }
                     )

            $.ajax({
                type: "POST",
                url: "pdtentry.aspx/Done",
                contentType: "application/json; charset=utf-8",
                data: "{ info: rowValues}",
                dataType: "json",
                success: function(result) {
                    alert('Yay! It worked!');
                },
                error: function(result) {
                    alert('Oh no :(');
                }
            });


            return false;

        }
    });
</script>

</head>
<body>
<form id="form1" runat="server">
<div id="mhead">
  <asp:Label ID="Label1" runat="server" Text="Enter product into database"></asp:Label>
</div>
<div>
    <asp:TextBox ID="TextBox1" runat="server" Style="display: none;"></asp:TextBox>          
</div>
<asp:Panel ID="Pnl" runat="server">
    <div id="table">
        <asp:Table ID="Table1" runat="server">
            <asp:TableHeaderRow ID="head">
                <asp:TableHeaderCell ID="remove">-</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="sno">S.No</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="pdt_id">Product ID</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="pdt_descp">Product Description</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="plu">PLU</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="pdt_type">Product Type</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="cost">Cost</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="discount">Discount</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="qty">Quantity</asp:TableHeaderCell>
            </asp:TableHeaderRow>

        </asp:Table>
    </div>
</asp:Panel>
<div>
    <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="Button1_Click"
        UseSubmitBehavior="False" /></div>
<input id="Button2" type="button" value="button" class="check" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>

And code behind 和后面的代码

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Collections;
 using System.Web.Services;



public partial class product_entry : System.Web.UI.Page
{
int count;
static String[] a;

protected void Page_Load(object sender, EventArgs e)
{
int i,j;

    for (i = 0; i < 1; i++)
    {
        TableRow tr = new TableRow();
        tr.Attributes.Add("class", "tabrow");

        for (j = 0; j <= 8; j++)
        {
            TableCell tc = new TableCell();

            if (j == 0)
            {

                tc.Controls.Add(new LiteralControl("<Button class=remove type=button>-</button>"));

            }
            if (j == 1)
            {
                tc.Attributes.Add("class", "sno");
            }
            if (j == 2 || j == 3 || j == 4 || j == 5 || j == 6 || j == 7 || j == 8)
            {
                TextBox tb = new TextBox();


                tb.Style["width"] = "98%";

                tc.Controls.Add(tb);
            }
            tr.Controls.Add(tc);
        }
        Table1.Controls.Add(tr);
    }


}
[WebMethod]
public static string Done(string[] info)
{

    a = info;
    Label lb2=new Label();
    lb2.Text=""+a.Length;
    string str = string.Empty;
    return str;


}
protected void Button1_Click(object sender, EventArgs e)
{
    Label2.Text = TextBox1.Text;
       count = Convert.ToInt32(TextBox1.Text);
    for (int i = 1; i <= count; i++)
   {
       for (int j = 1; j <= 7; j++)
       {
           Response.Write("textbox" + i + j);
          string aa = ((TextBox)Pnl.FindControl("textbox" + i + j)).Text;
            Response.Write(((TextBox)Pnl.FindControl("textbox11")).Text);
    }
   }
    /* try
    {
        int count = Convert.ToInt32(TextBox1.Text);
        for (int i = 1; i <= count; i++)
        {
            for (int j = 1; j <= 7; j++)
            {
                Label l = new Label();

               string str = "textbox" + i + j;
               TextBox tb=((TextBox)this.FindControl(str)).Text;
                l.Text = tb.Text;
            }
        }
    }
    catch { }
    Panel[] pnls = new Panel[1] { Pnl };
    //ArrayList answers = new ArrayList();
    //Alternatively....
    ArrayList arrayList = new ArrayList();
    //string[] arr=new string[7];
    //arrayList.Add(arr);
    //for (int i = 0; i < 7  ; i++)
    //{
      // ArrayList list2 = new ArrayList();
       //arrayList.Add(list2);
     //}

    //System.Collections.Hashtable ht = new System.Collections.Hashtable();

    foreach (Panel Pnl in pnls)
    {
        foreach (Control childControl in Pnl.Controls)
        {
            if (childControl is TextBox)
            {
                TextBox box = (TextBox)childControl;
                //if(box.Text!=null)
                //Retrieve and store value from childControl.Text
                arrayList.Add(box.Text);
                foreach (int i in arrayList)
                {
                    Label l3 = new Label();
                    l3.Text = (String)arrayList[i];

                    //Alternatively...
                    //   ht.Add( box.ID, box.Text );
                }
            }
        }
    } */


}
}

the one way is to find item is Request.Forms collection 一种查找项目的方法是Request.Forms集合

Request.From["controlName"] 

This will contain the required value. 这将包含所需的值。 Note that it work on Name rather than Id so you should provide the Name as well. 请注意,它适用于Name而不是Id因此您也应该提供Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM