简体   繁体   English

为什么我的按钮的点击事件没有触发?

[英]Why is my button's click event not firing?

I have two Buttons on my Sharepoint 2010 Web Page. 我的Sharepoint 2010网页上有两个按钮。 One fires (server-side), the other doesn't*. 一个触发(服务器端),另一个不触发*。 They are set up similarly. 它们的设置类似。 Here is the one that does fire: 这是触发的:

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
}

...and here is the one that isn't invoked: ...这是未被调用的:

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
}        

Why is btnSave's handler invoked when I click it, but btnGeneratePDF's handler is not? 为什么单击btnSave的处理程序时会调用,而btnGeneratePDF的处理程序却未调用?

  • I can get buttons to fire client-side (jQuery) by using HtmlButtons, but I need this one to fire server-side/C#. 我可以使用HtmlButtons来触发客户端(jQuery)的按钮,但是我需要这个按钮来触发服务器端/ C#。

UPDATE 更新

Would the fact that I'm creating the pdfgen button inside the click handler of the other Button have anything to do with this? 我要在另一个Button的单击处理程序内创建pdfgen按钮的事实与此有关吗? Here's the significant part of that: 这是其中的重要部分:

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

?

UPDATE 2 更新2

Only the first (btnSave) button (and not the identically-created btnGeneratePDF button) is marked as type="submit": 只有第一个(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(&quot;ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" />

I believe only one button of type submit is allowed, but I tried to shove btnSave out of the way when I was done with it by setting its Enabled property to false, but that did nothing (good). 我相信只有一个类型为Submit的按钮是允许的,但是在完成btnSave时,我尝试将其Enabled属性设置为false,以免btnSave出现问题,但这无济于事。

If you assign the click event handler of the "pdf" button inside the click event of the Save button means that the PDF button is dynamically created and its click event assigned each time you press click on the save button. 如果在“保存”按钮的单击事件内部分配了“ pdf”按钮的单击事件处理程序,则意味着将动态创建PDF按钮,并在每次单击保存按钮时分配其单击事件。 Only at that time the pdf will get its event assigned. 仅在那时pdf才会分配其事件。

You must recreate the dynamic control in page processing. 您必须在页面处理中重新创建动态控件。 Here is an example of how to do it: 这是一个如何做的例子:

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";
    }

This is how I got it to work: 这就是我的工作方式:

0) Declare button at the top: 0)在顶部声明按钮:

Button btnGeneratePDF = null;

1) Create and configure it within the Page_Load() event (as suggested by Michael Palermo) 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);

Note that visible is set to false at first 请注意,首先将visible设置为false

2) Make the "Generate PDF" button visible within the "btnSave" click handler: 2)使“生成PDF”按钮在“ btnSave”点击处理程序中可见:

btnGeneratePDF.Visible = true;

That's it - it works fine now. 就是这样-现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM