为什么我的按钮的点击事件没有触发?
Why is my button's click event not firing?
我的 Sharepoint 2010 网页上有两个按钮。一个触发(服务器端),另一个不触发*。它们的设置类似。这是 触发的那个:
Button btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
. . .
private void btnSave_Click(object sender, EventArgs e)
{
LiteralControl message = null; // this breakpoint is hit, sho' nuff
. . . // code elided for brevity
}
...这是未调用的那个:
Button btnGeneratePDF = null;
. . .
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't work without it... (still doesn't work)
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
private void btnGenPDF_Click(object sender, EventArgs e)
{
GeneratePDF(listOfListItems); // breakpoint here never reached
}
为什么在我单击时调用 btnSave 的处理程序,但不调用 btnGeneratePDF 的处理程序?
- 我可以通过使用 HtmlButtons 获取按钮来触发客户端 (jQuery),但我需要这个来触发 server-side/C#。
更新
我在另一个按钮的点击处理程序中创建 pdfgen 按钮这一事实是否与此有关?这是其中的重要部分:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
. . . relatively insignificant code elided for brevity
Button btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't
work without it...still doesn't work; don't know why - configuration of this
button is the same as for btnSave
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
. . . relatively insignificant code elided for brevity
?
更新 2
只有第一个 (btnSave) 按钮(而不是相同创建的 btnGeneratePDF 按钮)被标记为 type="submit":
<input type="submit" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152" value="Save" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152", "", true, "", "", false, false))" />
我相信只有一个提交类型的按钮是允许的,但是当我完成它时,我试图通过将它的 Enabled 属性 设置为 false 来将 btnSave 推开,但这没有任何作用(好).
如果您在保存按钮的点击事件中分配 "pdf" 按钮的点击事件处理程序,则意味着动态创建 PDF 按钮并在您每次按下保存按钮时分配其点击事件.只有在那时 pdf 才会分配事件。
您必须在页面处理中重新创建动态控件。以下是如何操作的示例:
protected void Page_Load(object sender, EventArgs e)
{
Button b = new Button();
b.ID = "one";
b.Text = "one";
b.Click += B_Click;
this.form1.Controls.Add(b);
// this is where the dynamic control is created if id is in postback
if (Request["two"] != null)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
}
}
private void B_Click(object sender, EventArgs e)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
LabelOutput.Text += " ...one";
}
private void X_Click(object sender, EventArgs e)
{
LabelOutput.Text += " ...two";
}
我是这样工作的:
0) 在顶部声明按钮:
Button btnGeneratePDF = null;
1) 在 Page_Load() 事件中创建并配置它(按照 Michael Palermo 的建议)
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // I don't know if this is necessary
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
btnGeneratePDF.Visible = false;
this.Controls.Add(btnGeneratePDF);
注意visible一开始设置为false
2) 使 "Generate PDF" 按钮在 "btnSave" 单击处理程序中可见:
btnGeneratePDF.Visible = true;
就是这样 - 现在可以正常工作了。
我的 Sharepoint 2010 网页上有两个按钮。一个触发(服务器端),另一个不触发*。它们的设置类似。这是 触发的那个:
Button btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
. . .
private void btnSave_Click(object sender, EventArgs e)
{
LiteralControl message = null; // this breakpoint is hit, sho' nuff
. . . // code elided for brevity
}
...这是未调用的那个:
Button btnGeneratePDF = null;
. . .
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't work without it... (still doesn't work)
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
private void btnGenPDF_Click(object sender, EventArgs e)
{
GeneratePDF(listOfListItems); // breakpoint here never reached
}
为什么在我单击时调用 btnSave 的处理程序,但不调用 btnGeneratePDF 的处理程序?
- 我可以通过使用 HtmlButtons 获取按钮来触发客户端 (jQuery),但我需要这个来触发 server-side/C#。
更新
我在另一个按钮的点击处理程序中创建 pdfgen 按钮这一事实是否与此有关?这是其中的重要部分:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
. . . relatively insignificant code elided for brevity
Button btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't
work without it...still doesn't work; don't know why - configuration of this
button is the same as for btnSave
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
. . . relatively insignificant code elided for brevity
?
更新 2
只有第一个 (btnSave) 按钮(而不是相同创建的 btnGeneratePDF 按钮)被标记为 type="submit":
<input type="submit" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152" value="Save" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152", "", true, "", "", false, false))" />
我相信只有一个提交类型的按钮是允许的,但是当我完成它时,我试图通过将它的 Enabled 属性 设置为 false 来将 btnSave 推开,但这没有任何作用(好).
如果您在保存按钮的点击事件中分配 "pdf" 按钮的点击事件处理程序,则意味着动态创建 PDF 按钮并在您每次按下保存按钮时分配其点击事件.只有在那时 pdf 才会分配事件。
您必须在页面处理中重新创建动态控件。以下是如何操作的示例:
protected void Page_Load(object sender, EventArgs e)
{
Button b = new Button();
b.ID = "one";
b.Text = "one";
b.Click += B_Click;
this.form1.Controls.Add(b);
// this is where the dynamic control is created if id is in postback
if (Request["two"] != null)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
}
}
private void B_Click(object sender, EventArgs e)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
LabelOutput.Text += " ...one";
}
private void X_Click(object sender, EventArgs e)
{
LabelOutput.Text += " ...two";
}
我是这样工作的:
0) 在顶部声明按钮:
Button btnGeneratePDF = null;
1) 在 Page_Load() 事件中创建并配置它(按照 Michael Palermo 的建议)
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // I don't know if this is necessary
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
btnGeneratePDF.Visible = false;
this.Controls.Add(btnGeneratePDF);
注意visible一开始设置为false
2) 使 "Generate PDF" 按钮在 "btnSave" 单击处理程序中可见:
btnGeneratePDF.Visible = true;
就是这样 - 现在可以正常工作了。