C# web部件中的NullReferenceException
我创建了一个web部件,共有五种配置:在usercontrol.ascx中,我通过以下代码获取值:C# web部件中的NullReferenceException,c#,asp.net,.net,sharepoint,sharepoint-2010,C#,Asp.net,.net,Sharepoint,Sharepoint 2010,我创建了一个web部件,共有五种配置:在usercontrol.ascx中,我通过以下代码获取值: namespace tasks_email.tasks_email_webpart { public partial class tasks_email_webpartUserControl : UserControl { public tasks_email_webpart WebPart { get; set; } protected voi
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");
但是当我点击按钮时,它说:null reference exception,无论我编辑配置的内容是什么,变量都无法获取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()"
/>
开发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; }
}
string subject = this.WebPart.SUBJECT as string;
string emailAddrS = this.WebPart.EMAILADDR as string;
string checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string;
public string Subject {get;set;}
因为您是从UserControl继承的,所以在属性上总是得到null引用
因此,如果您不先设置道具,您应该修改代码,如下所示:
[ToolboxItemAttribute(false)]
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
[Personalizable(), WebBrowsable, Category("GPWF Settings")]
public string WebClientUrl { get; set; }
}
string subject = this.WebPart.SUBJECT as string;
string emailAddrS = this.WebPart.EMAILADDR as string;
string checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string;
public string Subject {get;set;}
更好的方法是在usercontrol中添加SUBJECT、EMAILADDR和CheckOutlinesNames作为公共属性,如下所示:
[ToolboxItemAttribute(false)]
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
[Personalizable(), WebBrowsable, Category("GPWF Settings")]
public string WebClientUrl { get; set; }
}
string subject = this.WebPart.SUBJECT as string;
string emailAddrS = this.WebPart.EMAILADDR as string;
string checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string;
public string Subject {get;set;}
在web部件中,您在其中实例化用户控件,并编写:
protected override void CreateChildControls()
{
var control = (tasks_email_webpartUserControl) Page.LoadControl(_ascxPath);
control.Subject = this.SUBJECT;
}
这样做,,您仍然将主题设置为web部件属性,但您将数据传递给usercontrol,而不是尝试从usercontrol获取数据。您的类从什么对象类型继承?公共部分类任务\u电子邮件\u webpartUserControl:UserControlOk那么这就是问题的原因。声明的公共字符串主题是自动属性,但设置该属性的代码在哪里?另外:)如果该属性的类型为string,则可能不应在其上使用.ToString;)根据戴维斯的评论,我相信他找到了你问题的原因。你可能不是在设置道具,而只是先看它们。。。我会更新你的代码,这会帮你解决这个问题。。。仍然为空引用,是否有我忘记发布的信息?能否发布完整的web部件代码?您好,亲爱的Gregor,我刚刚编辑了我的问题,看看您是否满意我如何设置主题值:我进入前端,按下编辑按钮,选择配置并输入主题表单