简体   繁体   English

如何从动态生成的文本框中获取值?

[英]how to get values from dynamically generated text boxes?

I am trying to get the values of text box which i generated dynamically on page load and cloned them using jquery.... 我正在尝试获取我在页面加载时动态生成并使用jquery克隆它们的文本框的值。

every text box has a unique id in form of a matrix for eg textboxes of row one have ids textbox11,textbox12,textbox13,textbox14 etc for row two textbox21,textbox22,textbox23........ 每个文本框都具有矩阵形式的唯一ID,例如,第一行的文本框具有ID的textbox11,textbox12,textbox13,textbox14等,第二行的textbox21,textbox22,textbox23 ........

is there any way to get the values.. 有什么方法可以获取值。

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

public partial class product_entry : System.Web.UI.Page
{
    int count;
    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);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label2.Text = TextBox1.Text;
    for (int i = 1; i <= count; i++)
    {
        for (int j = 1; j <= 7; j++)
        {
            TextBox aa = (TextBox)Pnl.FindControl("textbox" + i + j);
            Response.Write(aa.Text);
        }
    }
    }
}

i want to fetch the values of hundreds of text boxes geerated using jquery by using the loop designed above is there any way to do that 我想通过使用上面设计的循环来获取使用jquery生成的数百个文本框的值,有没有办法做到这一点

you can add a specific css class to all dynamically generated text boxes on page load and by class selector using Jquery you can access all of them: 您可以在页面加载时向所有动态生成的文本框添加特定的CSS类,并使用Jquery通过类选择器访问所有这些文本框:

Use each: i is the postion in the array, obj is the DOM object that you are iterating (can be accessed with the jQuery wrapper $(this) as well). 分别使用: i是数组中的位置, obj是您要迭代的DOM对象(也可以使用jQuery包装器$(this)访问)。

$('input.SomeClass').each(function(i,obj){

var textboxid = $(this).id;
var textboxValue = $(this).val(); // get text inside text box

});

If an element has a unique ID, you can query that element using the selector format 如果元素具有唯一ID,则可以使用选择器格式查询该元素

$('#theExactId').val()

In HTML 4.01, IDs cannot start with a number. 在HTML 4.01中,ID不能以数字开头。 If your IDs really are 11, 12, etc you might want/need to prepend at least one alpha character. 如果您的ID确实是11、12等,则可能希望/需要至少加一个字母字符。

If you were trying to get the data on the server side, the variables would be part of the HTTP POST. 如果您试图在服务器端获取数据,则这些变量将是HTTP POST的一部分。

实际获得您使用的价值

$('#theExactId').val()

An ASP.NET Literal doesn't add any markup to the page. ASP.NET Literal不会在页面上添加任何标记。 Therefore you have to wrap your content in some container so that you can edit it via JavaScript: 因此,您必须将内容包装在某个容器中,以便可以通过JavaScript对其进行编辑:

ou should also consider refactoring all "hard" references to control ids from within your JavaScript code. 您还应该考虑从JavaScript代码中重构所有“硬”引用以控制ID。 In ASP.NET, there's the concept of naming containers that will ensure unique ids throughout the page. 在ASP.NET中,有一个命名容器的概念,可以确保整个页面的唯一ID。 So if you have a control called 因此,如果您有一个名为

 txtName 

in your Content section, the real name of the client (as seen from JavaScript) will be eg 在您的“内容”部分中,客户端的真实姓名(如从JavaScript中看到的)例如

 ct00_txtName 

if using masterpages. 如果使用母版页。 The prefix is automatically added by the naming container to make the id unique. 前缀由命名容器自动添加,以使ID唯一。

Fortunately, every control has a property (server-side) called ClientID which will reveal the actual id that is available on the client. 幸运的是,每个控件都有一个名为ClientID的属性(服务器端),它将显示客户端上可用的实际ID。 So if you need to access a control from client-side, make sure you always use ClientID property to get the name. 因此,如果您需要从客户端访问控件,请确保始终使用ClientID属性来获取名称。 Like this: 像这样:

var name = document.getElementById('<% =txtName.ClientID %>');

Make sure that your C# code that initially creates the text boxes and your jQuery that duplicates the tags assign a unique name to each tag. 确保最初创建文本框的C#代码和复制标签的jQuery为每个标签分配了唯一的名称。 So you have tags like this: 所以你有这样的标签:

<input name="textbox1" ... />
<input name="textbox2" ... />
<input name="textbox99" ... />

After you submitted the form via postback or Ajax, you can use the Request.Form collection to access the values of the text boxes like so: 通过回发或Ajax提交表单后,可以使用Request.Form集合来访问文本框的值,如下所示:

foreach (string key in Request.Form.Keys)
{
     if(key.StartsWith("textbox"))
     {
         string currentValue = Request.Form[key];
     }
}

If you submit the form using GET, you need to access Request.QueryString 如果使用GET提交表单,则需要访问Request.QueryString

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

相关问题 如何将占位符控件中动态生成的文本框中的值插入数据库? - How to insert Values from dynamically generated text-boxes in placeholder control into database? 当我从多个文本框中插入单个值时,如何将值返回到多个文本框中 - How to get values back to multiple text boxes as i inserted from multiple text boxes to single value 通过动态创建文本框获取文本值 - Get text value from dynamically creating text boxes 如何获取Gridview Row值的文本框? - How to get text boxes for Gridview Row values? 如何从程序化文本框获取值? - How to get values form programmatic text boxes? 如何动态创建文本框和按钮并从c#中的每个文本框中获取值? - How to create text boxes and buttons dynamically and get the value from each text box in c#? 如何获取动态生成的复选框的值 - How to get values of dynamically generated checkboxes 如何从ASP.NET中动态创建的文本框中获取数据 - How to get data from dynamically created text-boxes in ASP.NET 如何从Windows Phone 8.1中动态添加的文本框中获取数据? - how to get data from dynamically added text boxes in windows phone 8.1? 如何获取动态创建的文本框的 id 和总值? - How I get id and total value of dynamically created text boxes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM