Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > LoadPostData NullReferenceException

Reply
Thread Tools

LoadPostData NullReferenceException

 
 
Jane Bell
Guest
Posts: n/a
 
      03-03-2006
I have a webcontrol which renders a select | option html control. I have
implemented
IPostBackDataHandler with LoadPostData looking like this:

public bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
string oldSelectedValue = SelectedValue;
string newSelectedValue = postCollection.GetValues(postDataKey)[0];
if (newSelectedValue != null && !oldSelectedValue.Equals(newSelectedValue))
{
SelectedValue = newSelectedValue;
return true;
}
return false;
}

If I select a regular value in the dropdown, it works fine but selecting n
blank value (string.empty) does not cause the LoadPostData method to be
called so it is impossible to deselect a value.

If I add:
if (Page != null)
{
Page.RegisterRequiresPostBack(this);
}

to Page_OnInit then LoadPostData gets called every time, but I get a
NullReferenceException if I select a blank value.

This is because the postCollection does not contain an entry for the control
when I select a blank.

What do It do?


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      03-05-2006
Hi,

could you use hardcoded value which indicates empty selection or something?
E.g something which is unique for sure. You could replace that when
databinding initially and use in LoadPostData as basis to check if something
is selected (and let LoadPostData to ber called with
Page.RegisterRequiresPostBack)?

Or: You could also just check if the value in postCollection is null.
Normally when working with DDL, you clear the selection by setting the
SelectedIndex to -1, so perhaps setting SelectedValue to String.Empty in
code (or some hardcode value) could also work.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


"Jane Bell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a webcontrol which renders a select | option html control. I have
>implemented
> IPostBackDataHandler with LoadPostData looking like this:
>
> public bool LoadPostData(string postDataKey, NameValueCollection
> postCollection)
> {
> string oldSelectedValue = SelectedValue;
> string newSelectedValue = postCollection.GetValues(postDataKey)[0];
> if (newSelectedValue != null &&
> !oldSelectedValue.Equals(newSelectedValue))
> {
> SelectedValue = newSelectedValue;
> return true;
> }
> return false;
> }
>
> If I select a regular value in the dropdown, it works fine but selecting n
> blank value (string.empty) does not cause the LoadPostData method to be
> called so it is impossible to deselect a value.
>
> If I add:
> if (Page != null)
> {
> Page.RegisterRequiresPostBack(this);
> }
>
> to Page_OnInit then LoadPostData gets called every time, but I get a
> NullReferenceException if I select a blank value.
>
> This is because the postCollection does not contain an entry for the
> control when I select a blank.
>
> What do It do?
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Control & System.Web.UI.IPostBackDataHandler.LoadPostData One Handed Man \( OHM - Terry Burns \) ASP .Net 1 06-17-2004 06:07 PM
LoadPostData not being called at the right time for controls dynamically added at page load Sam ASP .Net 2 04-26-2004 07:19 AM
RegisterRequiresPostback failing to call LoadPostData John Burke ASP .Net 2 04-26-2004 07:12 AM
LoadPostData and LinkButtons Lucas Tam ASP .Net 0 08-18-2003 12:57 PM
LoadPostData William F. Robertson, Jr. ASP .Net 1 07-24-2003 04:39 PM



Advertisments