'Itemimage1'具有选区值,该选区值无效,因为它在项目列表中不存在。参数名称:值第2部分 [英] 'Itemimage1' has a selectedvalue which is invalid because it does not exist in the list of items. Parameter name: value part 2

查看:99
本文介绍了'Itemimage1'具有选区值,该选区值无效,因为它在项目列表中不存在。参数名称:值第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站'itemImage1'中存在此异常错误的问题,其SelectedValue无效,因为它在项目列表中不存在。参数名称:值。



为了总结代码,我有两个叫做吉他和贝司的单选按钮。如果管理员选择吉他,它将生成指定的文件路径并查找该位置内的图像。然后,它将在下拉列表中显示可用图像。同样适用于低音但具有不同的位置。



我第一次尝试使用代码,因为我选择了吉他单片机,它工作得很好并且显示了可用的图像。下拉列表。但是,当我改变为bass radiobutton时,它给了我上面提到的异常错误。



我试过解除它就像设置数据源和selectedvalue一样null,清除项目并将selectedindex设置为-1。但一切都行不通。请给我建议或提供解决方案?



我尝试过:



以下是代码:



I have a problem with this exception error 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 radiobutton, it is giving me the exception error that i have mentioned above.

I have tried unbinding it like setting the datasource and selectedvalue to null, clearing the items and setting the selectedindex to -1. But all is not working. Kindly advice or provide solution for this?

What I have tried:

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;

    }
}





这是aspx代码:





Here is the aspx code:

<tr>
                    <td style="width: 160px; height: 37px;">

                    Item Type:</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;">

                    Item Image1:</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;">

                    Item Image2:</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>

推荐答案

使用 FindByValue方法 [ ^ ]或 ListItemCollection.FindByText方法 [ ^ ]到检查该项目是否存在于该集合中。



示例:

use FindByValue Method[^] or ListItemCollection.FindByText Method [^] to check whether the item is present in the collection.

example:
string ValueToSet = "someValue";
           var targetItemByValue = itemImage1.Items.FindByValue(ValueToSet);
           if (targetItemByValue != null)
               targetItemByValue.Selected = true;

           //or


           string TextToSet = "someText";
           var targetItemByText = itemImage1.Items.FindByText(TextToSet);
           if (targetItemByText != null)
               targetItemByText.Selected = true;


这篇关于'Itemimage1'具有选区值,该选区值无效,因为它在项目列表中不存在。参数名称:值第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆