Dynamic dropdownlist in repeater, ASP.NET

≯℡__Kan透↙ 提交于 2020-01-02 07:35:48

问题


Basically, the codes I have is from here : http://ranafaisal.wordpress.com/2009/02/17/dynamically-adding-removing-textboxes-in-aspnet-repeater/

However, the thing is that, I will need a dropdownlist with textboxes. The purpose of having dropdownlist is to allow users to select their country of origin. They have the option to Add or Remove the particulars they have entered before.

This is my error message:

'ddlName' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

This is my dropdownlist code inside a repeater in Default.aspx

<asp:DropDownList ID="ddlName" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "ddl") %>'></asp:DropDownList>

The codes behind is exactly the same as the link I provided.

  • Points to note: There is no database involved.

Please not tell me to google or anything because I have been googling for the past few hours, to no avail. I definitely have googled enough, and tried the solutions given by others before posting here. I am pretty much at my wits end

To add-on, I cannot even start-up my application because of the dropdownlist problem.


回答1:


The problem is you need to fill the DropDownList possible options before you set the selected value which you are trying to do inline with the Eval. I would switch it to use the OnDataBinding of the DropDownList and do what you need there.

Example:

<asp:DropDownList ID="ddlName" runat="server" OnDataBinding="ddlName_DataBinding" />

protected void ddlName_DataBinding(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)(sender);

    // Fill your ddl here (eg. ddl.Items.Add("abc", xyz");
    // Make sure the value you are going to set the selected item to has been added

    // Now set the selected value since it will now exist.
    ddl.SelectedValue = Eval("ddl").ToString(); 
}


来源:https://stackoverflow.com/questions/6560114/dynamic-dropdownlist-in-repeater-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!