获取动态创建的复选框值 [英] Get dynamically created checkbox values

查看:56
本文介绍了获取动态创建的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我有一个网页,上面有一个向导。在向导中,我动态创建了Accordions,在手风琴中,有动态创建的复选框。它运行正常,但是当用户点击下一个时,我需要将选中的文本框id-s放入列表中。我的问题是,我不知道,如何检查文本框是否被检查。



这是我的代码:

Hi!

I have a webpage, with a wizard on it. In the wizard I dynamically created Accordions, and in the accordions, there are dynamically created checkboxes. It works fine, but I need to put the checked textbox id-s in to a list, when the user clicks next. My problem is, that I don't know, how to get wich textboxes are checked.

Here is my code:

private void PopulateAcrDynamically()
    {
        List<string[]> parameterek = new List<string[]>();

        int M1 = 1;

        parameterek.Add(new string[3] { "@M1", "SqlDbType.Int", M1.ToString() });

        DataTable eredmeny = AdatEleres.TaroltEljarasHivas("GetCompetenciesByMandatory", parameterek);

        parameterek.Clear();

        DataTable Behaviors = new DataTable();
        DataTable Questions = new DataTable();
        if (eredmeny.Rows.Count != 0)
        {
            Label lbTitle;
            GridView gvContent;
            GridView gvQuestions;
            AjaxControlToolkit.AccordionPane pn;
            int i = 0;

        foreach (DataRow dr in eredmeny.Rows)
        {
                int k = 0;
                k++;
                parameterek.Add(new string[3] { "@CompID", "SqlDbType.Int", dr[0].ToString() });
                Behaviors = AdatEleres.TaroltEljarasHivas("GetBehaviors", parameterek);
                parameterek.Clear();

                parameterek.Add(new string[3] { "@CompID", "SqlDbType.Int", dr[0].ToString() });
                Questions = AdatEleres.TaroltEljarasHivas("GetQuestions", parameterek);
                parameterek.Clear();

                lbTitle = new Label();
                gvContent = new GridView();
                gvQuestions = new GridView();

                gvContent.DataSource = Behaviors;
                gvContent.DataBind();

                gvContent.HeaderRow.Visible = false;
                gvContent.HorizontalAlign = HorizontalAlign.Left;

                foreach (GridViewRow gvr in gvContent.Rows)
                {
                    gvr.Cells[0].Visible = false;
                    gvr.Cells[1].Visible = false;
                }

                gvQuestions.DataSource = Questions;
                gvQuestions.DataBind();

                gvQuestions.HeaderRow.Visible = false;
                gvQuestions.HorizontalAlign = HorizontalAlign.Center;

                foreach (GridViewRow gvr in gvQuestions.Rows)
                {
                    gvr.Cells[0].Visible = false;
                    gvr.Cells[1].Visible = false;
                }

                cbSelect = new CheckBox();
                cbSelect.Text = "Kiválasztás";
                cbSelect.ID = dr[0].ToString();

                lbTitle.Text = dr["Name"].ToString();
                pn = new AjaxControlToolkit.AccordionPane();
                pn.ID = "Pane" + i;
                pn.HeaderContainer.Controls.Add(lbTitle);
                pn.ContentContainer.Controls.Add(gvContent);
                pn.ContentContainer.Controls.Add(gvQuestions);
                pn.ContentContainer.Controls.Add(new LiteralControl("<p />"));
                pn.ContentContainer.Controls.Add(cbSelect);
                Accordion1.Panes.Add(pn);
                ++i;
            }
        }





提前感谢您的答案。



Thanking you in advance for your answers.

推荐答案

假设你想在客户端读取下面的javascript值,你可以参考这个网址。



< a href =http://stackoverflow.com/questions/4606791/how-to-access-checkboxes-and-their-values-with-getelementsbyname>如何使用getElementsByName访问复选框及其值 [< a href =http://stackoverflow.com/questions/4606791/how-to-access-checkboxes-and-their-values-with-getelementsbynametarget =_ blanktitle =New Window> ^ ]



以下网址显示如何使用jQuery访问复选框值



获取jQuery中的复选框值 [ ^ ]
Assuming that you are wanted to read values at client side using javascript below is the url you can refer.

how to access checkboxes and their values with getElementsByName[^]

Below url shows how to access checkbox values using jQuery

Get checkbox value in jQuery[^]


好的,我已经成功了:



Ok, I've made it:

foreach (Control pane in Accordion1.Panes)
            {
                //Check the controls in the Accordions Content
                foreach (Control item in pane.Controls[1].Controls) 
                {
                    //Is it a CheckBox? I have multiple controls, but only 1 CheckBox
                    if (item is CheckBox)
                    {
                        //Check if it's checked
                        if (((CheckBox)item).Checked == true)
                        {
                            lstID.Add(item.ID);
                        }
                    }
                }
            }



如果选中了,则会返回复选框的ID单击按钮。


This will give back the ID of the checkbox when, if it's checked a button is clicked.


这篇关于获取动态创建的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆