在下拉列表中添加一个默认值

时间:2021-03-15 17:07:51

I'm trying to add just 1 default value into 1 of my dropdownlistbox.

我想在dropdownlistbox中添加一个默认值。

I've got 2;

我有2;

  1. DDLocation
  2. DDLocation
  3. DDLpolice
  4. DDLpolice

I added items into the DDLocation via the properties. So whenever the user attempts to click one of the items, the DDLpolice will automatically get the value from the database. Unfortunately, there is a persistant bug such as whenever there is only 1 value available, the data will not be displayed. Data will only be displayed if you have 2 values to select before it is able to display the first choice. I bind my DDLocation with the codes below. I added a default value below. Unfortunately the default value only works upon selecting a choice of DDLocation

我通过属性将项目添加到DDLocation中。因此,每当用户试图单击其中一项时,DDLpolice将自动从数据库中获取值。不幸的是,有一个持久的bug,例如只要有一个值可用,数据就不会显示出来。只有当您有两个要选择的值时,数据才会显示出来。我将DDLocation与下面的代码绑定。我在下面添加了一个默认值。不幸的是,默认值只在选择DDLocation时有效

protected void DDLocation_SelectedIndexChanged(object sender, EventArgs e)
{
        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "Select policeid from LoginRegisterPolice where location='" + lblocation.SelectedItem.Text + "' AND status='available' AND handle='offcase'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);

                DDLpolice.DataSource = ds2;
                DDLpolice.DataTextField = "policeid";
                DDLpolice.DataValueField = "policeid";
                DDLpolice.DataBind();

                DDLpolice.Items.Insert(0, new ListItem("Select Officer", "NA"));
            }

            connAdd.Close();
        }
}

I tried to add a default value into my DDLpolice just like how i did to my previous DDL. Unfortunately, the DDLpolice is being binded, hence there's no option for me to add a default value. I tried using this 2 methods and all failed

我试图向DDLpolice中添加一个默认值,就像我对之前的DDL所做的那样。不幸的是,DDLpolice正在被绑定,因此我无法添加默认值。我尝试了这两种方法,都失败了

I added a Text for this.

我为此添加了一个文本。

<asp:DropDownList ID="DDLpolice" runat="server" DataTextField="dropdownpolice" DataValueField="dropdownpolice" Text="Select Officer" OnSelectedIndexChanged ="DDLpolice_SelectedIndexChanged1" style="height: 22px">
    </asp:DropDownList>

I also tried using this method

我也尝试过使用这种方法

protected void DDLpolice_SelectedIndexChanged1(object sender, EventArgs e)
{
    DDLpolice.Items.Add("Select Officer");
}

Hence in conclusion, i reckon i'm not able to add due to the binding of the DDL to the database. Any help would be appreciated.

综上所述,我认为由于DDL与数据库的绑定,我无法添加。如有任何帮助,我们将不胜感激。

Regards.

的问候。


UPDATE! I'm finally able to add a default value by using this method given by AD.net

更新!通过使用AD.net给出的这个方法,我终于可以添加一个默认值了

    <asp:DropDownList ID="DDLpolice" runat="server" DataTextField="dropdownpolice"       DataValueField="dropdownpolice" OnSelectedIndexChanged ="DDLpolice_SelectedIndexChanged1"     style="height: 22px"
    AppendDataBoundItems="true">
    <asp:ListItem Value="">Select Officer</asp:ListItem>
    </asp:DropDownList>

Basically, you would have to add an AppendDataBoundItem method into your source code(html-side). After that, add a ListItemValue which will contain your default value.

基本上,您必须将AppendDataBoundItem方法添加到源代码(html-side)中。然后,添加一个ListItemValue,它将包含您的默认值。

1 个解决方案

#1


2  

And that makes sense. Whenever you do DataBind for the DropDownList, it loses any previous values. You can try to add the extra item after the OnDataBound event for the ddl, which should fire after it finishes DataBinding. You can also try playing with this property AppendDataBoundItems and declare a default item in the html.

这是有意义的。无论何时为下拉列表做DataBind,它都会丢失任何先前的值。您可以尝试在ddl的OnDataBound事件之后添加额外的项,该事件应该在它完成数据库之后启动。您还可以尝试使用这个属性AppendDataBoundItems并在html中声明一个默认项。

AppendDataBoundItems let's you append items each time you do databind, so it should not be the right choice for you unless you delete items other than the first one.

AppendDataBoundItems让我们在每次执行databind时都追加项目,因此它不应该是您的正确选择,除非您删除除第一个项目之外的其他项目。

You should follow the first approach, insert the default item ondatabound event.

您应该遵循第一种方法,插入默认项ondatabound事件。

protected void DDLpoliceDataBound(object sender, EventArgs e)
{
    DDLpolice.Items.Insert(0, new ListItem("Select Officer", "NA"));
}

<asp:DropDownList runat="server" ID="DDLpolice" 
ondatabound="DDLpoliceDataBound"></asp:DropDownList>

#1


2  

And that makes sense. Whenever you do DataBind for the DropDownList, it loses any previous values. You can try to add the extra item after the OnDataBound event for the ddl, which should fire after it finishes DataBinding. You can also try playing with this property AppendDataBoundItems and declare a default item in the html.

这是有意义的。无论何时为下拉列表做DataBind,它都会丢失任何先前的值。您可以尝试在ddl的OnDataBound事件之后添加额外的项,该事件应该在它完成数据库之后启动。您还可以尝试使用这个属性AppendDataBoundItems并在html中声明一个默认项。

AppendDataBoundItems let's you append items each time you do databind, so it should not be the right choice for you unless you delete items other than the first one.

AppendDataBoundItems让我们在每次执行databind时都追加项目,因此它不应该是您的正确选择,除非您删除除第一个项目之外的其他项目。

You should follow the first approach, insert the default item ondatabound event.

您应该遵循第一种方法,插入默认项ondatabound事件。

protected void DDLpoliceDataBound(object sender, EventArgs e)
{
    DDLpolice.Items.Insert(0, new ListItem("Select Officer", "NA"));
}

<asp:DropDownList runat="server" ID="DDLpolice" 
ondatabound="DDLpoliceDataBound"></asp:DropDownList>