简体   繁体   English

asp:button以编程方式创建:EventHandler不触发

[英]asp:button Created Programmatically: EventHandler does not fire

I am writing a SharePoint web part which will have a simple ASP.NET form. 我正在编写一个具有简单ASP.NET表单的SharePoint Web部件。 I am using HtmlTextWriter to render the controls. 我正在使用HtmlTextWriter呈现控件。 The problem I have is that my button does not seem to be triggering the EventHandler I have assigned it. 我的问题是我的按钮似乎没有触发我为其分配的EventHandler。

I initially declared the button in the CreateChildControls method, and wired the event handler: 我最初在CreateChildControls方法中声明了按钮,并连接了事件处理程序:

{
    Button submitButton;
    submitButton = new Button();
    submitButton.Text = "Go!";
    submitButton.Click += new EventHandler(submitButton_Click);
    Controls.Add(submitButton);
}

I have declared the functionality of the "submitButton_Click" EventHandler: 我已经声明了“ submitButton_Click” EventHandler的功能:

void submitButton_Click(object sender, EventArgs e)
{
    submitButton.Text = "Good!";
}

I render the controls: 我渲染控件:

protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
{ 
        RenderChildren(output);
}

Finally, I deploy the web part. 最后,我部署了Web部件。 It shows up fine in the catalog and when I add it to a page, the control shows up. 它在目录中显示良好,当我将其添加到页面时,该控件也会显示。 However, I would assume that when I click the button, its text should change from "Go!" 但是,我假设单击按钮时,其文本应更改为“ Go!”。 to "Good!" 为“好!” Instead, it does nothing. 相反,它什么也不做。 I'm pretty new to all of these technologies -- C#, Sharepoint, and ASP.NET -- so I'm pretty sure it's a problem with my understanding, but trying different steps from articles all over the net and previous questions here haven't fixed my problem. 我对所有这些技术(C#,Sharepoint和ASP.NET)都很陌生,因此我很确定这是我的理解问题,但是请尝试使用不同于网上文章的其他步骤以及此处以前的问题没解决我的问题。 Thanks for taking a look. 谢谢参观。

EDIT: I opened the SharePoint page with the web part on it and the button has been created like so: 编辑:我打开了带有Web部件的SharePoint页面,并按如下所示创建了按钮:

<input type="submit" name="ctl00$PlaceHolderMain$ctl00$ctl04" value="Go!" />

It looks like the OnClick value has not been added at all, which is what I thought adding the EventHandler would do. 似乎根本没有添加OnClick值,这就是我认为添加EventHandler可以做到的。 Am I trying to add OnClick in a completely wrong way? 我是否要以完全错误的方式添加OnClick? I also don't understand why the button name does not match what I declared in my code. 我也不明白为什么按钮名称与我在代码中声明的名称不匹配。

Look at this link: 查看此链接:

http://msdn.microsoft.com/en-us/library/ms452873.aspx http://msdn.microsoft.com/en-us/library/ms452873.aspx

You're overriding the RenderContents method incorrectly, and wiring the click event in the wrong place. 您错误地重写了RenderContents方法,并将click事件连接到错误的位置。

Are you inheriting from System.Web.UI.WebControls.WebParts.WebPart or the SharePoint WebPart (Sorry, I don't recall the namespace). 您是从System.Web.UI.WebControls.WebParts.WebPart还是SharePoint WebPart继承(对不起,我不记得命名空间了)。 It is recommended to inherit from "System.Web.UI.WebControls.WebParts.WebPart" so that it is a simple ASP.NET Web Part and it can also be used in SharePoint. 建议从“ System.Web.UI.WebControls.WebParts.WebPart”继承,以便它是一个简单的ASP.NET Web部件,也可以在SharePoint中使用。

Once you inherit from that class, you will need to override the "Render" method which is what I always do and it works out fine. 从该类继承后,您将需要重写“ Render”方法,这是我一直以来所做的,而且效果很好。 I think your CreatechildControls method is correct. 我认为您的CreatechildControls方法是正确的。 So your Render method will look like 所以您的Render方法看起来像

protected override void Render(System.Web.UI.HtmlTextWriter output)
{ 
        submitButton.RenderControl(write);
}

You will also need to declare your button outside the "CreateChildControls" method.When writing SharePoint WebParts, I always take this approach 您还需要在“ CreateChildControls”方法之外声明按钮。编写SharePoint WebParts时,我总是采用这种方法

  1. Inherit from System.Web.UI.WebControls.WebParts.WebPart 继承自System.Web.UI.WebControls.WebParts.WebPart
  2. Create a partial class which will be used to declare all the controls used by the WebPart and to override the following methods: "CreateChildControls", "OnInit", and "Render". 创建一个子类,该子类将用于声明WebPart使用的所有控件并覆盖以下方法:“ CreateChildControls”,“ OnInit”和“ Render”。

Hope it helps. 希望能帮助到你。

改用CreateChildControls替代,可以在适当的时候在ASP.NET Control呈现管道中调用此替代。

I had a similar problem. 我有一个类似的问题。 The event handler was always firing. 事件处理程序始终在触发。 I checked the view source of the SharePoint page and discovered the HTML control rendered was the input type = submit. 我检查了SharePoint页面的视图源,发现呈现的HTML控件是输入类型= Submit。 To solve the issue, I set the 'button.UseSubmitBehavior = false' in code and set the ID. 为了解决该问题,我在代码中设置了“ button.UseSubmitBehavior = false”并设置了ID。 UseSubmitBehavior by default in ASP.NET 2.0 is true, if not set in code. 如果未在代码中设置,则默认情况下,ASP.NET 2.0中的UseSubmitBehavior为true。

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

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