简体   繁体   English

将文本传递到Dropdownlist(使用Enum作为其数据源列表)时,ASP.net C#错误

[英]ASP.net C# Error when passing text to Dropdownlist, which uses an Enum as its datasource list


(Asp.net/C# VS2008) (Asp.net/C# VS2008)
I have a datagrid populated by a database and when pressing Edit, a form opens and populates fields/controls from that datagrid line. 我有一个由数据库填充的datagrid,当按Edit时,将打开一个表单,并从该datagrid行填充字段/控件。
My issue lies when trying to populate a dropdownlist box, which has a datasource from an ENum List. 我的问题在于尝试填充一个下拉列表框,该框具有来自ENum列表的数据源。
I Just cannot get the text from the datagrid cell to show up in the ddL, it should also equal one of the Enum Items and auto select it. 我只是无法从datagrid单元中获取要显示在ddL中的文本,它也应该等于枚举项之一并自动选择它。
My Code 我的密码
Pull from the datagrid cell gives me “Low” 从datagrid单元格中拉出时会显示“低”
ddl_reg.Text = e.Item.Cells[25].Text; ddl_reg.Text = e.Item.Cells [25] .Text;

    public void Populate_regstatus_dropdownlist()
{
    //if (!IsPostBack)
    //{
    //    ddl_reg.DataSource = Enum.GetNames(typeof(regstatus));
    //    ddl_reg.DataBind();
    //}
    //if (!IsPostBack)
    //{
    //    foreach (int value in Enum.GetValues(typeof(regstatus)))
    //    {
    //        ddl_reg.Items.Add(new ListItem(Enum.GetName(typeof(regstatus), value), value.ToString()));
    //    }
    //}
    ddl_reg.DataSource = Enum.GetNames(typeof(regstatus ));
    //ddl_reg.DataValueField = regstatus;
    //ddl_reg.DataTextField = "Low";
    //ddl_reg .SelectedItem = Enum.GetName(typeof (regstatus ));
    ddl_reg.DataBind();
    //ddl_reg.SelectedIndex = ddl_reg.Items.IndexOf(ddl_reg.Items.FindByText("Low"));


}
public enum regstatus
{
    NotSelected,
    Low,
    Medium,
    High
}

Error received is; 收到错误是;

ddl_reg' has a SelectedValue which is invalid because it does not exist in the list of items. ddl_reg'的SelectedValue无效,因为它在项目列表中不存在。 Parameter name: value 参数名称:值

I am new to C# but through searching your site i realise this means the value is not seen or has not been pulled and would really apreciate some help, or pointing in the right direction, cheers. 我是C#的新手,但是通过搜索您的网站,我意识到这意味着看不到或没有获得价值,并且确实会有所帮助,或者朝着正确的方向欢呼。

I believe the problem is that the drop down list doesn't contain the item you are passing. 我认为问题在于下拉列表不包含您要传递的项目。 You may check it before changing the selected value. 您可以在更改所选值之前进行检查。 Other chances are that the value you are getting from e.Item.Cells[25].Text may contain spaces, so you may trim before setting it to the drop down. 其他机会是,您从e.Item.Cells[25].Text获得的值可能包含空格,因此您可以在将其设置为下拉菜单之前进行修剪。

 if (ddl_reg.Items.FindByText("Low") != null)
            {

                ddl_reg.Text = e.Item.Cells[25].Text;
            }
            else
            {
                //Not found
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM