如何获取动态生成的控件的值并在asp.net中调用它们的事件#

时间:2017-07-27 06:23:42

标签: c# asp.net webforms asp.net-webpages

这是我在2个面板中创建动态控件的代码。 现在我想从这些动态控件中获取值。我还生成了动态按钮及其事件,但是当我点击按钮时它不会达到断点,页面会回发。我在网上看到我必须生成控件或将它们的值存储到Asp.net页面生命周期的PreInit中。但我无法理解。如果有人知道它回复我。 感谢。

  public void showControls(string p)
    {

        Button btn = new Button();
        List<string> lblUD = getUserDetails(p);
        List<string> lblli = getWebPagesName();
        for (int i = 0; i < o; i++)
        {

            HtmlAnchor ha = new HtmlAnchor();
            CheckBox cb = new CheckBox();
            Label lbl = new Label();
            cb.Checked = false;

            cb.ID = "cb" + i.ToString();
            cb.Text = "Allow";

            lbl.Text = lblli[i].ToString();

            lbl.ID = "lbl" + i.ToString();
            ha.Controls.Add(lbl);
            ha.Controls.Add(new LiteralControl("<br/>"));
            lbl.Style.Add("line-height", "28px");
            //lbl.Style.Add("border", "1px solid");
            //cb.Style.Add("border", "1px solid");

            lblPanel.Controls.Add(ha);

            //lblPanel.Controls.Add( new LiteralControl("<br/>"));

            cb.Style.Add("line-height", "24px");
            cbPanel.Controls.Add(cb);
            cbPanel.Controls.Add(new LiteralControl("<br/>"));    
            Label2.Enabled = true;
            Label2.Text = "<h2>Assign Roles</h2>";
        }
        for (int s = 0; s < lblUD.Count; s++)
        {
            Label lblud = new Label();
            lblud.ID = "lblud" + s.ToString();
            lblud.Text = lblUD[s].ToString();
            lblud.Style.Add("line-height", "28px");
            userdetailPanel.Controls.Add(lblud);
            userdetailPanel.Controls.Add(new LiteralControl("<br/>"));
        }
        btn.ID = "btn_Submit";
        btn.Text = "Save";
        btn.Click += new System.EventHandler(this.btn_Submit_click);
        cbPanel.Controls.Add(btn);

    }

0 个答案:

没有答案