Can't add items to dropdownlist

G

Guest

Hi again,
I really hope that there is a simple solution to this one, cause I'm having
simular things happening to other controls on this usercontrol. I have the
following control:
<asp:DropDownList ID="cmbOptions" runat="server" Width="195px">
<asp:ListItem Text="Search by PO ID" Value=0 Selected="True"
/>
<asp:ListItem Text="Search by Field" Value=1 />
<asp:ListItem Text="Search between dates" Value=2 />
<asp:ListItem Text="Search Most recent Num days" Value=3 />
</asp:DropDownList><br />
This would be the default list that all user would see. If a Adin logged in,
I wanted to add a few more items to the list using the follwoing code in the
usercontrol:
cmbOptions.Items.Add(New ListItem("Search by UserId", "4"))
cmbOptions.Items.Add(New ListItem("Awaiting Approval", "5"))
If I check the count before each one, and then check after, the count does
increment, but after the page re-renders, the two exrta items are not listed.
Am I missing something. This is driving me crazy. BTY, EnableViewState is
enabled for the dropdownlist control if that makes a difference in this case.
Thanks for any suggestions.
Michael
 
G

Guest

I think you've run into the classic misunderstanding that when a page posts
back, you need to "reconstitute" any custom items you've added to your
control in the interim.

So for example, in your page_load you might do something like

if( User.IsInRole("Admins")
{
AddBackMyExtraOptions();
}

Hoipe that helps.
Peter
 
G

Guest

Hi Peter,
Thanks for the reply. I can't use the load event of the usercontrol because
the search usercontrol is loaded along with the master page. Once everything
is rendered, the user would login and then I would find out if the user is
admin or not. For another purpose, I had setup a event for the login
usercontrol to kick off and pass the UserInfo to the event. One thing I did
try was to call a function in the POSearch control to add the new items if
the user is admin, but that failed also(without error), the new items just
don't display. Does this make since?
This is the sequence of events:
1. User logs in. Login User control kicks off event.
2. Master Page captures the event.
3. Master Page event calls POSearch.SetUser (not current function name, I'm
at home and can't remember the name of function)
4. POSearch then should Add new items to the dropdownlist like so:
cmbOptions.Items.Add(New ListItem("Search by UserId", "4"))
cmbOptions.Items.Add(New ListItem("Awaiting Approval", "5"))
This is where something goes wrong. I can check the item count and it will
be correct right after these two commands are run. Why the new items don't
show is really confusing me. Thanks again for the reply.
Michael
 

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,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top