Link to home
Start Free TrialLog in
Avatar of sunseshasai
sunseshasai

asked on

How to read asp:dropdownlist

I am using ASP.NET 1.1 Version.
I have two asp:dropdownlists named BooksTotalList and BooksSelectedList.

I am binding the BooksTotalList from database. And then I am using Javascript to move selected books from BooksTotalList to BooksSelectedList. This all works fine.

Now at the click of asp:button, I want to read the BooksSelectedList dropdownlist and do the processing.
When I try to read it gives me the following error
System.NullReferenceException: Object reference not set to instance of an object.

Please let me know how to solve this.

Thanks.
Avatar of DotNetThinker
DotNetThinker
Flag of United States of America image

Would you mind posting your code-behind, specifically the method in which the error is occuring?
Avatar of sunseshasai
sunseshasai

ASKER

Even though I want to read all the books selected, first i was trying to read one selected book. So I am selecting one book then clicking on the button. Here is the codebehind

protected void BooksSelectedBtn_Click(Object sender, EventArgs e)
{
      Response.Redirect( "abc.aspx?id=" + BooksSelectedList.SelectedItem.Value.ToString());
}

ok, step through the method and see what it's not finding. It's going to be one of these: BooksSelectedList, BooksSelectedList.SelectedItem, or BooksSelected.SelectedItem.Value. Also, do me a favor and post the code where you're databinding the lists.
Sunseshasi,

Check out this free demo (with source code) from MetaBuilders:

http://www.metabuilders.com/Tools/DualList.aspx

Using the JavaScript they provide there, you should be able to get your scenario up and running.

Thanks~
Todd

ASKER CERTIFIED SOLUTION
Avatar of Niraj_Singh
Niraj_Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Niraj-

That's exactly what I was going to say. In fact, I had that full response typed before I found the meatabuilders demo. It is in effect an implementation of the suggestion you made. They use a hidden input field to track which items have changed and reference that hidden input to communicate changes back to the server.

That said, it is good to point out that the reason for these errors is exactly as you stated. Client side changes are "invisible" to the server and not communicated during the postback. You need to adopt an approach such as that in the DualList example to overcome this problem.

Thanks~
Todd
Hi Niraj,
So how to access the Javascript's hidden variable from the codebehind code?

Niraj-

I'd highly encourage you to check out the MetaBuilders demo. You can download their working example with full source code to see exactly how things are done. In short, this is (some of) the server code being used by their list controls to access the hidden data field on a PostBack:

private Boolean loadNewItems(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) {
   this.EnsureChildControls();
   String postedValue = postCollection[this.itemTracker.UniqueID];
                  
   Boolean result = false;

   ArrayList currentSelection = this.SelectedIndicesInternal;
   
   if (postedValue != null && postedValue.Length != 0 ) {
      ListCommand[] commands = ListCommand.Split( postedValue.Trim() );
      foreach( ListCommand command in commands ) {
      if ( command.Operator == "+" ) {
      ListItem newItem = new ListItem(command.Text, command.Value);
      if ( command.Index >= 0 && command.Index <= this.Items.Count - 1 ) {
            this.Items.Insert(command.Index,newItem);
        } else {
            this.Items.Add(newItem);
      }
    ....

The key here is that they must implement code in the PostBackHandler to extract the form values from the hidden field (contained in the postCollection). Download the MetaBuilder DualList source code to see the full context of this code.

Let me know if that helps.

Thanks~
Todd