asp:button 以编程方式创建:EventHandler 不触发
我正在编写一个 SharePoint Web 部件,它将有一个简单的 ASP.NET 表单。我正在使用 HtmlTextWriter 来呈现控件。我遇到的问题是我的按钮似乎没有触发我分配给它的事件处理程序。
我最初在 CreateChildControls 方法中声明了该按钮,并连接了事件处理程序:
{
Button submitButton;
submitButton = new Button();
submitButton.Text = "Go!";
submitButton.Click += new EventHandler(submitButton_Click);
Controls.Add(submitButton);
}
我已经声明了“submitButton_Click”事件处理程序的功能:
void submitButton_Click(object sender, EventArgs e)
{
submitButton.Text = "Good!";
}
我渲染了控件:
protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
{
RenderChildren(output);
}
最后,我部署了 Web 部件。它在目录中显示良好,当我将其添加到页面时,控件就会显示。但是,我假设当我单击该按钮时,其文本应该从“Go!”更改为“Go!”到“好!”相反,它什么也不做。我对所有这些技术 - C#、Sharepoint 和 ASP.NET - 都很陌生,所以我很确定这是我的理解问题,但尝试了网络上的文章和之前的问题中的不同步骤没有解决我的问题。感谢您的关注。
编辑:我打开了带有 Web 部件的 SharePoint 页面,并且已创建按钮,如下所示:
<input type="submit" name="ctl00$PlaceHolderMain$ctl00$ctl04" value="Go!" />
看起来根本没有添加 OnClick 值,这就是我认为添加 EventHandler 会做的事情。我是否尝试以完全错误的方式添加 OnClick?我也不明白为什么按钮名称与我在代码中声明的名称不匹配。
I am writing a SharePoint web part which will have a simple ASP.NET form. I am using HtmlTextWriter to render the controls. The problem I have is that my button does not seem to be triggering the EventHandler I have assigned it.
I initially declared the button in the CreateChildControls method, and wired the event handler:
{
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:
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. 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!" 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. 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:
<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. Am I trying to add OnClick in a completely wrong way? I also don't understand why the button name does not match what I declared in my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看此链接:
http://msdn.microsoft.com/en-us /library/ms452873.aspx
您错误地重写了 RenderContents 方法,并将单击事件连接到错误的位置。
Look at this link:
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.
您是从 System.Web.UI.WebControls.WebParts.WebPart 继承还是从 SharePoint WebPart 继承(抱歉,我不记得命名空间了)。建议继承“System.Web.UI.WebControls.WebParts.WebPart”,这样它就是一个简单的ASP.NET Web Part,并且也可以在SharePoint中使用。
一旦您从该类继承,您将需要重写“Render”方法,这是我经常做的事情,并且效果很好。我认为你的 CreatechildControls 方法是正确的。因此,您的 Render 方法将如下所示
您还需要在“CreateChildControls”方法之外声明您的按钮。在编写 SharePoint WebParts 时,我总是采用这种方法
希望有帮助。
Are you inheriting from System.Web.UI.WebControls.WebParts.WebPart or the SharePoint WebPart (Sorry, I don't recall the namespace). 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.
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. I think your CreatechildControls method is correct. So your Render method will look like
You will also need to declare your button outside the "CreateChildControls" method.When writing SharePoint WebParts, I always take this approach
Hope it helps.
请改用 CreateChildControls 替代,这会在 ASP.NET 控件渲染管道中的正确时刻被调用。
Use the CreateChildControls override instead, this gets called at the right moment in the ASP.NET Control rendering pipeline.
我有类似的问题。事件处理程序始终在触发。我检查了SharePoint页面的查看源,发现呈现的HTML控件是输入类型=提交。为了解决这个问题,我在代码中设置了“button.UseSubmitBehavior = false”并设置了ID。如果未在代码中设置,则 ASP.NET 2.0 中的 UseSubmitBehavior 默认情况下为 true。
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. To solve the issue, I set the 'button.UseSubmitBehavior = false' in code and set the ID. UseSubmitBehavior by default in ASP.NET 2.0 is true, if not set in code.