0

I am trying to get the values of text box which i generated dynamically on page load and cloned them using 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........

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

9
  • Assign a unique ID to each TextBox, then use ((TextBox)this.FindControl(someIDToLookFor)).Text in your C# code-behind to get its value. Commented Mar 28, 2014 at 20:32
  • i dont want to get value iin jquery i want it in c# or code behind so that i can send them to database Commented Mar 28, 2014 at 20:35
  • In case it wasn't clear, the code I provided is C#, not JavaScript. Commented Mar 28, 2014 at 21:41
  • but i want to run a loop because i have hundreds of text boxes like this 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; } } is there any way out Commented Mar 29, 2014 at 6:34
  • your button click event handler is doing exactly what you asked for... But you have to go down the tree recursive because you add your textboxes to the tablecell Commented Mar 29, 2014 at 10:44

5 Answers 5

1

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:

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).

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

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

});
Sign up to request clarification or add additional context in comments.

3 Comments

but how to get these values in C# so that they can be entered into database
Simpy the way you do in c#
id you textbox id is txtBox1 you do like txtBox1.Text to get its value which is in the textbox
0

If an element has a unique ID, you can query that element using the selector format

$('#theExactId').val()

In HTML 4.01, IDs cannot start with a number. If your IDs really are 11, 12, etc you might want/need to prepend at least one alpha character.

If you were trying to get the data on the server side, the variables would be part of the HTTP POST.

Comments

0

To actually get the value you use

$('#theExactId').val()

1 Comment

i dont want to get value iin jquery i want it in c# or code behind so that i can send them to database
0

An ASP.NET Literal doesn't add any markup to the page. Therefore you have to wrap your content in some container so that you can edit it via JavaScript:

ou should also consider refactoring all "hard" references to control ids from within your JavaScript code. In ASP.NET, there's the concept of naming containers that will ensure unique ids throughout the page. So if you have a control called

 txtName 

in your Content section, the real name of the client (as seen from JavaScript) will be e.g.

 ct00_txtName 

if using masterpages. The prefix is automatically added by the naming container to make the id unique.

Fortunately, every control has a property (server-side) called ClientID which will reveal the actual id that is available on the client. So if you need to access a control from client-side, make sure you always use ClientID property to get the name. Like this:

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

1 Comment

i dont want to get value in jquery or javascript,i want it in c# or code behind so that i can send them to database.is there any way to do that
0

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. 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:

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

2 Comments

yea the first thing you said is already achieved (all tags have unique id).but can i set the request.form[key] to be the text of lbel
The input tags need a unique name, not a unique Id for this to work. When submitting an html form the name of the input tag is used as the key for the Request.Form collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.