简体   繁体   中英

ASP.NET: Populate a dropdown list within a repeater (from database), and assign it a default value from the repeater datascource

I am using an asp.net repeater. Within the repeater, I have a dropdown list that is populated from the database. Each dropdown list for each item in the repeater may have a different default value. But when I use something like this:

<asp:DropDownList ID="NoSaleDropDown" runat="server" Width="446" AppendDataBoundItems="True" Text='<%#Eval("NoSaleReasonDesc") %>'></asp:DropDownList>

I get this error: 'NoSaleDropDown' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

The same holds true if I use: SelectedValue='<%#Eval("NoSaleReasonID")%>'

If I take out Text='<%#Eval("NoSaleReasonDesc") %>', the value that is displayed in the dropdownlist is the first item returned in the query that populates the list.

This code works fine for a textbox:

<asp:TextBox ID="NoSaleTxtBox" Width="446" runat="server" ReadOnly="true" Text='<%#Eval("NoSaleReasonDesc") %>' ></asp:TextBox></td>--%>

But, NoSaleReasonDesc sometimes needs to be edited, preferably with items from a dropdown list.

Does anyone have an idea?

Try this in your Repeater

<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductID">
            </asp:DropDownList>

you need set DataSourceID 、DataTextField 、DataValueField

and you will get result like my image. Try it : )

在此处输入图片说明

I see why it doesn't work in the .aspx file. When SelectedValue is evaluated, the dropdown list is null so it doesn't have a corresponding value in the list. I added "NoSaleDropDown.SelectedValue = DataBinder.Eval(e.Item.DataItem, "NoSaleReasonID").ToString();" to the drop down list event handler in the .aspx.cs file. Here is the entire event handler:

    protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DropDownList NoSaleDropDown = (DropDownList)e.Item.FindControl("NoSaleDropDown");
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            string sConnection2 = ConfigurationManager.ConnectionStrings["DevConn"].ConnectionString;
            SqlConnection dbConn2 = new SqlConnection(sConnection2);
            dbConn2.Open();

            string sqlString2 = "SELECT NoSaleReasonID, NoSaleReasonDesc FROM M_NoSaleReason";

            SqlCommand command2 = new SqlCommand();
            command2.CommandText = sqlString2;
            command2.Connection = dbConn2;

            SqlDataAdapter DataAdapter2 = new SqlDataAdapter();
            DataAdapter2.SelectCommand = command2;
            DataTable dt2 = new DataTable();
            DataAdapter2.Fill(dt2);

            if (NoSaleDropDown != null)
            {
                NoSaleDropDown.DataTextField = "NoSaleReasonDesc";
                NoSaleDropDown.DataValueField = "NoSaleReasonID";
                NoSaleDropDown.DataSource = dt2;
                NoSaleDropDown.DataBind();
                NoSaleDropDown.SelectedValue = DataBinder.Eval(e.Item.DataItem, "NoSaleReasonID").ToString();
            }
        }
    }

You should not set the selected value directly from the .aspx file. And also not with changing the text. It is better to set selcted item with the value (wich should be unique)

in .apsx:

    <asp:DropDownList ID="NoSaleDropDown" 
          runat="server" Width="446" AppendDataBoundItems="True"></asp:DropDownList>

in .aspx.vb (or similar for C#):

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    NoSaleDropDown.Items.Add(New ListItem("one", 1))
    NoSaleDropDown.Items.Add(New ListItem("two", 2))
    NoSaleDropDown.Items.Add(New ListItem("three", 3))
End Sub


Public NoSaleReasonId As Integer = 2

Protected Sub NoSaleDropDown_Load(sender As Object, e As EventArgs) Handles NoSaleDropDown.Load
    NoSaleDropDown.SelectedValue= NoSaleReasonId

End Sub

Works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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