我有一個webusercontrol有一些控件就像一些標籤,一個文本框,最終一個按鈕。此控件的用途是每次單擊webusercontrol上的按鈕時將其添加到我的主頁中的佔位符中。C#asp.net Button_Click事件處理程序不工作
這是我的按鈕後面的代碼在我的三夏
protected void btnCriteriaToevoegen_Click(object sender, EventArgs e)
{
//New eventhandler == all of the eventhandlers of all the objects who have subscribed to the event.
EventHandler eventhandler = ButtonDoorgaan;
ButtonOpslaanEvent mijnevent = new ButtonOpslaanEvent();
//Basic variables I will give with my costum event(ButtonOpslaanEvent)
mijnevent.Naam = txtCriteriumNaam.Text;
mijnevent.Score = Convert.ToInt16(DdlCriteriumScoreSchaal.SelectedValue);
int weging = Convert.ToInt16(DdlCriteriumWeging.SelectedValue) - 1;
mijnevent.Weging = Convert.ToInt16(weging);
//If the eventhandler is not null, for every object that has an eventhandler, execute it.
if(eventhandler!=null)
eventhandler(sender, mijnevent);
}
需要的時候在我的主頁是這樣定義的觸發事件要執行的事件處理程序:
private void critlijn_ButtonDoorgaan(object sender, EventArgs e)
{
ButtonOpslaanEvent eigenevent = (ButtonOpslaanEvent)e;
IEnumerator<Domein> domeinenumerator = domeinen.GetEnumerator();
while (domeinenumerator.MoveNext())
{
if (domeinenumerator.Current.DomeinNaam.Equals(lijstdomeinitemgeselecteerd))
{
Criterium nieuwcriterium = new Criterium();
nieuwcriterium.CriteriumNaam = eigenevent.Naam;
nieuwcriterium.CriteriumScore = Convert.ToString(eigenevent.Score);
nieuwcriterium.CriteriumWeging = Convert.ToString(eigenevent.Weging);
domeinenumerator.Current.Criteriums.Add(nieuwcriterium);
}
}
btnCriteriaToevoegen_Click(sender, e);
}
的btnCriteriaToevoegen_Click事件觸發,然後調用此方法(addCriteriaButton()),該方法會將按鈕添加到我的主頁中的佔位符上:
private void addCriteriaButton()
{
Criterialijn criterialijn = (Criterialijn)LoadControl("~/Criterialijn.ascx");
//Add eventhandlers to control
criterialijn.ButtonDoorgaan += new EventHandler(critlijn_ButtonDoorgaan);
criterialijn.Aangevinkt += new EventHandler(critlijn_Aangevinkt);
//Every control on the page except this one, not enabled
IEnumerator<Criterialijn> criterialijnenumerator = criteriacontrols.GetEnumerator();
while (criterialijnenumerator.MoveNext())
{
criterialijnenumerator.Current.Enabled = false;
}
//Add it to a list of webusercontrols that are currently on screen
criteriacontrols.Add(criterialijn);
criterialijn.Enabled = true;
//Add to placeholder
plhCriteria.Controls.Add(criterialijn);
}
所以當所有這些都說完了,我運行我的程序時,他將控件添加到佔位符中,但是當我單擊按鈕時,他不會向佔位符添加新控件,只是清除佔位符因爲某些原因。通常情況下,一切都應該沒問題,但是我試着去看看他是否真的在點擊按鈕時觸發了事件,而他卻沒有。我試圖給你一個我的代碼樣本,因爲整個頁面的代碼是相當大的,並不會幫助你。任何想法他爲什麼不解開按鈕事件?
+1給我「當然!」感覺 – 2011-03-15 10:49:32