Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:15:39+00:00 2026-06-15T07:15:39+00:00

I have dynamically created textbox in asp.net. Now i am extracting the values through

  • 0

I have dynamically created textbox in asp.net. Now i am extracting the values through following code.

string[] sublist = new string[] { };
int[] maxmarkslist = new int[] { };
int i;
for (i = 0; i < Convert.ToInt32(Label15.Text); i++)
{
    string sub = "subject" + i;
    string marks = "maxmarks" + i;
    TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
    TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
    sublist[i] = subject.Text;
    maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
}

But I getting error “Index was outside the bounds of the array” for the below two lines:

sublist[i] = subject.Text;
maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);

When I debugged it, values are coming in subject.Text and maxmarks.Text but not going to array.

Did I define the array in a wrong way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:15:40+00:00Added an answer on June 15, 2026 at 7:15 am

    You define both the arrays as empty arrays. So you will get index out of bound erros if you try to index into those.

    Arrays are not dynamically expanding. If you want that, use a collection type and may be later convert to an array.

    Try this:

    int length = Convert.ToInt32(Label15.Text);
    string[] sublist = new string[length-1];
    int[] maxmarkslist = new int[length-1];
    
    for (int i = 0; i < length; i++)
    {
        string sub = "subject" + i;
        string marks = "maxmarks" + i;
        TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
        TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
        sublist[i] = subject.Text;
        maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
    }
    

    Or here is how to do this with a collection (List) type:

            int length = Convert.ToInt32(Label15.Text);
            List<string> sublist1 = new List<string>();
            List<int> maxmarkslist1 = new List<int>();
    
            for (int i = 0; i < Convert.ToInt32(Label15.Text); i++)
            {
                string sub = "subject" + i;
                string marks = "maxmarks" + i;
                TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
                TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
                sublist1.Add(subject.Text);
                maxmarkslist1.Add(Convert.ToInt32(maxmarks.Text));
            }
    
            string[] sublist = sublist1.ToArray();
            int[] maxmarkslist = maxmarkslist1.ToArray();
    

    Note with collections you dont have to specify the size upfront. But keep adding items to it as it can expand as needed. But arrays can not do this.

    Your string[] sublist = new string[] { }; is a shortcut method where you create and initialize the array. In that you don’t have to specify the size, but compiler will count the elements between {} and set the size appropriately. In your case since there are no elements inside {} it will create an empty array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have textbox controls that are created dynamically in the ASP.NET. I want to
I have a dynamically created TextBox in a C#/ASP.NET web page that I want
I am learning ASP.Net. I have a dynamically created ImageButton that I would like
I have an asp.net page, some of its controls are created dynamically, these controls
I have dynamically created some list of RadioButtons with some values. pro grammatically I
I have a dynamically created (runtime creation) textbox whose name is available as a
I have a number of text boxes that are dynamically created via code. I
I have created a TextBox dynamically and attached a Tap event handler to it
A ASP.NET page's ViewState seems to have troubles keeping up with dynamically removed controls
I have a page that contains some dynamically created controls (TextBox and DropDownList). When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.