简体   繁体   中英

How can I dynamically open TableRows in an aspx page based on user input using C#?

I am trying to use input provided by a user to dynamically open a series of rows in a table created in asp.net. How can I accomplish this using the C# code behind?

Basically, the user will input the number of crew members for a given job, then they will be given drop down lists with names of crew members and asked to identify their crew. The number of lists will be determined by the number they input. All of the lists exist in the asp page, but each row's visibility is set to false (I know there are ways to dynamically create Controls in an asp page, but I have not found a way to do so which creates them within table cell for the neater organization I want for this page).

I have tried using a for loop, but could not find the proper means of calling the rows using a string which could be set to the ID dynamically like this:

Int16 crewSize = Convert.ToInt16(Session["CrewSize"]);
            for (Int16 i = 2; i <= crewSize; i++)
            {
                String member = "TM" + i.ToString();
                TableRow row = (TableRow)FindControl(member);
                row.Visible = true;
            }

But this would always return null. I also tried a foreach loop comparing the desired ID to this.Controls which would not work for the same reason. So far, only the following will do what I want:

Int16 teamSize = Convert.ToInt16(Session["CrewSize"]);
            if(teamSize > 1)
            {
                TM2.Visible = true;
                if (teamSize > 2)
                {
                    TM3.Visible = true;
                    if (teamSize > 3)
                    {
                        TM4.Visible = true;
                        if (teamSize > 4)
                        {
                            TM5.Visible = true;
                            if (teamSize > 5)
                            {
                                TM6.Visible = true;
                                if (teamSize > 6)
                                {
                                    TM7.Visible = true;
                                    if (teamSize > 7)
                                    {
                                        TM8.Visible = true;
                                        if (teamSize > 8)
                                        {
                                            TM9.Visible = true;
                                            if (teamSize > 9)
                                            {
                                                TM10.Visible = true;
                                                if (teamSize > 10)
                                                {
                                                    TM11.Visible = true;
                                                    if (teamSize > 11)
                                                    {
                                                        TM12.Visible = true;
                                                        if (teamSize > 12)
                                                        {
                                                            TM13.Visible = true;
                                                            if (teamSize > 13)
                                                            {
                                                                TM14.Visible = true;
                                                                if (teamSize > 14)
                                                                {
                                                                    TM15.Visible = true;
                                                                    if (teamSize > 15)
                                                                    {
                                                                        TM16.Visible = true;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

As you can see, it is horribly ugly and likely inefficient. However, it does clearly show and execute what I am trying to accomplish. If anyone knows how to do this in a cleaner way, I would greatly appreciate the help (creating the lists in the asp page would also be fine as long as I could organize them into my table). Thanks for any and all help!

Without addressing the approach you're taking to a problem that can be solved purely client side, you could tidy up your nested statements considerably like so:

TM2.Visible = teamSize > 1;
TM3.Visible = teamSize > 2;
TM4.Visible = teamSize > 3;
TM5.Visible = teamSize > 4;
// etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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