I was creating a transformation earlier today where I had to define the RelationshipWithNodeGuid property of a cms:repeater nested within a Smart Search Results web part. The code looked something like this:
<script runat="server">
protected override void OnInit(EventArgs e)
{
If(!IsPostBack) {
base.OnInit(e);
WebPartID.RelationshipWithNodeGuid = (Guid)GetSearchValue("NodeGUID");
}
}
</script>
This worked just fine, until I also had to use a Smart Search Filter with the Smart Search results web part - which caused a post back to occur anytime you selected a condition to filter your search results. After the post back, the script would return false, so it would not set the RelationshipWithNodeGuid property of that nested repeater.
I figured the following would take care of that problem:
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
WebPartID.RelationshipWithNodeGuid = (Guid)GetSearchValue("NodeGUID");
}
</script>
However, this caused an exception upon postback:
Object not set to an instance of an object
I'm pretty new to C# and ASP.NET (surprise!), so I wasn't exactly sure why I was getting this error. My manager suggested I try the following:
<script runat="server">
protected override void OnInit(EventArgs e)
{
If(GetSearchValue("NodeGUID") != null) {
base.OnInit(e);
WebPartID.RelationshipWithNodeGuid = (Guid)GetSearchValue("NodeGUID");
}
}
</script>
This worked just perfectly, but neither of us were 100% certain why this solution worked as opposed to what I was trying. Would anyone care to explain what the problem was?