简体   繁体   English

在页面中找不到Checkbox控件

[英]Cannot find Checkbox controls in page

I am trying to take the Checkboxes on the page and insert their Text into the database. 我试图在页面上取回复选框并将其文本插入数据库。 When I click the submit button it doesn't find any of the Checkbox controls. 当我单击提交按钮时,它找不到任何Checkbox控件。 I am trying to enumerate through all of them and insert each. 我试图通过所有列举,并插入每个。 I need to use individual checkboxes rather than a checkboxlist for this project in case someones suggests I use a list. 我需要使用单独的复选框而不是此项目的复选框列表,以防有人建议我使用列表。 Why aren't my controls showing up? 为什么我的控件不显示?

C# - Submit button C#-提交按钮

protected void Submit_Click(object sender, EventArgs e)
{
// removed code to shorten up
  foreach (Control c in Page.Controls)
  {
    foreach (Control childc in c.Controls)
    {
      if ((childc is CheckBox) && ((CheckBox)childc).Checked)
      {
         cmd.Parameters.Clear();
         cmd.Parameters.AddWithValue("@Name", ((CheckBox)childc).Text);
         cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now.ToString());
         cmd.ExecuteNonQuery();
       }
    }
  }
}

C# - Where checkboxes are created and inserted into panel. C# - 创建复选框并将其插入面板。 I hit a button called Submit_Click_Generate which calls this and populates/creates the checkboxes. 我点击一个名为Submit_Click_Generate的按钮,调用此按钮并填充/创建复选框。

protected void Submit_Click_Generate(object sender, EventArgs e)
{
// removed code to shorten up
  foreach (System.Xml.XmlNode node in nodes)
  {
     string displayname = node.SelectSingleNode("FName").InnerText.Trim();
     string idnumber = node.SelectSingleNode("idNum").InnerText.Trim();
     CheckBox ckBox = new CheckBox();
     ckBox.InputAttributes.Add("value", idnumber);
     panel1.Controls.Add(new LiteralControl("<div class='checkbox checkbox-slider-lg checkbox-slider--b-flat'><label>"));
     panel1.Controls.Add(ckBox);
     panel1.Controls.Add(new LiteralControl("<span>" + displayname + "</span></label></div>"));
    }
}

All of my checkboxes are dynamically inserted into this panel on my page 我所有的复选框都动态插入到我页面的此面板中

.ASPX .ASPX

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   <form id="form1" runat="server">
       <asp:Button ID="btn" runat="server" OnClick="Submit_Click_Generate" Text="Generate" />
       <asp:Panel ID="panel1" runat="server"></asp:Panel>
       <asp:Button ID="btn1" runat="server" OnClick="Submit_Click" Text="Save" />
   </form>
</asp:Content>

Also tried this but it didn't pick up the controls either: 也试过这个,但它没有拿起控件:

foreach (var ctrl in this.Controls.OfType<CheckBox>().Where(x => x.Checked))

Use... 采用...

 foreach (Control childc in panel1.Controls)
  {
    if ((childc is CheckBox) && ((CheckBox)childc).Checked)
    {
       cmd.Parameters.Clear();
       cmd.Parameters.AddWithValue("@Name", ((CheckBox)childc).Text);
       cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now.ToString());
       cmd.ExecuteNonQuery();
     }
  }

For dynamically generated controls you need to recreate then in onInit event of the page when you click on submit button.according to the follownig link: Why dynamically created user controls disappear when controls are not doing full postbacks 对于动态生成的控件,您需要在单击提交按钮时在页面的onInit事件中重新创建。根据以下链接: 为什么动态创建的用户控件在控件未执行完全回发时消失

You need to populate them on each Page Init event in order to persist Viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next Postback. 您需要在每个Page Init事件上填充它们以保持Viewstate,在按钮单击期间添加到更新面板内的控件的事件似乎在下一个Postback之前不会被注册。 My suggestion is to keep a list of what you have added dynamically, and store it in a session variable 我的建议是保留动态添加的列表,并将其存储在会话变量中

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

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