简体   繁体   English

如何使用C#根据用户输入在aspx页中动态打开TableRows?

[英]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. 我正在尝试使用用户提供的输入来动态打开在asp.net中创建的表中的一系列行。 How can I accomplish this using the C# code behind? 如何使用后面的C#代码完成此操作?

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). 所有列表都存在于asp页中,但是每一行的可见性都设置为false(我知道有一些方法可以在asp页中动态创建控件,但是我还没有找到一种方法可以在表格单元格中为我想要此页面的更整洁的组织)。

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: 我曾尝试过使用for循环,但是找不到使用可以动态设置为ID的字符串来调用行的正确方法,如下所示:

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. 但这总是返回null。 I also tried a foreach loop comparing the desired ID to this.Controls which would not work for the same reason. 我还尝试了一个foreach循环,将所需的ID与this.Controls进行比较,原因相同。 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). 如果有人知道如何以一种更简洁的方式执行此操作,我将不胜感激(在asp页中创建列表也可以,只要可以将它们组织到表中就可以了)。 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

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

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