这是我的方案:如果!page.ispostback,我填写了一个来自数据库中数据的下拉列表!同样,在页面上还有一个butron和onclick,它从数据库中获取一个ID,并且在一个动态创建的按钮中,一个面板箱一个面板。问题是当我单击此动态创建的按钮_什么都没有发生,我无法解释原因。
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");
}
您也需要在页面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);
}