单击动态创建的按钮后,为什么我的函数没有执行

时间:2014-01-24 18:48:37

标签: asp.net event-handling

这是我的scenarioton:if!page.Ispostback我用数据库中的数据填充下拉列表! 同样在页面中有一个butron和onclick它从数据库中获取一个id并且包含一个面板IN WHIH是否有动态创建的按钮。问题是当我点击这个动态创建的按钮时_几乎没有发生,我无法解释为什么。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlLanguages.DataSource = this.catRep.GetAllAvailableLanguages();
                ddlLanguages.DataBind();
            }

            //IEnumerable<CatgoriesLanguages> allcategories = this.catRep.GetAllCategoriesByID(1)

        }



        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            inseredID = this.catRep.AddCategory();

            Label mylab = new Label();
            mylab.Text = "Yeeee" + inseredID;
            Page.FindControl("form1").Controls.Add(mylab);

            Panel myFieldSet = new Panel();
            myFieldSet.GroupingText= "Add New Category";
            Label lblTitle = new Label();
            lblTitle.Text="Title: ";
            myFieldSet.Controls.Add(lblTitle);
            TextBox txbTitle = new TextBox();
            txbTitle.ID = "txbTitle";
            myFieldSet.Controls.Add(txbTitle);
            myFieldSet.Controls.Add(new LiteralControl("<br />"));

            Label lblShrtDescrpt = new Label();
            lblShrtDescrpt.Text = "Short Description: ";
            myFieldSet.Controls.Add(lblShrtDescrpt);
            TextBox txbShrtDescrpt = new TextBox();
            txbShrtDescrpt.ID = "txbShrtDescrpt";
            myFieldSet.Controls.Add(txbShrtDescrpt);
            myFieldSet.Controls.Add(new LiteralControl("<br />"));
            Label lblDescrpt = new Label();
            lblDescrpt.Text = "Description: ";
            myFieldSet.Controls.Add(lblDescrpt);
            TextBox txbDescrpt = new TextBox();
            txbDescrpt.ID = "txbDescrpt";
            myFieldSet.Controls.Add(txbDescrpt);
            Button btnAddcategorieslanguage = new Button();
            btnAddcategorieslanguage.Click += new EventHandler(btnAddcategorieslanguage_Click);
            myFieldSet.Controls.Add(btnAddcategorieslanguage);
            Page.FindControl("form1").Controls.Add(myFieldSet);



        }
        public void btnAddcategorieslanguage_Click(object sender, EventArgs e)
        {

            TextBox txbTitle = (TextBox)FindControl("txbTitle");
            TextBox txbShrtDescrpt = (TextBox)FindControl("txbShrtDescrpt");
            TextBox txbDescrpt = (TextBox)FindControl("txbDescrpt");



            this.catRep.AddCategoriesLanguages(11, 2, "malee", "tariiiiii", "liliiii");

        }

1 个答案:

答案 0 :(得分:1)

您还需要在页面Init或页面加载事件中创建所有动态添加的控件。 像这样:

protected void Page_Load(object sender, EventArgs e)
        {
    if(ThereIsDynamicControl())
    {
    //You can set some session or viewState in the btnAddNew_Click to determine whether you need to add dynamic controls again here or not.
    }
         if (!IsPostBack)
            {
                ddlLanguages.DataSource = this.catRep.GetAllAvailableLanguages();
                ddlLanguages.DataBind();
            }
     //IEnumerable<CatgoriesLanguages> allcategories = this.catRep.GetAllCategoriesByID(1);
            }