I have a problem with this exception in my website itemImage1
has a SelectedValue
which is invalid because it does not exist in the list of items.
Parameter name: value
.
To summarize the code, I have two radio buttons called guitar and bass. If the admin select guitar, it will then generate the specified file path and look for the images within the location. It will then display the available images in the dropdownlist. Same goes for bass but with a different location.
My first try with the code, as I select the guitar radiobutton, it worked perfectly and displays the available images in the dropdownlist. But when I change to the bass radio button, it is giving me the exception that I have mentioned above.
I have tried unbinding it by setting the datasource and selectedvalue to null
, clearing the items and setting the SelectedIndex
to -1, but it still isn't working. Where am I going wrong?
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
ShowItemImages();
}
private void ShowItemImages()
{
var imageList = new ArrayList();
if (itemType1.Checked)
{
itemImage1.Items.Clear();
itemImage2.Items.Clear();
itemImage1.SelectedIndex = -1;
itemImage2.SelectedIndex = -1;
itemImage1.SelectedValue = null;
itemImage2.SelectedValue = null;
itemImage1.DataSource = null;
itemImage1.DataBind();
itemImage2.DataSource = null;
itemImage2.DataBind();
itemselectedValueGuitar1 = itemImage1.SelectedValue;
itemselectedValueGuitar2 = itemImage2.SelectedValue;
var images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/"));
foreach (var image in images)
{
var imageName = image.Substring(image.LastIndexOf(@"\") + 1);
imageList.Add(imageName);
}
itemImage1.DataSource = imageList;
itemImage1.DataBind();
itemImage2.DataSource = imageList;
itemImage2.DataBind();
itemImage1.SelectedValue = itemselectedValueGuitar1;
itemImage2.SelectedValue = itemselectedValueGuitar2;
}
else if (itemType2.Checked)
{
itemImage1.Items.Clear();
itemImage2.Items.Clear();
itemImage1.SelectedIndex = -1;
itemImage2.SelectedIndex = -1;
itemImage1.SelectedValue = null;
itemImage2.SelectedValue = null;
itemImage1.DataSource = null;
itemImage1.DataBind();
itemImage2.DataSource = null;
itemImage2.DataBind();
itemselectedValueBass1 = itemImage1.SelectedValue;
itemselectedValueBass2 = itemImage2.SelectedValue;
var images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Bass/"));
foreach (var image in images)
{
var imageName = image.Substring(image.LastIndexOf(@"\") + 1);
imageList.Add(imageName);
}
itemImage1.DataSource = imageList;
itemImage1.DataBind();
itemImage2.DataSource = imageList;
itemImage2.DataBind();
itemImage1.SelectedValue = itemselectedValueBass1;
itemImage2.SelectedValue = itemselectedValueBass2;
}
}
Here is the aspx code:
<tr>
<td style="width: 160px; height: 37px;"><strong>Item Type:</strong></td>
<td style="height: 37px">
<asp:RadioButton ID="itemType1" runat="server" Text="Guitar" AutoPostBack="True" GroupName="ItemType"/>
<asp:RadioButton ID="itemType2" runat="server" Text="Bass" AutoPostBack="True" GroupName="ItemType"/>
</td>
</tr>
<tr>
<td style="width: 160px; height: 37px;">
<strong>Item Image1:</strong></td>
<td style="height: 37px">
<asp:DropDownList ID="itemImage1" runat="server" Width="300px">
</asp:DropDownList>
<br />
<asp:FileUpload ID="itemFileUpload1" runat="server" />
<asp:Button ID="itemUploadImage1" runat="server" Text="Upload Image" OnClick="itemUploadImage1_Click"/>
</td>
</tr>
<tr>
<td style="width: 160px; height: 37px;">
<strong>Item Image2:</strong></td>
<td style="height: 37px">
<asp:DropDownList ID="itemImage2" runat="server" Width="300px"></asp:DropDownList>
<br />
<asp:FileUpload ID="itemFileUpload2" runat="server" />
<asp:Button ID="itemUploadImage2" runat="server" Text="Upload Image" OnClick="itemUploadImage2_Click"/>
</td>
</tr>