LoadPostData NullReferenceException

J

Jane Bell

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?
 
T

Teemu Keiski

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top