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

时间:2015-07-09 16:35:08

标签: c# sharepoint-2010 submit-button

我的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)的按钮,但是我需要这个按钮来触发服务器端/ C#。

更新

我在另一个Button的点击处理程序中创建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(&quot;ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" />

我相信只允许一个提交类型的按钮,但是当我通过将其Enabled属性设置为false时,我尝试将btnSave推开,但这没有做任何事情(好)。

3 个答案:

答案 0 :(得分:1)

如果您指定&#34; pdf&#34;的点击事件处理程序“保存”按钮的单击事件内的按钮表示每次按下保存按钮时动态创建PDF按钮并分配其单击事件。只有在那个时候,pdf才能分配它的事件。

答案 1 :(得分:1)

您必须在页面处理中重新创建动态控件。以下是如何执行此操作的示例:

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

答案 2 :(得分:1)

这就是我开始工作的方式:

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);

请注意,首先将

设置为false

2)制作&#34;生成PDF&#34;在&#34; btnSave&#34;中可见的按钮点击处理程序:

btnGeneratePDF.Visible = true;

那就是它 - 它现在运作正常。