简体   繁体   English

如何获取在按钮单击事件中动态创建的选中复选框值

[英]How to get checked checkbox value which is created dynamically in button click event

i am having checkboxes which are created dynamically in page_load event and put it in panel. 我有在page_load事件中动态创建的复选框,并将其放在面板中。

foreach (DataRow dr in column_ds.Rows)
        {
            column_checkbox = new CheckBox();
            column_checkbox.Text = (string)dr["COLUMN_NAME"];
            columnpanel1.Controls.Add(column_checkbox);
        }

now i want the checked check box values in btn_click event. 现在我想要btn_click事件中选中的复选框值。 any ideas? 有任何想法吗? i tried, 我试过了,

columnpanel1.FindControl("column_checkbox");

and

  CheckBox cb=(CheckBox)FindControl("column_checkbox");
        if (column_checkbox.Checked) { }
        {
            string name = column_checkbox.Text;
        }

On click event of button, you can implement below logic. 在按钮的单击事件上,您可以实现以下逻辑。

protected void btn_click(object sender, EventArgs e)
{
   foreach(var row in columnpanel1.Rows)
   {
       var tempchkBx= row.Controls[0] as CheckBox;
       if(tempchkBx.IsChecked)
       {
         //write your code
       }
   }
}

You should do this 你应该做这个

foreach (DataRow dr in column_ds.Rows)
        {
            column_checkbox = new CheckBox();
            column_checkbox.Text = (string)dr["COLUMN_NAME"];
            column_checkbox.ID = (string)dr["ID"]
            columnpanel1.Controls.Add(column_checkbox);
        }

Than you can find control using ID. 比您可以找到使用ID的控件。

Thank you for all your comments and answers. 感谢您的所有评论和答复。

Finally i got by, 终于我过去了

foreach (Control cb in columnpanel1.Controls)
        {
            if (cb is CheckBox)
            {
                CheckBox c = (CheckBox)cb;
                if (c.Checked)
                {
                    string s = c.Text;
                }
            }
        }

You can also do this in LINQ: 您也可以在LINQ中执行此操作:

var boxes = columnpanel1.Controls.OfType<CheckBox>().Where(c=>c.Checked).ToList();

 foreach(var chk in boxes )
        {
          string s = chk.Text;
        }

In case for that to work you need to add an event handler to dynamically added controls in your case 为了使它正常工作,您需要在事件中向动态添加的控件添加事件处理程序

checkbox = new CheckBox();
phChecklist.Controls.Add(checkbox);
checkbox.CheckedChanged += checkBox_CheckedChanged;

and then what you need to do in the method 然后你需要在方法中做什么

private void CheckBox_CheckedChanged(object sender, System.EventArgs e)
{
  ...
}

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

相关问题 如何在“保存单击”事件中获取复选框选中的值并将其保存在数据库中 - How to get the checkbox checked value in the save click event and save it in the database 如何编写动态创建的CheckBox的选中更改事件 - how to write the checked change event for dynamically created CheckBox 未触发动态创建复选框的选中更改事件 - the checked change event of dynamically created checkbox is not fired 如何在按钮单击事件的隐藏字段中存储逗号分隔的选中复选框的值? - How to store comma seperated checked checkbox value in hidden field on button click event? 如何检查是否选中了动态创建的复选框? - How to check that Dynamically created checkbox checked or not? 如何在wpf中查找动态创建的CheckBox - how to find dynamically created CheckBox checked in wpf 单击动态创建的按钮时,我无法进入事件 - I can not get into the event when I click the dynamically created button 按钮单击事件未触发动态创建的按钮 - Button Click event not firing for dynamically created button 单击提交按钮时,如何获取动态创建的复选框的值? - How can I get the value of a dynamically created checkbox when the submit button is clicked? 如何为 ListBox 中动态创建的 Button 获取索引并创建点击事件? - How to get index and create click event for dynamically created Button within ListBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM