更改嵌套的中继器内DropDownList的选定值

更改嵌套的中继器内DropDownList的选定值

本文介绍了更改嵌套的中继器内DropDownList的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图DropDownList中选定值从C#​​code改变:

 <转发器GT;
   <转发器GT;
      <&下拉GT;

和我在aspx页面的数据源添加到下拉列表
每一件事工作正常..但我想改变的下拉列表中选择的值的onload
要能显示用户的previous选择的名称时,编辑表格

 保护无效prepairDropDownList(对象发件人,EventArgs的发送)
    {
        shiftrepeater.DataBind();
        如果(shiftrepeater.Items.Count大于0)
        {
            对于(INT shiftCount转换= 0;&shiftCount转换LT; shiftrepeater.Items.Count; shiftCount转换++)
            {
                直放站TEMP =(中继器)shiftrepeater.Items [shiftCount转换] .FindControl(saturdayrepeater);
                如果(temp.Items.Count大于0)
                {
                    对于(诠释计数= 0; COUNT< temp.Items.Count;计数++)
                    {
                        DropDownList的DS =(DropDownList的)temp.Items [统计] .FindControl(userdropdown);
                        HiddenField HF =(HiddenField)temp.Items [统计] .FindControl(hiddenid); //包含ID如果该字段
                        SarcShiftUser用户= CRUD< SarcShiftUser>获得(int.Parse(hf.Value)); //一个方法来选择与特定ID的用户,并将其添加从类sarcshiftuser对象                        如果(user.id == 0)
                            ds.SelectedValue =;
                        其他
                        {
                            ds.SelectedValue = user.user_id +;                        }
                    }
                }            }
        }
        shiftrepeater.DataBind();
    }

我这个方法添加到中继器的Onload:
但没有任何变化和previous的名字并未出现。

P.S:用户对象是正确的并且user.user_id也是正确的
P.S 2:我尝试添加ds.DataBind();改变选择,但给该错误后:

  System.ArgumentOutOfRangeException:'userdropdown'具有的SelectedValue,因为它不在项目列表中存在这是无效的。


解决方案

设置最安全的方法的SelectedItem A DROPDOWNLIST 是设置的SelectedIndex 是这样的:

  ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString()));

I tried to change a dropdownlist selected value from a c# code :

<repeater >
   <repeater>
      <dropdown>

and I add a datasource in aspx page to the dropdownevery thing works fine .. but I want to change the selected value onload of the dropdownto be able to show the user the previous selected name when edit the table

protected void PrepairDropDownList(object sender,EventArgs e)
    {
        shiftrepeater.DataBind();
        if (shiftrepeater.Items.Count > 0)
        {
            for (int shiftcount = 0; shiftcount < shiftrepeater.Items.Count; shiftcount++)
            {
                Repeater temp = (Repeater)shiftrepeater.Items[shiftcount].FindControl("saturdayrepeater");
                if (temp.Items.Count > 0)
                {
                    for (int count = 0; count < temp.Items.Count; count++)
                    {
                        DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
                        HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");//contain the id if the field
                        SarcShiftUser user = CRUD<SarcShiftUser>.Get(int.Parse(hf.Value)); //a method to select a user with a specific id and add it to object from class sarcshiftuser

                        if (user.id == 0)
                            ds.SelectedValue = "";
                        else
                        {
                            ds.SelectedValue = user.user_id + "";

                        }
                    }
                }

            }
        }
        shiftrepeater.DataBind();
    }

I add this method to Onload of the repeater :but nothing change and the previous name didn't show

P.S : the user object is correct and the user.user_id is also correctP.S 2 : I tried to add a ds.DataBind(); after changing the selected but give that error :

System.ArgumentOutOfRangeException: 'userdropdown' has a SelectedValue which is invalid because it does not exist in the list of items.
解决方案

Safest way to set SelectedItem of a DropdownList is to set the SelectedIndex like this:

ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString()));

这篇关于更改嵌套的中继器内DropDownList的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:46