2

我创建了一个 Web 部件,有五种配置:在 usercontrol.ascx 中,我通过以下代码获取值:

namespace tasks_email.tasks_email_webpart
{
    public partial class tasks_email_webpartUserControl : UserControl
    {

        public tasks_email_webpart WebPart { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
 protected void btn_send_id_Click(object sender, EventArgs e)
        {

            string subject = " ";
            subject = this.WebPart.SUBJECT as string;
            string emailAddrS = " ";
            emailAddrS = this.WebPart.EMAILADDR as string;
            string checkout_changes = " ";
            checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string;
            SendCheckout("bo.wang@us.bnpparibas.com", subject, "this is a project test");

但是当我点击按钮时,它说:空引用异常,无论我编辑配置什么东西,变量都无法获取 SUBJECT 值

任何机构都可以告诉我如何处理吗?

*我检查了 webpart.cs,我确实写过:

    namespace tasks_email.tasks_email_webpart
{
    [ToolboxItemAttribute(false)]
    public class tasks_email_webpart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        [WebBrowsable(true), Category("Configurations"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Subject")]
        public string SUBJECT { get; set; }

在 ascx 中,我使用以下方法创建了一个按钮:

<asp:Button ID="btn_send_id" runat="server" Text=" Email_changes " 
onclick="btn_send_id_Click" CssClass="Search_Submit" onclientclick="return checkna()" 
    />

错误报告:

[NullReferenceException: Object reference not set to an instance of an object.]
   tasks_email.tasks_email_webpart.tasks_email_webpartUserControl.btn_send_id_Click(Object sender, EventArgs e) +375
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980
4

2 回答 2

1

当您开发 SP2010 可视化 Web 部件时,您应该始终从 Microsoft.SharePoint.WebPartPages.WebPart 继承,因此您的部件看起来像这样:

[ToolboxItemAttribute(false)]
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{

    [Personalizable(), WebBrowsable, Category("GPWF Settings")]
    public string WebClientUrl { get; set; }

}

因为您是从 UserControl 继承的,所以您总是在属性上获得空引用。

所以如果你不先设置你的道具,你应该像这样修改你的代码:

    string subject = this.WebPart.SUBJECT as string;
    string emailAddrS = this.WebPart.EMAILADDR as string;
    string checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string; 
于 2012-10-11T18:16:03.797 回答
0

更好的方法是在用户控件中添加 SUBJECT、EMAILADDR 和 CHECKOUTLISTSNAMES 作为公共属性,如下所示:

public string Subject {get;set;}

在您实例化用户控件的 Web 部件中,您编写:

protected override void CreateChildControls()
{
    var control = (tasks_email_webpartUserControl) Page.LoadControl(_ascxPath);
    control.Subject = this.SUBJECT;
}

通过这样做,您仍将主题设置为 Web 部件属性,但您将数据传递用户控件,而不是尝试用户控件获取数据。

于 2012-10-12T09:12:12.967 回答