LoadPostData NullReferenceException

Discussion in 'ASP .Net Building Controls' started by Jane Bell, Mar 3, 2006.

  1. Jane Bell

    Jane Bell Guest

    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?
    Jane Bell, Mar 3, 2006
    #1
    1. Advertising

  2. Jane Bell

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    >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?
    >
    Teemu Keiski, Mar 5, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.

    LoadPostData

    William F. Robertson, Jr., Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    510
    William F. Robertson, Jr.
    Jul 24, 2003
  2. Lucas Tam

    LoadPostData and LinkButtons

    Lucas Tam, Aug 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    313
    Lucas Tam
    Aug 18, 2003
  3. John Burke
    Replies:
    2
    Views:
    1,489
  4. Sam
    Replies:
    2
    Views:
    4,449
  5. One Handed Man \( OHM - Terry Burns \)

    Custom Control & System.Web.UI.IPostBackDataHandler.LoadPostData

    One Handed Man \( OHM - Terry Burns \), Jun 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,926
    One Handed Man \( OHM - Terry Burns \)
    Jun 17, 2004
Loading...

Share This Page