Hi Brett,
From your description you want to avoid getting the exception:
'ddlAssignedTo' has a SelectedValue which is invalid because it does not
exist in the list of items.
Is my understanding correct?
If so you don't have to add an extra field in the data base. This exception
is generally caused by binding the DropDownList on every postback. To
demonstrate how this exception will be thrown I've created a demo:
aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Uncomment following code to reproduce this exception
// this.DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.DropDownList1.SelectedValue = this.TextBox1.Text;
}
}
aspx:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
SelectCommand="SELECT * FROM [Table_1]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="theID"
DataValueField="theID">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="height: 26px" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
To reproduce please try following steps:
1. View the page and enter an item of the DropDownList in the TextBox.
2. Delete the record in the data base where the above item resides.
3. Click the button to post back.
Initially there's no exception. If you uncomment the code in the Page_Load
event handler the exception will be thrown.
So if we want to eliminate this exception, we should check if we bind the
DropDownList on every postback. If it's done in code behind, following code
may always help:
if(!IsPostBack)
{
//Bind DropDownList
}
If binding the DropDownList on every postback is a must in the requirement
I think we can do some check before setting the selected value of the
DropDownList.
As to your following concerns:
==================================================
So, is there a way to either
a) Allow entries into a DropDownList that are not in the databound table
(like a VB ComboBox), or
b) Add the missing value to the DropDownList items after it is bound to
the table?
==================================================
We can add an item in the PreRender event handler of the DropDownList
control. Something like:
void DropDownList1_PreRender(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
ddl.Items.Insert(0, new ListItem("hello", "world"));
}
If this problem isn't got solved please send me a demo project that can
help me understand your scenario better.
Regards,
Allen Chen
Microsoft Online Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Brett" <br*****@newsgroup.nospam>
| Subject: DropDownList has SelectedValue which in invalid
| Date: Fri, 19 Sep 2008 17:42:02 -0400
| Lines: 38
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
| X-RFC2646: Format=Flowed; Original
| Message-ID: <up**************@TK2MSFTNGP02.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: dsl-1-210.d01.scpnbh.pbtcomm.net 64.53.27.210
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSF TNGP02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:76417
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I have a DropDownList in an ASP.NET web form that is populated with items
| from a lookup table by binding that DropDownList to a SqlDataSource.
| However, the items in the lookup table can change over time. The problem
is
| that when an item has been removed from the lookup table, and a user
wants
| to retrieve a record that used the deleted item, the following error
occurs:
|
|
|
| 'ddlAssignedTo' has a SelectedValue which is invalid because it does not
| exist in the list of items.
|
|
|
| I need to allow the lookup table items to change, while still allowing
| retrieval of old records that used the now deleted items. I could add a
| field to the lookup table indicating whether an item is active or
inactive,
| but only the active ones (and the selected one) should appear in the
| DropDownList.
|
|
|
| So, is there a way to either
|
| a) Allow entries into a DropDownList that are not in the databound
table
| (like a VB ComboBox), or
|
| b) Add the missing value to the DropDownList items after it is bound
to
| the table?
|
|
|
|
|
| Thanks,
|
| Brett
|
|
|