繁体   English   中英

在asp.net上获取RadiobuttonList的ID列表

[英]Getting list of IDs for RadiobuttonList on asp.net

在此方面需要帮助,因为与此相关的大多数答案对我来说都不起作用。

我有一个带有以下内容的aspx文件:

<p><asp:RadioButtonList ID="id001" RepeatLayout="Flow" RepeatDirection="Horizontal" runat="server">
              <asp:ListItem class="radio-inline" Value="0" Text="None" Selected="True"></asp:ListItem>
              <asp:ListItem class="radio-inline" Value="1" Text="Basic"></asp:ListItem>
              <asp:ListItem class="radio-inline" Value="2" Text="Expert"></asp:ListItem>
              </asp:RadioButtonList></p>

以及其他ID为“ id002”,“ id003”等。

因此,我想获取RadioButtonList的列表,并为每个ID采取措施。

我在aspx.cs上尝试过这样的事情:

foreach(Control c in Page.Controls)
            {


                        if (c.ID.Contains("id"))
                        {
                            string FullID = c.ID.ToString();
                            int StrippedID = Convert.ToInt32(FullID.Remove(0, 2));

                            string SQLSubmit = "INSERT INTO SkillData VALUES ('1', '01', '01', '01', '" + StrippedID + "' )";
                            SqlCommand Submit = new SqlCommand(SQLSubmit, SQLConn);
                            Submit.ExecuteNonQuery();

                        }

            }

我不太了解控件ID的搜索方式。 当我调试应用程序时,只有深入了解变量,我才能看到此信息,这就是为什么我不知道如何到达它的原因:

(App.Skill)(System.Web.UI.Control)(新System.Linq.SystemCore_EnumerableDebugView(c.Controls).Items [3]))。Page).id001.ID值id001

对如何解决此问题有帮助吗?

谢谢。

//If Page is with Master Page Add 
  Control form = Master.FindControl("form1");
  Control content1 = form.FindControl("ContentPlaceHolder1"); 
  //and Replace this with content1 in loop. Same with LINQ version

  foreach (Control c in content1.Controls)
 // Here this refers to Page Controls. If page is with MasterPage then first 
 //we need to go to parent container.
 //Let's say all your controls are inside of Panel, then you need to use 
 //panel1.Controls where panel1 is ID of Panel.
            {
            if (c is RadioButtonList)
            {
                if (c.ID.Contains("id"))
                {
                    string FullID = c.ID.ToString();
                }
            }
            }

如果要与上述版本一起使用LINQ- Same,请首先转到父容器

   foreach (Control c in this.Controls.OfType<RadioButtonList>())
            {

                if (c.ID.Contains("id"))
                {
                    string FullID = c.ID.ToString();
                }

            }

让我知道它是否无效。

暂无
暂无

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

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