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

发布于 2024-08-12 03:58:54 字数 1162 浏览 9 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身软味 2024-08-19 03:58:54

查看此链接:

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.

偏爱你一生 2024-08-19 03:58:54

您是从 System.Web.UI.WebControls.WebParts.WebPart 继承还是从 SharePoint WebPart 继承(抱歉,我不记得命名空间了)。建议继承“System.Web.UI.WebControls.WebParts.WebPart”,这样它就是一个简单的ASP.NET Web Part,并且也可以在SharePoint中使用。

一旦您从该类继承,您将需要重写“Render”方法,这是我经常做的事情,并且效果很好。我认为你的 CreatechildControls 方法是正确的。因此,您的 Render 方法将如下所示

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

您还需要在“CreateChildControls”方法之外声明您的按钮。在编写 SharePoint WebParts 时,我总是采用这种方法

  1. 继承自 System.Web.UI.WebControls.WebParts.WebPart
  2. 创建一个分部类,其中将用于声明 WebPart 使用的所有控件并重写以下方法:“CreateChildControls”、“OnInit”和“Render”。

希望有帮助。

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

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

  1. Inherit from 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".

Hope it helps.

黯然#的苍凉 2024-08-19 03:58:54

请改用 CreateChildControls 替代,这会在 ASP.NET 控件渲染管道中的正确时刻被调用。

Use the CreateChildControls override instead, this gets called at the right moment in the ASP.NET Control rendering pipeline.

天涯沦落人 2024-08-19 03:58:54

我有类似的问题。事件处理程序始终在触发。我检查了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文