简体   繁体   English

如何访问以编程方式添加的复选框

[英]How to Access Programmatically Added checkbox

I am developing an application in which i needed the to add filter. 我正在开发一个需要添加过滤器的应用程序。 for that am adding checkbox programmatically because the filtering option are not predefine it depends on the value in the table to i add the checkbox as fallow 为此,由于过滤选项未预先定义,因此以编程方式添加复选框,这取决于表中的值,我将复选框添加为休假

public void addCusineCheckbox()
{
            DataTable CusineList = Sql.ExecuteSelectCommand("select prod_group_id,Prod_group_name from Master_Prod_Groups");
            for (int i = 0; i < CusineList.Rows.Count; i++)
            {
                CheckBox chkCusine = new CheckBox();
                chkCusine.ID = "chk" + CusineList.Rows[i]["Prod_group_name"].ToString();
                chkCusine.Text = CusineList.Rows[i]["Prod_group_name"].ToString();
                divCusineFilter.Controls.Add(chkCusine);
            }
}

which help user to select the required field(there more other option to select) and then click on apply filter on that am try to access the that added check box as fallow 这可以帮助用户选择必填字段(还有其他更多选项可供选择),然后在该应用上单击“应用过滤器”,以尝试访问添加的复选框作为休假框

public string getCusineFilterString()
{

            string CusineID =null;
            DataTable CusineList = Sql.ExecuteSelectCommand("select prod_group_id,Prod_group_name from Master_Prod_Groups");
            for (int i = 0; i < CusineList.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)Page.FindControl("chk" + CusineList.Rows[i]["Prod_group_name"]);
                if (chk.Checked == true)
                {
                    if (i == 0)
                    {
                        CusineID = CusineList.Rows[i]["prod_group_id"].ToString();
                    }
                    else
                    {
                        CusineID = CusineID + "," + CusineList.Rows[i]["prod_group_id"].ToString();
                    }
                }
            }
            return CusineID;
}

but it gives the error that object not set to an instance. 但它给出了对象未设置为实例的错误。

i am not getting any idea to access to the checkbox. 我不知道要访问该复选框。

There are certain limitations: 有一些限制:

  • You cannot access any controls in ASP.net which are not added before or on Init. 您无法访问ASP.net中没有在Init之前或之前添加的任何控件。 So you can add the controls on Page OnInit, and then it will be accessible. 因此,您可以在Page OnInit上添加控件,然后将可以访问它。 But you have to be careful in adding the controls, so that they are added at right place and not duplicated. 但是您在添加控件时必须小心,以便将它们添加到正确的位置而不要重复。

  • You should not write 你不应该写

    CheckBox chk = (CheckBox)Page.FindControl("chk"); CheckBox chk =(CheckBox)Page.FindControl(“ chk”);

instead write 改写

var chkControl = Page.FindControl("chk");
if(chkControl != null /*&& check type*/) {
  CheckBox chk = (CheckBox)chkControl;
// and do something
}
  • It will be better if you use CheckBoxList Control at design time and change the binding values to populate different check boxes at runtime 如果您在设计时使用CheckBoxList控件并在运行时更改绑定值以填充不同的复选框会更好。

  • Create check box(s) at design time and show hide based on user action. 在设计时创建复选框,并根据用户操作显示隐藏。

Hope it helps 希望能帮助到你

You can get the values using Request.Forms collection. 您可以使用Request.Forms集合获取值。 It keeps the values of every control which is on the form. 它保留窗体上每个控件的值。 But it works on name rather than ID . 但是它只作用于name而不作用于ID

Get dynamic control (unique ID) on postback 在回发中获取动态控制(唯一ID)

The better approach is use empty CheckBoxList and add item at run-time. 更好的方法是使用空的CheckBoxList并在运行时添加项目。

Here is a link which has discussion on CheckBoxList 这是一个有关CheckBoxList的链接
How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart 如何获取CheckBoxList选定的值,我所拥有的似乎不起作用C#.NET / VisualWebPart

Example

Create an empty 创建一个空的

<asp:CheckBoxList ID="ChkBox1" runat="server"></asp:CheckBoxList>

And add items at runtime 并在运行时添加项目

 ChkBox1.Items.Add(new ListItem("Item 1", "Item1"));

And you can get data by using foreach loop as follow 您可以通过使用如下的foreach循环获取数据

  // Loop through each item.
    foreach (ListItem item in ChkBox1.Items)
    {
        if (item.Selected)
        {
            // If the item is selected, add the value to the list.

        }
        else
        {
            // Item is not selected, do something else.
        }
    }

You can add your checkboxes in Page_Load. 您可以在Page_Load中添加您的复选框。 I think the problem is in using Page.FindControl. 我认为问题出在使用Page.FindControl。 Use divCusineFilter.FindControl rather than Page.FindControl and make sure that you add your checkboxes to page in postback too. 使用divCusineFilter.FindControl而不是Page.FindControl,并确保您也将复选框添加到回发页面中。

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

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