Dynamic dropdownlist problem

M

^MisterJingo^

Hi all,

I have a web page which has a single dropdownlist containing 3 items.
Below the dropdownlist are two listboxes. Depending on the option
selected from the dropdownlist, the left most listbox populates itself
from a DB table, assigning the correct id to each item as a value.
In between the listboxes are two buttons. On clicking an 'add'
button, I am trying to grab the id of the item selected (the goal being
to place this id and relevant information gained from it in a dataset
to populate the other listbox), but I keep getting an "Object
reference not set to an instance of an object" error.
From googling I think this is to do with posting back when the button
is clicked? How can I get around this problem, and get the ID I need?
Any help would be greatly appreciated.

Chris
 
G

Guest

Could you post some code, it is a bit difcult to work out the problem from
your description. The error you mention is normally cause by trying to
access a variable/object that has been declared but not initalised. This is
often cause by having something like

stirng mystring = myotherstring;
mylabel.text=mystring;

If myothersting is set to null then mystring does not get initialised.
 
M

^MisterJingo^

Posting code might be a problem as i'm working within the framework of
a portal application and it's difficult to seperate pure ASP from what
i'm doing, but i'll try. I think i know what the problem is.

I have a drop down list:

<asp:DropDownList ID="ddbCollaboratorType" runat="server"
AutoPostBack="True" CssClass="Select">
<asp:ListItem>Other Individuals</asp:ListItem>
<asp:ListItem>Other Divisions</asp:ListItem>
<asp:ListItem>Other Organisations</asp:ListItem>
</asp:DropDownList>

Which posts back when an option is selected. Based upon this postback,
a switch statement catches the value from the selected item and pulls a
dataset from the portal:

switch (ddbCollaboratorType.SelectedItem.Value)
{
case "Other Individuals":

_ds = collab.otherIndividuals();

lbListCollaborators.DataSource = _ds;

lbListCollaborators.DataValueField="idusers";
lbListCollaborators.DataTextField="firstname";
lbListCollaborators.DataBind();

break;
case "Other Divisions":

_ds = collab.otherDivisions();

lbListCollaborators.DataSource = _ds;

lbListCollaborators.DataValueField="div_ID";
lbListCollaborators.DataTextField="division";
lbListCollaborators.DataBind();


break;
case "Other Organisations":

_ds = collab.otherOrganisations();

lbListCollaborators.DataSource = _ds;

lbListCollaborators.DataValueField="idorganisations";
lbListCollaborators.DataTextField="name";
lbListCollaborators.DataBind();

break;
default:
break;
}

The above switch statement then populates a listbox assigning values
and text. I have a button on this form, which, when clicked, I want to
get the value of the selected item (which is being stored in another
dataset). This is what is producing the error. Due to needing the
dropdownlist to repopulate the listbox I can't put it in a !isPostBack
block (which seems to solve this problem). Any ideas?
 
G

Guest

The line below is what is causing the problem

switch (ddbCollaboratorType.SelectedItem.Value)

You havn't actually set any values in your drop downlist, they are all set
to null.

You could either change it to

switch (ddbCollaboratorType.SelectedItem.Text)

or give your ListItems values as well as Text as below

<asp:DropDownList ID="ddbCollaboratorType" runat="server"
AutoPostBack="True" CssClass="Select">
<asp:ListItem Value="Value1">Other Individuals</asp:ListItem>
<asp:ListItem Value="Value2">Other Divisions</asp:ListItem>
<asp:ListItem Value="Value3">Other Organisations</asp:ListItem>
</asp:DropDownList>

you would then have to change you case arguments to the values in the DDL
rather than the dissplayed text.
 
M

^MisterJingo^

Hi clickon,

I tried the above, and still got the "Object reference not set to an
instance of an object." error.
I think I found the problem though. Each page reload rebinds the
listbox contents, so the value selected is lost during the postback -
and this is giving the error. I've solved it by having a simple int
keep track of the previous menu option, and then only rebinding the
list if the dropdownlist selection is changed ie so if selecting an
item from listbox without changing dropdown list, the listbox doesnt
re-bind and the value is kept eg:

if(ddbCollaboratorType.SelectedItem.Value == "Other Individuals" && id
!=1 )
{
_ds = collab.otherIndividuals();

lbListCollaborators.DataSource = _ds;
lbListCollaborators.DataValueField="idusers";
lbListCollaborators.DataTextField="firstname";
lbListCollaborators.DataBind();
id = 1;
}

Chris
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top